From 1d17449c0cb809cec8ca16f2ac860f6377bfa305 Mon Sep 17 00:00:00 2001 From: skyleo Date: Mon, 2 Dec 2019 19:58:08 +0100 Subject: Add unit->cbl2ud for when using const block_list --- src/map/unit.c | 33 +++++++++++++++++++++++++++++++++ src/map/unit.h | 1 + 2 files changed, 34 insertions(+) (limited to 'src') diff --git a/src/map/unit.c b/src/map/unit.c index 418bbe4b0..acf64e820 100644 --- a/src/map/unit.c +++ b/src/map/unit.c @@ -99,6 +99,38 @@ static struct unit_data *unit_bl2ud(struct block_list *bl) } } +/** + * Returns the const unit_data for the given const block_list. If the object is using + * shared unit_data (i.e. in case of BL_NPC), it returns the shared data. + * + * __Warning:__ if bl->type is not known or NULL, + * an assertion will be triggered and NULL returned. + * @param bl block_list to process, it is expected to be not NULL. + * @return a pointer to the given object's unit_data + **/ +static const struct unit_data *unit_cbl2ud(const struct block_list *bl) +{ + Assert_retr(NULL, bl != NULL); + switch (bl->type) { + case BL_PC: + return &BL_UCCAST(BL_PC, bl)->ud; + case BL_MOB: + return &BL_UCCAST(BL_MOB, bl)->ud; + case BL_PET: + return &BL_UCCAST(BL_PET, bl)->ud; + case BL_NPC: + return BL_UCCAST(BL_NPC, bl)->ud; + case BL_HOM: + return &BL_UCCAST(BL_HOM, bl)->ud; + case BL_MER: + return &BL_UCCAST(BL_MER, bl)->ud; + case BL_ELEM: + return &BL_UCCAST(BL_ELEM, bl)->ud; + default: + Assert_retr(NULL, false); + } +} + /** * Returns the unit_data for the given block_list. If the object is using * shared unit_data (i.e. in case of BL_NPC), it recreates a copy of the @@ -3054,6 +3086,7 @@ void unit_defaults(void) unit->final = do_final_unit; /* */ unit->bl2ud = unit_bl2ud; + unit->cbl2ud = unit_cbl2ud; unit->bl2ud2 = unit_bl2ud2; unit->init_ud = unit_init_ud; unit->attack_timer = unit_attack_timer; diff --git a/src/map/unit.h b/src/map/unit.h index 57f04b57a..7bf94d3e1 100644 --- a/src/map/unit.h +++ b/src/map/unit.h @@ -103,6 +103,7 @@ struct unit_interface { int (*final) (void); /* */ struct unit_data* (*bl2ud) (struct block_list *bl); + const struct unit_data* (*cbl2ud) (const struct block_list *bl); struct unit_data* (*bl2ud2) (struct block_list *bl); void (*init_ud) (struct unit_data *ud); int (*attack_timer) (int tid, int64 tick, int id, intptr_t data); -- cgit v1.2.3-70-g09d2