From 205558527913c42956bf5bbe2afbceac3dad1935 Mon Sep 17 00:00:00 2001 From: Haru Date: Mon, 28 Dec 2015 00:24:24 +0100 Subject: Replaced some map->id2sd calls with the proper map->id2XX function - Also added new map->id2XX convenience functions for the other bl types. Signed-off-by: Haru --- src/map/atcommand.c | 12 ++-- src/map/chat.c | 14 ++-- src/map/clif.c | 48 +++++++------ src/map/map.c | 195 +++++++++++++++++++++++++++++++++++++++++++--------- src/map/map.h | 18 +++-- src/map/mob.c | 4 +- src/map/npc.c | 25 +++---- src/map/npc_chat.c | 24 ++++--- src/map/pc.c | 19 ++--- src/map/script.c | 194 ++++++++++++++++++++++++++------------------------- src/map/skill.c | 20 ++++-- 11 files changed, 360 insertions(+), 213 deletions(-) diff --git a/src/map/atcommand.c b/src/map/atcommand.c index ef569afa1..9d03dd057 100644 --- a/src/map/atcommand.c +++ b/src/map/atcommand.c @@ -3744,10 +3744,11 @@ ACMD(reloadscript) { * 3 = Shows the chats in that map TODO# add the missing mapflags e.g. adjust_skill_damage to display *------------------------------------------*/ -ACMD(mapinfo) { +ACMD(mapinfo) +{ const struct map_session_data *pl_sd; struct s_mapiterator* iter; - struct chat_data *cd = NULL; + const struct chat_data *cd = NULL; char direction[12]; int i, m_id, chat_num = 0, list = 0, vend_num = 0; unsigned short m_index; @@ -3786,7 +3787,7 @@ ACMD(mapinfo) { if( pl_sd->mapindex == m_index ) { if( pl_sd->state.vending ) vend_num++; - else if( (cd = (struct chat_data*)map->id2bl(pl_sd->chatID)) != NULL && cd->usersd[0] == pl_sd ) + else if ((cd = map->id2cd(pl_sd->chatID)) != NULL && cd->usersd[0] == pl_sd) chat_num++; } } @@ -3965,10 +3966,7 @@ ACMD(mapinfo) { clif->message(fd, msg_fd(fd,1113)); // ----- Chats in Map ----- iter = mapit_getallusers(); for (pl_sd = BL_UCCAST(BL_PC, mapit->first(iter)); mapit->exists(iter); pl_sd = BL_UCCAST(BL_PC, mapit->next(iter))) { - if ((cd = (struct chat_data*)map->id2bl(pl_sd->chatID)) != NULL && - pl_sd->mapindex == m_index && - cd->usersd[0] == pl_sd) - { + if ((cd = map->id2cd(pl_sd->chatID)) != NULL && pl_sd->mapindex == m_index && cd->usersd[0] == pl_sd) { safesnprintf(atcmd_output, sizeof(atcmd_output), msg_fd(fd,1114), // Chat: %s | Player: %s | Location: %d %d cd->title, pl_sd->status.name, cd->bl.x, cd->bl.y); clif->message(fd, atcmd_output); diff --git a/src/map/chat.c b/src/map/chat.c index 45d4ea0cc..7b4894deb 100644 --- a/src/map/chat.c +++ b/src/map/chat.c @@ -140,7 +140,7 @@ bool chat_joinchat(struct map_session_data* sd, int chatid, const char* pass) { nullpo_ret(sd); nullpo_ret(pass); - cd = (struct chat_data*)map->id2bl(chatid); + cd = map->id2cd(chatid); if( cd == NULL || cd->bl.type != BL_CHAT || cd->bl.m != sd->bl.m || sd->state.vending || sd->state.buyingstore || sd->chatID || ((cd->owner->type == BL_NPC) ? cd->users+1 : cd->users) >= cd->limit ) { @@ -204,7 +204,7 @@ int chat_leavechat(struct map_session_data* sd, bool kicked) { nullpo_retr(0, sd); - cd = (struct chat_data*)map->id2bl(sd->chatID); + cd = map->id2cd(sd->chatID); if( cd == NULL ) { pc_setchatid(sd, 0); return 0; @@ -279,7 +279,7 @@ bool chat_changechatowner(struct map_session_data* sd, const char* nextownername nullpo_ret(sd); nullpo_ret(nextownername); - cd = (struct chat_data*)map->id2bl(sd->chatID); + cd = map->id2cd(sd->chatID); if( cd == NULL || (struct block_list*) sd != cd->owner ) return false; @@ -324,7 +324,7 @@ bool chat_changechatstatus(struct map_session_data* sd, const char* title, const nullpo_ret(title); nullpo_ret(pass); - cd = (struct chat_data*)map->id2bl(sd->chatID); + cd = map->id2cd(sd->chatID); if( cd==NULL || (struct block_list *)sd != cd->owner ) return false; @@ -352,7 +352,7 @@ bool chat_kickchat(struct map_session_data* sd, const char* kickusername) { nullpo_ret(sd); nullpo_ret(kickusername); - cd = (struct chat_data *)map->id2bl(sd->chatID); + cd = map->id2cd(sd->chatID); if( cd==NULL || (struct block_list *)sd != cd->owner ) return false; @@ -409,8 +409,8 @@ bool chat_deletenpcchat(struct npc_data* nd) { struct chat_data *cd; nullpo_ret(nd); - cd = (struct chat_data*)map->id2bl(nd->chat_id); - if( cd == NULL ) + cd = map->id2cd(nd->chat_id); + if (cd == NULL) return false; chat->npc_kick_all(cd); diff --git a/src/map/clif.c b/src/map/clif.c index 5419e6cd5..a1581553d 100644 --- a/src/map/clif.c +++ b/src/map/clif.c @@ -446,12 +446,12 @@ bool clif_send(const void* buf, int len, struct block_list* bl, enum send_target case CHAT_WOS: nullpo_retr(true, bl); { - struct chat_data *cd; - if (sd) { - cd = (struct chat_data*)map->id2bl(sd->chatID); - } else if (bl->type == BL_CHAT) { - cd = (struct chat_data*)bl; - } else break; + const struct chat_data *cd = NULL; + if (sd != NULL) { + cd = map->id2cd(sd->chatID); + } else { + cd = BL_CCAST(BL_CHAT, bl); + } if (cd == NULL) break; for(i = 0; i < cd->users; i++) { @@ -4119,8 +4119,8 @@ void clif_getareachar_pc(struct map_session_data* sd,struct map_session_data* ds nullpo_retv(sd); nullpo_retv(dstsd); if( dstsd->chatID ) { - struct chat_data *cd = NULL; - if( (cd = (struct chat_data*)map->id2bl(dstsd->chatID)) && cd->usersd[0]==dstsd) + struct chat_data *cd = map->id2cd(dstsd->chatID); + if (cd != NULL && cd->usersd[0] == dstsd) clif->dispchat(cd,sd->fd); } else if( dstsd->state.vending ) clif->showvendingboard(&dstsd->bl,dstsd->message,sd->fd); @@ -4202,7 +4202,7 @@ void clif_getareachar_unit(struct map_session_data* sd,struct block_list *bl) { { struct npc_data *nd = (struct npc_data *)bl; if (nd->chat_id != 0) - clif->dispchat((struct chat_data *)map->id2bl(nd->chat_id),sd->fd); + clif->dispchat(map->id2cd(nd->chat_id), sd->fd); if (nd->size == SZ_BIG) clif->specialeffect_single(bl,423,sd->fd); else if (nd->size == SZ_MEDIUM) @@ -4615,9 +4615,8 @@ int clif_outsight(struct block_list *bl,va_list ap) case BL_PC: if (sd->vd.class_ != INVISIBLE_CLASS) clif->clearunit_single(bl->id,CLR_OUTSIGHT,tsd->fd); - if(sd->chatID){ - struct chat_data *cd; - cd=(struct chat_data*)map->id2bl(sd->chatID); + if (sd->chatID) { + struct chat_data *cd = map->id2cd(sd->chatID); if(cd->usersd[0]==sd) clif->dispchat(cd,tsd->fd); } @@ -9969,9 +9968,8 @@ void clif_parse_HowManyConnections(int fd, struct map_session_data *sd) { clif->user_count(sd, map->getusers()); } -void clif_parse_ActionRequest_sub(struct map_session_data *sd, int action_type, int target_id, int64 tick) { - struct block_list *target = NULL; - +void clif_parse_ActionRequest_sub(struct map_session_data *sd, int action_type, int target_id, int64 tick) +{ nullpo_retv(sd); if (pc_isdead(sd)) { clif->clearunit_area(&sd->bl, CLR_DEAD); @@ -9998,8 +9996,10 @@ void clif_parse_ActionRequest_sub(struct map_session_data *sd, int action_type, switch(action_type) { case 0x00: // once attack case 0x07: // continuous attack - if( (target = map->id2bl(target_id)) && target->type == BL_NPC ) { - npc->click(sd, (struct npc_data *)target); + { + struct npc_data *nd = map->id2nd(target_id); + if (nd != NULL) { + npc->click(sd, nd); return; } @@ -10019,6 +10019,7 @@ void clif_parse_ActionRequest_sub(struct map_session_data *sd, int action_type, pc->delinvincibletimer(sd); pc->update_idle_time(sd, BCIDLE_ATTACK); unit->attack(&sd->bl, target_id, action_type != 0); + } break; case 0x02: // sitdown if (battle_config.basic_skill_check && pc->checkskill(sd, NV_BASIC) < 3) { @@ -10276,13 +10277,10 @@ void clif_parse_TakeItem(int fd, struct map_session_data *sd) __attribute__((non /// 009f .L (CZ_ITEM_PICKUP) /// 0362 .L (CZ_ITEM_PICKUP2) /// There are various variants of this packet, some of them have padding between fields. -void clif_parse_TakeItem(int fd, struct map_session_data *sd) { - struct flooritem_data *fitem; - int map_object_id; - - map_object_id = RFIFOL(fd,packet_db[RFIFOW(fd,0)].pos[0]); - - fitem = (struct flooritem_data*)map->id2bl(map_object_id); +void clif_parse_TakeItem(int fd, struct map_session_data *sd) +{ + int map_object_id = RFIFOL(fd,packet_db[RFIFOW(fd,0)].pos[0]); + struct flooritem_data *fitem = map->id2fi(map_object_id); do { if (pc_isdead(sd)) { @@ -10290,7 +10288,7 @@ void clif_parse_TakeItem(int fd, struct map_session_data *sd) { break; } - if (fitem == NULL || fitem->bl.type != BL_ITEM || fitem->bl.m != sd->bl.m) + if (fitem == NULL || fitem->bl.m != sd->bl.m) break; if( sd->sc.count && ( diff --git a/src/map/map.c b/src/map/map.c index 0a0a09fcb..e17d18a1a 100644 --- a/src/map/map.c +++ b/src/map/map.c @@ -1898,42 +1898,185 @@ int map_quit(struct map_session_data *sd) { return 0; } -/*========================================== - * Lookup, id to session (player,mob,npc,homon,merc..) - *------------------------------------------*/ -struct map_session_data *map_id2sd(int id) { - if (id <= 0) return NULL; - return (struct map_session_data*)idb_get(map->pc_db,id); -} +/** + * Looks up a session data by ID. + * + * The search is performed using the pc_db. + * + * @param id The bl ID to search. + * @return The searched map_session_data, if it exists. + * @retval NULL if the ID is invalid or doesn't belong to a player unit. + */ +struct map_session_data *map_id2sd(int id) +{ + struct block_list *bl = NULL; + if (id <= 0) + return NULL; -struct mob_data *map_id2md(int id) { - if (id <= 0) return NULL; - return (struct mob_data*)idb_get(map->mobid_db,id); + bl = idb_get(map->pc_db,id); + + Assert_retr(NULL, bl->type == BL_PC); + return BL_UCAST(BL_PC, bl); } -struct npc_data *map_id2nd(int id) { +/** + * Looks up a NPC data by ID. + * + * @param id The bl ID to search. + * @return The searched npc_data, if it exists. + * @retval NULL if the ID is invalid or doesn't belong to a NPC. + */ +struct npc_data *map_id2nd(int id) +{ // just a id2bl lookup because there's no npc_db struct block_list* bl = map->id2bl(id); return BL_CAST(BL_NPC, bl); } -struct homun_data *map_id2hd(int id) { +/** + * Looks up a mob data by ID. + * + * The search is performed using the mobid_db. + * + * @param id The bl ID to search. + * @return The searched mob_data, if it exists. + * @retval NULL if the ID is invalid or doesn't belong to a mob unit. + */ +struct mob_data *map_id2md(int id) +{ + struct block_list *bl = NULL; + if (id <= 0) + return NULL; + + bl = idb_get(map->mobid_db,id); + + Assert_retr(NULL, bl->type == BL_MOB); + return BL_UCAST(BL_MOB, bl); +} + +/** + * Looks up a floor item data by ID. + * + * @param id The bl ID to search. + * @return The searched flooritem_data, if it exists. + * @retval NULL if the ID is invalid or doesn't belong to a floor item. + */ +struct flooritem_data *map_id2fi(int id) +{ + struct block_list* bl = map->id2bl(id); + + return BL_CAST(BL_ITEM, bl); +} + +/** + * Looks up a chat data by ID. + * + * @param id The bl ID to search. + * @return The searched chat_data, if it exists. + * @retval NULL if the ID is invalid or doesn't belong to a chat. + */ +struct chat_data *map_id2cd(int id) +{ + struct block_list* bl = map->id2bl(id); + + return BL_CAST(BL_CHAT, bl); +} + +/** + * Looks up a skill unit data by ID. + * + * @param id The bl ID to search. + * @return The searched skill_unit data, if it exists. + * @retval NULL if the ID is invalid or doesn't belong to a skill unit. + */ +struct skill_unit *map_id2su(int id) +{ + struct block_list* bl = map->id2bl(id); + + return BL_CAST(BL_SKILL, bl); +} + +/** + * Looks up a pet data by ID. + * + * @param id The bl ID to search. + * @return The searched pet_data, if it exists. + * @retval NULL if the ID is invalid or doesn't belong to a pet. + */ +struct pet_data *map_id2pd(int id) +{ + struct block_list* bl = map->id2bl(id); + + return BL_CAST(BL_PET, bl); +} + +/** + * Looks up a homunculus data by ID. + * + * @param id The bl ID to search. + * @return The searched homun_data, if it exists. + * @retval NULL if the ID is invalid or doesn't belong to a homunculus. + */ +struct homun_data *map_id2hd(int id) +{ struct block_list* bl = map->id2bl(id); return BL_CAST(BL_HOM, bl); } -struct mercenary_data *map_id2mc(int id) { +/** + * Looks up a mercenary data by ID. + * + * @param id The bl ID to search. + * @return The searched mercenary_data, if it exists. + * @retval NULL if the ID is invalid or doesn't belong to a mercenary. + */ +struct mercenary_data *map_id2mc(int id) +{ struct block_list* bl = map->id2bl(id); return BL_CAST(BL_MER, bl); } -struct chat_data *map_id2cd(int id) { +/** + * Looks up an elemental data by ID. + * + * @param id The bl ID to search. + * @return The searched elemental_data, if it exists. + * @retval NULL if the ID is invalid or doesn't belong to an elemental. + */ +struct elemental_data *map_id2ed(int id) +{ struct block_list* bl = map->id2bl(id); - return BL_CAST(BL_CHAT, bl); + return BL_CAST(BL_ELEM, bl); +} + +/** + * Looks up a block_list by ID. + * + * The search is performed using the id_db. + * + * @param id The bl ID to search. + * @return The searched block_list, if it exists. + * @retval NULL if the ID is invalid. + */ +struct block_list *map_id2bl(int id) +{ + return idb_get(map->id_db, id); +} + +/** + * Verifies whether a block list ID is valid. + * + * @param id The bl ID to search. + * @retval true if the ID exists and is valid. + * @retval false otherwise. + */ +bool map_blid_exists(int id) +{ + return (idb_exists(map->id_db,id)); } /// Returns the nick of the target charid or NULL if unknown (requests the nick to the char server). @@ -2009,20 +2152,6 @@ struct map_session_data * map_nick2sd(const char *nick) return found_sd; } -/*========================================== - * Looksup id_db DBMap and returns BL pointer of 'id' or NULL if not found - *------------------------------------------*/ -struct block_list * map_id2bl(int id) { - return (struct block_list*)idb_get(map->id_db,id); -} - -/** - * Same as map->id2bl except it only checks for its existence - **/ -bool map_blid_exists( int id ) { - return (idb_exists(map->id_db,id)); -} - /*========================================== * Convext Mirror *------------------------------------------*/ @@ -6159,11 +6288,15 @@ void map_defaults(void) { map->foreachininstance = map_foreachininstance; map->id2sd = map_id2sd; - map->id2md = map_id2md; map->id2nd = map_id2nd; + map->id2md = map_id2md; + map->id2fi = map_id2fi; + map->id2cd = map_id2cd; + map->id2su = map_id2su; + map->id2pd = map_id2pd; map->id2hd = map_id2hd; map->id2mc = map_id2mc; - map->id2cd = map_id2cd; + map->id2ed = map_id2ed; map->id2bl = map_id2bl; map->blid_exists = map_blid_exists; map->mapindex2mapid = map_mapindex2mapid; diff --git a/src/map/map.h b/src/map/map.h index 8a32cf361..4c74d352c 100644 --- a/src/map/map.h +++ b/src/map/map.h @@ -1110,13 +1110,17 @@ END_ZEROED_BLOCK; int (*vforeachininstance)(int (*func)(struct block_list*,va_list), int16 instance_id, int type, va_list ap); int (*foreachininstance)(int (*func)(struct block_list*,va_list), int16 instance_id, int type,...); - struct map_session_data * (*id2sd) (int id); - struct mob_data * (*id2md) (int id); - struct npc_data * (*id2nd) (int id); - struct homun_data* (*id2hd) (int id); - struct mercenary_data* (*id2mc) (int id); - struct chat_data* (*id2cd) (int id); - struct block_list * (*id2bl) (int id); + struct map_session_data *(*id2sd) (int id); + struct npc_data *(*id2nd) (int id); + struct mob_data *(*id2md) (int id); + struct flooritem_data *(*id2fi) (int id); + struct chat_data *(*id2cd) (int id); + struct skill_unit *(*id2su) (int id); + struct pet_data *(*id2pd) (int id); + struct homun_data *(*id2hd) (int id); + struct mercenary_data *(*id2mc) (int id); + struct elemental_data *(*id2ed) (int id); + struct block_list *(*id2bl) (int id); bool (*blid_exists) (int id); int16 (*mapindex2mapid) (unsigned short map_index); int16 (*mapname2mapid) (const char* name); diff --git a/src/map/mob.c b/src/map/mob.c index 074dc13bc..338d886ca 100644 --- a/src/map/mob.c +++ b/src/map/mob.c @@ -824,7 +824,7 @@ int mob_linksearch(struct block_list *bl,va_list ap) * mob spawn with delay (timer function) *------------------------------------------*/ int mob_delayspawn(int tid, int64 tick, int id, intptr_t data) { - struct block_list* bl = map->id2bl(id); + struct block_list* bl = map->id2bl(id); // TODO: Why does this not use map->bl2md? struct mob_data* md = BL_CAST(BL_MOB, bl); if( md ) @@ -1880,7 +1880,7 @@ void mob_item_drop(struct mob_data *md, struct item_drop_list *dlist, struct ite } int mob_timer_delete(int tid, int64 tick, int id, intptr_t data) { - struct block_list* bl = map->id2bl(id); + struct block_list* bl = map->id2bl(id); // TODO: Why does this not use map->id2md? struct mob_data* md = BL_CAST(BL_MOB, bl); if( md ) diff --git a/src/map/npc.c b/src/map/npc.c index 56fc8cc8a..11e4da147 100644 --- a/src/map/npc.c +++ b/src/map/npc.c @@ -571,10 +571,11 @@ struct timer_event_data { /*========================================== * triger 'OnTimerXXXX' events *------------------------------------------*/ -int npc_timerevent(int tid, int64 tick, int id, intptr_t data) { +int npc_timerevent(int tid, int64 tick, int id, intptr_t data) +{ int old_rid, old_timer; int64 old_tick; - struct npc_data* nd=(struct npc_data *)map->id2bl(id); + struct npc_data *nd = map->id2nd(id); struct npc_timerevent_list *te; struct timer_event_data *ted = (struct timer_event_data*)data; struct map_session_data *sd=NULL; @@ -741,14 +742,13 @@ void npc_timerevent_quit(struct map_session_data* sd) } // Delete timer - nd = (struct npc_data *)map->id2bl(td->id); + nd = map->id2nd(td->id); ted = (struct timer_event_data*)td->data; timer->delete(sd->npc_timer_id, npc->timerevent); sd->npc_timer_id = INVALID_TIMER; // Execute OnTimerQuit - if( nd && nd->bl.type == BL_NPC ) - { + if (nd != NULL) { char buf[EVENT_NAME_LENGTH]; struct event_data *ev; @@ -1032,15 +1032,14 @@ int npc_touch_areanpc(struct map_session_data* sd, int16 m, int16 x, int16 y) *------------------------------------------*/ int npc_untouch_areanpc(struct map_session_data* sd, int16 m, int16 x, int16 y) { - struct npc_data *nd; + struct npc_data *nd = NULL; nullpo_retr(1, sd); if (!sd->areanpc_id) return 0; - nd = (struct npc_data *) map->id2bl(sd->areanpc_id); - if (!nd) - { + nd = map->id2nd(sd->areanpc_id); + if (nd == NULL) { sd->areanpc_id = 0; return 1; } @@ -1302,7 +1301,7 @@ int npc_scriptcont(struct map_session_data* sd, int id, bool closing) { nullpo_retr(1, sd); if( id != sd->npc_id ){ - struct npc_data *nd_sd = (struct npc_data *)map->id2bl(sd->npc_id); + struct npc_data *nd_sd = map->id2nd(sd->npc_id); struct npc_data *nd = BL_CAST(BL_NPC, target); ShowDebug("npc_scriptcont: %s (sd->npc_id=%d) is not %s (id=%d).\n", nd_sd?(char*)nd_sd->name:"'Unknown NPC'", (int)sd->npc_id, @@ -1402,7 +1401,8 @@ int npc_cashshop_buylist(struct map_session_data *sd, int points, int count, uns if( points < 0 ) return ERROR_TYPE_MONEY; - if( !(nd = (struct npc_data *)map->id2bl(sd->npc_shopid)) ) + nd = map->id2nd(sd->npc_shopid); + if (nd == NULL) return ERROR_TYPE_NPC; if( nd->subtype != CASHSHOP ) { @@ -1725,7 +1725,8 @@ int npc_cashshop_buy(struct map_session_data *sd, int nameid, int amount, int po if( sd->state.trading ) return ERROR_TYPE_EXCHANGE; - if( !(nd = (struct npc_data *)map->id2bl(sd->npc_shopid)) ) + nd = map->id2nd(sd->npc_shopid); + if (nd == NULL) return ERROR_TYPE_NPC; if( (item = itemdb->exists(nameid)) == NULL ) diff --git a/src/map/npc_chat.c b/src/map/npc_chat.c index db300e362..fef3ba99b 100644 --- a/src/map/npc_chat.c +++ b/src/map/npc_chat.c @@ -400,38 +400,46 @@ int npc_chat_sub(struct block_list* bl, va_list ap) } // Various script built-ins used to support these functions -BUILDIN(defpattern) { +BUILDIN(defpattern) +{ int setid = script_getnum(st,2); const char* pattern = script_getstr(st,3); const char* label = script_getstr(st,4); - struct npc_data* nd = (struct npc_data *)map->id2bl(st->oid); + struct npc_data *nd = map->id2nd(st->oid); + nullpo_retr(false, nd); npc_chat->def_pattern(nd, setid, pattern, label); return true; } -BUILDIN(activatepset) { +BUILDIN(activatepset) +{ int setid = script_getnum(st,2); - struct npc_data* nd = (struct npc_data *)map->id2bl(st->oid); + struct npc_data *nd = map->id2nd(st->oid); + nullpo_retr(false, nd); npc_chat->activate_pcreset(nd, setid); return true; } -BUILDIN(deactivatepset) { +BUILDIN(deactivatepset) +{ int setid = script_getnum(st,2); - struct npc_data* nd = (struct npc_data *)map->id2bl(st->oid); + struct npc_data *nd = map->id2nd(st->oid); + nullpo_retr(false, nd); npc_chat->deactivate_pcreset(nd, setid); return true; } -BUILDIN(deletepset) { +BUILDIN(deletepset) +{ int setid = script_getnum(st,2); - struct npc_data* nd = (struct npc_data *)map->id2bl(st->oid); + struct npc_data *nd = map->id2nd(st->oid); + nullpo_retr(false, nd); npc_chat->delete_pcreset(nd, setid); diff --git a/src/map/pc.c b/src/map/pc.c index cf7bc6dcb..d1cd0592c 100644 --- a/src/map/pc.c +++ b/src/map/pc.c @@ -123,10 +123,11 @@ bool pc_should_log_commands(struct map_session_data *sd) return pcg->should_log_commands(sd->group); } -int pc_invincible_timer(int tid, int64 tick, int id, intptr_t data) { - struct map_session_data *sd; +int pc_invincible_timer(int tid, int64 tick, int id, intptr_t data) +{ + struct map_session_data *sd = map->id2sd(id); - if( (sd=(struct map_session_data *)map->id2sd(id)) == NULL || sd->bl.type!=BL_PC ) + if (sd == NULL) return 1; if(sd->invincible_timer != tid){ @@ -162,10 +163,10 @@ void pc_delinvincibletimer(struct map_session_data* sd) } int pc_spiritball_timer(int tid, int64 tick, int id, intptr_t data) { - struct map_session_data *sd; + struct map_session_data *sd = map->id2sd(id); int i; - if( (sd=(struct map_session_data *)map->id2sd(id)) == NULL || sd->bl.type!=BL_PC ) + if (sd == NULL) return 1; if( sd->spiritball <= 0 ) @@ -1861,9 +1862,9 @@ int pc_disguise(struct map_session_data *sd, int class_) { clif->updatestatus(sd,SP_CARTINFO); } if (sd->chatID) { - struct chat_data* cd; + struct chat_data *cd = map->id2cd(sd->chatID); - if( (cd = (struct chat_data*)map->id2bl(sd->chatID)) ) + if (cd != NULL) clif->dispchat(cd,0); } } @@ -10414,10 +10415,10 @@ bool pc_can_use_command(struct map_session_data *sd, const char *command) { */ int pc_charm_timer(int tid, int64 tick, int id, intptr_t data) { - struct map_session_data *sd; + struct map_session_data *sd = map->id2sd(id); int i; - if( (sd=(struct map_session_data *)map->id2sd(id)) == NULL || sd->bl.type!=BL_PC ) + if (sd == NULL) return 1; if (sd->charm_count <= 0) { diff --git a/src/map/script.c b/src/map/script.c index 214cb4914..4466d79f1 100644 --- a/src/map/script.c +++ b/src/map/script.c @@ -10142,18 +10142,20 @@ BUILDIN(initnpctimer) struct script_data *data; data = script_getdata(st,2); script->get_val(st,data); // dereference if it's a variable - if( data_isstring(data) ) //NPC name + if (data_isstring(data)) { + //NPC name nd = npc->name2id(script->conv_str(st, data)); - else if( data_isint(data) ) { + } else if (data_isint(data)) { //Flag - nd = (struct npc_data *)map->id2bl(st->oid); + nd = map->id2nd(st->oid); flag = script->conv_num(st,data); } else { ShowError("initnpctimer: invalid argument type #1 (needs be int or string)).\n"); return false; } - } else - nd = (struct npc_data *)map->id2bl(st->oid); + } else { + nd = map->id2nd(st->oid); + } if( !nd ) return true; @@ -10185,18 +10187,20 @@ BUILDIN(startnpctimer) struct script_data *data; data = script_getdata(st,2); script->get_val(st,data); // dereference if it's a variable - if( data_isstring(data) ) //NPC name + if (data_isstring(data)) { + //NPC name nd = npc->name2id(script->conv_str(st, data)); - else if( data_isint(data) ) { + } else if (data_isint(data)) { //Flag - nd = (struct npc_data *)map->id2bl(st->oid); + nd = map->id2nd(st->oid); flag = script->conv_num(st,data); } else { ShowError("initnpctimer: invalid argument type #1 (needs be int or string)).\n"); return false; } - } else - nd=(struct npc_data *)map->id2bl(st->oid); + } else { + nd = map->id2nd(st->oid); + } if( !nd ) return true; @@ -10225,18 +10229,20 @@ BUILDIN(stopnpctimer) { struct script_data *data; data = script_getdata(st,2); script->get_val(st,data); // Dereference if it's a variable - if( data_isstring(data) ) //NPC name + if (data_isstring(data)) { + //NPC name nd = npc->name2id(script->conv_str(st, data)); - else if( data_isint(data) ) { + } else if (data_isint(data)) { //Flag - nd = (struct npc_data *)map->id2bl(st->oid); + nd = map->id2nd(st->oid); flag = script->conv_num(st,data); } else { ShowError("initnpctimer: invalid argument type #1 (needs be int or string)).\n"); return false; } - } else - nd=(struct npc_data *)map->id2bl(st->oid); + } else { + nd = map->id2nd(st->oid); + } if( !nd ) return true; @@ -10258,10 +10264,9 @@ BUILDIN(getnpctimer) if( script_hasdata(st,3) ) nd = npc->name2id(script_getstr(st,3)); else - nd = (struct npc_data *)map->id2bl(st->oid); + nd = map->id2nd(st->oid); - if( !nd || nd->bl.type != BL_NPC ) - { + if (nd == NULL) { script_pushint(st,0); ShowError("getnpctimer: Invalid NPC.\n"); return false; @@ -10296,9 +10301,9 @@ BUILDIN(setnpctimer) if( script_hasdata(st,3) ) nd = npc->name2id(script_getstr(st,3)); else - nd = (struct npc_data *)map->id2bl(st->oid); + nd = map->id2nd(st->oid); - if( !nd || nd->bl.type != BL_NPC ) { + if (nd == NULL) { script_pushint(st,1); ShowError("setnpctimer: Invalid NPC.\n"); return false; @@ -10315,10 +10320,9 @@ BUILDIN(setnpctimer) BUILDIN(attachnpctimer) { struct map_session_data *sd; - struct npc_data *nd = (struct npc_data *)map->id2bl(st->oid); + struct npc_data *nd = map->id2nd(st->oid); - if( !nd || nd->bl.type != BL_NPC ) - { + if (nd == NULL) { script_pushint(st,1); ShowError("setnpctimer: Invalid NPC.\n"); return false; @@ -10348,10 +10352,9 @@ BUILDIN(detachnpctimer) { if( script_hasdata(st,2) ) nd = npc->name2id(script_getstr(st,2)); else - nd = (struct npc_data *)map->id2bl(st->oid); + nd = map->id2nd(st->oid); - if( !nd || nd->bl.type != BL_NPC ) - { + if (nd == NULL) { script_pushint(st,1); ShowError("detachnpctimer: Invalid NPC.\n"); return false; @@ -10441,8 +10444,8 @@ BUILDIN(itemeffect) if (sd == NULL) return true; - nd = (struct npc_data *)map->id2bl(sd->npc_id); - if( nd == NULL ) + nd = map->id2nd(sd->npc_id); + if (nd == NULL) return false; if( script_isstringtype(st, 2) ) { @@ -11293,9 +11296,8 @@ BUILDIN(changecharsex) /*========================================== * Works like 'announce' but outputs in the common chat window *------------------------------------------*/ -BUILDIN(globalmes) { - struct block_list *bl = map->id2bl(st->oid); - struct npc_data *nd = (struct npc_data *)bl; +BUILDIN(globalmes) +{ const char *name=NULL,*mes; mes=script_getstr(st,2); @@ -11305,7 +11307,9 @@ BUILDIN(globalmes) { // npc name to display name=script_getstr(st,3); } else { - name=nd->name; //use current npc name + const struct npc_data *nd = map->id2nd(st->oid); + nullpo_retr(false, nd); + name = nd->name; //use current npc name } npc->globalmessage(name,mes); // broadcast to all players connected @@ -11331,7 +11335,7 @@ BUILDIN(waitingroom) int minLvl = script_hasdata(st,7) ? script_getnum(st,7) : 1; int maxLvl = script_hasdata(st,8) ? script_getnum(st,8) : MAX_LEVEL; - nd = (struct npc_data *)map->id2bl(st->oid); + nd = map->id2nd(st->oid); if (nd != NULL) { int pub = 1; chat->create_npc_chat(nd, title, limit, pub, trigger, ev, zeny, minLvl, maxLvl); @@ -11349,8 +11353,8 @@ BUILDIN(delwaitingroom) { if( script_hasdata(st,2) ) nd = npc->name2id(script_getstr(st, 2)); else - nd = (struct npc_data *)map->id2bl(st->oid); - if( nd != NULL ) + nd = map->id2nd(st->oid); + if (nd != NULL) chat->delete_npc_chat(nd); return true; } @@ -11366,9 +11370,9 @@ BUILDIN(waitingroomkickall) { if( script_hasdata(st,2) ) nd = npc->name2id(script_getstr(st,2)); else - nd = (struct npc_data *)map->id2bl(st->oid); + nd = map->id2nd(st->oid); - if( nd != NULL && (cd=(struct chat_data *)map->id2bl(nd->chat_id)) != NULL ) + if (nd != NULL && (cd=map->id2cd(nd->chat_id)) != NULL) chat->npc_kick_all(cd); return true; } @@ -11384,9 +11388,9 @@ BUILDIN(enablewaitingroomevent) { if( script_hasdata(st,2) ) nd = npc->name2id(script_getstr(st, 2)); else - nd = (struct npc_data *)map->id2bl(st->oid); + nd = map->id2nd(st->oid); - if( nd != NULL && (cd=(struct chat_data *)map->id2bl(nd->chat_id)) != NULL ) + if (nd != NULL && (cd=map->id2cd(nd->chat_id)) != NULL) chat->enable_event(cd); return true; } @@ -11402,9 +11406,9 @@ BUILDIN(disablewaitingroomevent) { if( script_hasdata(st,2) ) nd = npc->name2id(script_getstr(st, 2)); else - nd = (struct npc_data *)map->id2bl(st->oid); + nd = map->id2nd(st->oid); - if( nd != NULL && (cd=(struct chat_data *)map->id2bl(nd->chat_id)) != NULL ) + if (nd != NULL && (cd=map->id2cd(nd->chat_id)) != NULL) chat->disable_event(cd); return true; } @@ -11427,9 +11431,10 @@ BUILDIN(disablewaitingroomevent) { /// /// getwaitingroomstate(,"") -> /// getwaitingroomstate() -> -BUILDIN(getwaitingroomstate) { - struct npc_data *nd; - struct chat_data *cd; +BUILDIN(getwaitingroomstate) +{ + const struct npc_data *nd; + const struct chat_data *cd; int type; int i; @@ -11437,9 +11442,9 @@ BUILDIN(getwaitingroomstate) { if( script_hasdata(st,3) ) nd = npc->name2id(script_getstr(st, 3)); else - nd = (struct npc_data *)map->id2bl(st->oid); + nd = map->id2nd(st->oid); - if( nd == NULL || (cd=(struct chat_data *)map->id2bl(nd->chat_id)) == NULL ) { + if (nd == NULL || (cd=map->id2cd(nd->chat_id)) == NULL) { script_pushint(st, -1); return true; } @@ -11488,8 +11493,8 @@ BUILDIN(warpwaitingpc) struct npc_data* nd; struct chat_data* cd; - nd = (struct npc_data *)map->id2bl(st->oid); - if( nd == NULL || (cd=(struct chat_data *)map->id2bl(nd->chat_id)) == NULL ) + nd = map->id2nd(st->oid); + if (nd == NULL || (cd=map->id2cd(nd->chat_id)) == NULL) return true; map_name = script_getstr(st,2); @@ -12149,7 +12154,7 @@ BUILDIN(flagemblem) if(g_id < 0) return true; - nd = (struct npc_data *)map->id2nd(st->oid); + nd = map->id2nd(st->oid); if( nd == NULL ) { ShowError("script:flagemblem: npc %d not found\n", st->oid); } else if( nd->subtype != SCRIPT ) { @@ -13887,12 +13892,11 @@ BUILDIN(npctalk) if (script_hasdata(st, 3)) { nd = npc->name2id(script_getstr(st, 3)); - } - else { - nd = (struct npc_data *)map->id2bl(st->oid); + } else { + nd = map->id2nd(st->oid); } - if (nd) { + if (nd != NULL) { char name[NAME_LENGTH], message[256]; safestrncpy(name, nd->name, sizeof(name)); strtok(name, "#"); // discard extra name identifier if present @@ -13909,9 +13913,9 @@ BUILDIN(npcspeed) { int speed; speed = script_getnum(st,2); - nd = (struct npc_data *)map->id2bl(st->oid); + nd = map->id2nd(st->oid); - if( nd ) { + if (nd != NULL) { unit->bl2ud2(&nd->bl); // ensure nd->ud is safe to edit nd->speed = speed; nd->ud->state.speed_changed = 1; @@ -13920,14 +13924,15 @@ BUILDIN(npcspeed) { return true; } // make an npc walk to a position [Valaris] -BUILDIN(npcwalkto) { - struct npc_data *nd=(struct npc_data *)map->id2bl(st->oid); +BUILDIN(npcwalkto) +{ + struct npc_data *nd = map->id2nd(st->oid); int x=0,y=0; x=script_getnum(st,2); y=script_getnum(st,3); - if( nd ) { + if (nd != NULL) { unit->bl2ud2(&nd->bl); // ensure nd->ud is safe to edit if (!nd->status.hp) { status_calc_npc(nd, SCO_FIRST); @@ -13940,10 +13945,11 @@ BUILDIN(npcwalkto) { return true; } // stop an npc's movement [Valaris] -BUILDIN(npcstop) { - struct npc_data *nd = (struct npc_data *)map->id2bl(st->oid); +BUILDIN(npcstop) +{ + struct npc_data *nd = map->id2nd(st->oid); - if( nd ) { + if (nd != NULL) { unit->bl2ud2(&nd->bl); // ensure nd->ud is safe to edit unit->stop_walking(&nd->bl, STOPWALKING_FLAG_FIXPOS|STOPWALKING_FLAG_NEXTCELL); } @@ -13952,9 +13958,10 @@ BUILDIN(npcstop) { } // set click npc distance [4144] -BUILDIN(setnpcdistance) { - struct npc_data *nd = (struct npc_data *) map->id2bl (st->oid); - if (!nd) +BUILDIN(setnpcdistance) +{ + struct npc_data *nd = map->id2nd(st->oid); + if (nd == NULL) return false; nd->area_size = script_getnum(st, 2); @@ -13965,23 +13972,20 @@ BUILDIN(setnpcdistance) { // return current npc direction [4144] BUILDIN(getnpcdir) { - struct npc_data *nd = 0; + const struct npc_data *nd = NULL; - if (script_hasdata(st, 2)) - { - nd = npc->name2id (script_getstr(st, 2)); + if (script_hasdata(st, 2)) { + nd = npc->name2id(script_getstr(st, 2)); } - if (!nd && !st->oid) - { + if (nd == NULL && !st->oid) { script_pushint(st, -1); return true; } - if (!nd) - nd = (struct npc_data *) map->id2bl (st->oid); + if (nd == NULL) + nd = map->id2nd(st->oid); - if (!nd) - { + if (nd == NULL) { script_pushint(st, -1); return true; } @@ -13995,22 +13999,19 @@ BUILDIN(getnpcdir) BUILDIN(setnpcdir) { int newdir; - struct npc_data *nd = 0; + struct npc_data *nd = NULL; - if (script_hasdata(st, 3)) - { - nd = npc->name2id (script_getstr(st, 2)); + if (script_hasdata(st, 3)) { + nd = npc->name2id(script_getstr(st, 2)); newdir = script_getnum(st, 3); - } - else if (script_hasdata(st, 2)) - { + } else if (script_hasdata(st, 2)) { if (!st->oid) return false; - nd = (struct npc_data *) map->id2bl (st->oid); + nd = map->id2nd(st->oid); newdir = script_getnum(st, 2); } - if (!nd) + if (nd == NULL) return false; if (newdir < 0) @@ -14031,23 +14032,20 @@ BUILDIN(setnpcdir) // return npc class [4144] BUILDIN(getnpcclass) { - struct npc_data *nd = 0; + const struct npc_data *nd = NULL; - if (script_hasdata(st, 2)) - { - nd = npc->name2id (script_getstr(st, 2)); + if (script_hasdata(st, 2)) { + nd = npc->name2id(script_getstr(st, 2)); } - if (!nd && !st->oid) - { + if (nd == NULL && !st->oid) { script_pushint(st, -1); return false; } - if (!nd) - nd = (struct npc_data *) map->id2bl(st->oid); + if (nd == NULL) + nd = map->id2nd(st->oid); - if (!nd) - { + if (nd == NULL) { script_pushint(st, -1); return false; } @@ -16108,7 +16106,7 @@ BUILDIN(npcshopattach) { } if (flag) - nd->master_nd = ((struct npc_data *)map->id2bl(st->oid)); + nd->master_nd = map->id2nd(st->oid); else nd->master_nd = NULL; @@ -17486,9 +17484,9 @@ BUILDIN(waitingroom2bg) { if( script_hasdata(st,7) ) nd = npc->name2id(script_getstr(st,7)); else - nd = (struct npc_data *)map->id2bl(st->oid); + nd = map->id2nd(st->oid); - if( nd == NULL || (cd = (struct chat_data *)map->id2bl(nd->chat_id)) == NULL ) { + if (nd == NULL || (cd = map->id2cd(nd->chat_id)) == NULL) { script_pushint(st,0); return true; } @@ -17547,7 +17545,7 @@ BUILDIN(waitingroom2bg_single) { y = script_getnum(st,5); nd = npc->name2id(script_getstr(st,6)); - if( nd == NULL || (cd = (struct chat_data *)map->id2bl(nd->chat_id)) == NULL || cd->users <= 0 ) + if (nd == NULL || (cd = map->id2cd(nd->chat_id)) == NULL || cd->users <= 0) return true; if( (sd = cd->usersd[0]) == NULL ) @@ -18889,7 +18887,7 @@ BUILDIN(npcskill) if (sd == NULL) return true; - nd = (struct npc_data *)map->id2bl(sd->npc_id); + nd = map->id2nd(sd->npc_id); if (stat_point > battle_config.max_third_parameter) { ShowError("npcskill: stat point exceeded maximum of %d.\n",battle_config.max_third_parameter ); diff --git a/src/map/skill.c b/src/map/skill.c index 1656ddeb0..1de4baff6 100644 --- a/src/map/skill.c +++ b/src/map/skill.c @@ -18097,11 +18097,14 @@ int skill_blockpc_start_(struct map_session_data *sd, uint16 skill_id, int tick) return 0; } -int skill_blockhomun_end(int tid, int64 tick, int id, intptr_t data) { // [orn] - struct homun_data *hd = (struct homun_data *)map->id2bl(id); +// [orn] +int skill_blockhomun_end(int tid, int64 tick, int id, intptr_t data) +{ + struct homun_data *hd = map->id2hd(id); if (data <= 0 || data >= MAX_SKILL) return 0; - if (hd) hd->blockskill[data] = 0; + if (hd != NULL) + hd->blockskill[data] = 0; return 1; } @@ -18121,11 +18124,14 @@ int skill_blockhomun_start(struct homun_data *hd, uint16 skill_id, int tick) { / return timer->add(timer->gettick() + tick, skill->blockhomun_end, hd->bl.id, idx); } -int skill_blockmerc_end(int tid, int64 tick, int id, intptr_t data) {// [orn] - struct mercenary_data *md = (struct mercenary_data *)map->id2bl(id); - if( data <= 0 || data >= MAX_SKILL ) +// [orn] +int skill_blockmerc_end(int tid, int64 tick, int id, intptr_t data) +{ + struct mercenary_data *md = map->id2mc(id); + if (data <= 0 || data >= MAX_SKILL) return 0; - if( md ) md->blockskill[data] = 0; + if (md != NULL) + md->blockskill[data] = 0; return 1; } -- cgit v1.2.3-60-g2f50