From e9914cef348c2388a2f9d8b02c9d8a8ff7a75ab5 Mon Sep 17 00:00:00 2001 From: Kenpachi Developer Date: Fri, 3 Jan 2020 11:41:40 +0100 Subject: Applied code style and some minor improvements. * Added new message 1517 (Script could not be unloaded.) * Applied code style to several functions I touched. * Added some minor improments to the function where I applied code style. --- conf/messages.conf | 1 + src/map/atcommand.c | 118 +++++++++++++++++++++++++++------------------------- src/map/mob.c | 44 +++++++++++--------- src/map/npc.c | 105 +++++++++++++++++++++++++--------------------- 4 files changed, 145 insertions(+), 123 deletions(-) diff --git a/conf/messages.conf b/conf/messages.conf index 93c3d4648..e457ed882 100644 --- a/conf/messages.conf +++ b/conf/messages.conf @@ -1638,6 +1638,7 @@ // @reloadnpc 1516: Usage: @reloadnpc {} +1517: Script could not be unloaded. //Custom translations import: conf/import/msg_conf.txt diff --git a/src/map/atcommand.c b/src/map/atcommand.c index 7bc10edf0..3cb10b5ad 100644 --- a/src/map/atcommand.c +++ b/src/map/atcommand.c @@ -4485,26 +4485,27 @@ ACMD(loadnpc) ACMD(unloadnpc) { - struct npc_data *nd; - char NPCname[NAME_LENGTH+1]; - short flag = 1; + char npc_name[NAME_LENGTH + 1]; + int flag = 1; - memset(NPCname, '\0', sizeof(NPCname)); + memset(npc_name, '\0', sizeof(npc_name)); - if (!*message || sscanf(message, "%24s %5hd", NPCname, &flag) < 1) { - clif->message(fd, msg_fd(fd, 1133)); // Please enter a NPC name (Usage: @unloadnpc {}). + if (*message == '\0' || sscanf(message, "%24s %5d", npc_name, &flag) < 1) { + clif->message(fd, msg_fd(fd, 1133)); /// Please enter a NPC name (Usage: @unloadnpc {}). return false; } - - if ((nd = npc->name2id(NPCname)) == NULL) { - clif->message(fd, msg_fd(fd,111)); // This NPC doesn't exist. + + struct npc_data *nd = npc->name2id(npc_name); + + if (nd == NULL) { + clif->message(fd, msg_fd(fd, 111)); /// This NPC doesn't exist. return false; } npc->unload_duplicates(nd, (flag != 0)); npc->unload(nd, true, (flag != 0)); npc->read_event_script(); - clif->message(fd, msg_fd(fd,112)); // Npc Disabled. + clif->message(fd, msg_fd(fd, 112)); /// Npc Disabled. return true; } @@ -4514,33 +4515,32 @@ ACMD(unloadnpc) ACMD(reloadnpc) { char file_path[100]; - short flag = 1; + int flag = 1; - if (!*message || (sscanf(message, "%99s %5hd", file_path, &flag) < 1)) { - clif->message(fd, msg_fd(fd, 1516)); // Usage: @reloadnpc {} + if (*message == '\0' || (sscanf(message, "%99s %5d", file_path, &flag) < 1)) { + clif->message(fd, msg_fd(fd, 1516)); /// Usage: @reloadnpc {} return false; - } else if (npc->unloadfile(file_path, (flag != 0)) == true) { - clif->message(fd, msg_fd(fd, 1386)); // File unloaded. Be aware that... + } - FILE *fp = fopen(file_path, "r"); - // check if script file exists - if (fp == NULL) { - clif->message(fd, msg_fd(fd, 261)); - return false; - } - fclose(fp); + FILE *fp = fopen(file_path, "r"); - // add to list of script sources and run it - npc->addsrcfile(file_path); - npc->parsesrcfile(file_path, true); - npc->read_event_script(); + if (fp == NULL) { + clif->message(fd, msg_fd(fd, 1387)); /// File not found. + return false; + } - clif->message(fd, msg_fd(fd, 262)); - } else { - clif->message(fd, msg_fd(fd, 1387)); // File not found. + fclose(fp); + + if (!npc->unloadfile(file_path, (flag != 0))) { + clif->message(fd, msg_fd(fd, 1517)); /// Script could not be unloaded. return false; } + clif->message(fd, msg_fd(fd, 1386)); /// File unloaded. Be aware that... + npc->addsrcfile(file_path); + npc->parsesrcfile(file_path, true); + npc->read_event_script(); + clif->message(fd, msg_fd(fd, 262)); /// Script loaded. return true; } @@ -6584,44 +6584,41 @@ ACMD(reset) ACMD(summon) { char name[NAME_LENGTH]; - int mob_id = 0; int duration = 0; - struct mob_data *md; - int64 tick=timer->gettick(); - if (!*message || sscanf(message, "%23s %12d", name, &duration) < 1) + if (*message == '\0' || sscanf(message, "%23s %12d", name, &duration) < 1) { - clif->message(fd, msg_fd(fd,1225)); // Please enter a monster name (usage: @summon {duration}). + clif->message(fd, msg_fd(fd, 1225)); /// Please enter a monster name (usage: @summon {duration}). return false; } - if (duration < 1) - duration =1; - else if (duration > 60) - duration =60; + int mob_id = atoi(name); - if ((mob_id = atoi(name)) == 0) + if (mob_id == 0) mob_id = mob->db_searchname(name); - if(mob_id == 0 || mob->db_checkid(mob_id) == 0) + + if (mob_id == 0 || mob->db_checkid(mob_id) == 0) { - clif->message(fd, msg_fd(fd,40)); // Invalid monster ID or name. + clif->message(fd, msg_fd(fd, 40)); /// Invalid monster ID or name. return false; } - md = mob->once_spawn_sub(&sd->bl, sd->bl.m, -1, -1, DEFAULT_MOB_JNAME, mob_id, "", SZ_SMALL, AI_NONE, 0); + struct mob_data *md = mob->once_spawn_sub(&sd->bl, sd->bl.m, -1, -1, DEFAULT_MOB_JNAME, mob_id, "", SZ_SMALL, AI_NONE, 0); - if(!md) + if (md == NULL) return false; md->master_id = sd->bl.id; md->special_state.ai = AI_ATTACK; - md->deletetimer = timer->add(tick+(duration*60000),mob->timer_delete,md->bl.id,0); - clif->specialeffect(&md->bl,344,AREA); - mob->spawn(md); - sc_start4(NULL,&md->bl, SC_MODECHANGE, 100, 1, 0, MD_AGGRESSIVE, 0, 60000); - clif->skill_poseffect(&sd->bl,AM_CALLHOMUN,1,md->bl.x,md->bl.y,tick); - clif->message(fd, msg_fd(fd,39)); // All monster summoned! + int64 tick = timer->gettick(); + + md->deletetimer = timer->add(tick + cap_value(duration, 1, 60) * 60000, mob->timer_delete, md->bl.id, 0); + clif->specialeffect(&md->bl, 344, AREA); + mob->spawn(md); + sc_start4(NULL, &md->bl, SC_MODECHANGE, 100, 1, 0, MD_AGGRESSIVE, 0, 60000); + clif->skill_poseffect(&sd->bl, AM_CALLHOMUN, 1, md->bl.x, md->bl.y, tick); + clif->message(fd, msg_fd(fd, 39)); /// All monster summoned! return true; } @@ -9017,19 +9014,28 @@ ACMD(addperm) ACMD(unloadnpcfile) { char file_path[100]; - short flag = 1; + int flag = 1; - if (!*message || (sscanf(message, "%99s %5hd", file_path, &flag) < 1)) { - clif->message(fd, msg_fd(fd, 1385)); // Usage: @unloadnpcfile {} + if (*message == '\0' || (sscanf(message, "%99s %5d", file_path, &flag) < 1)) { + clif->message(fd, msg_fd(fd, 1385)); /// Usage: @unloadnpcfile {} return false; } - if (npc->unloadfile(file_path, (flag != 0))) { - clif->message(fd, msg_fd(fd, 1386)); // File unloaded. Be aware that... - } else { - clif->message(fd, msg_fd(fd,1387)); // File not found. + FILE *fp = fopen(file_path, "r"); + + if (fp == NULL) { + clif->message(fd, msg_fd(fd, 1387)); /// File not found. return false; } + + fclose(fp); + + if (!npc->unloadfile(file_path, (flag != 0))) { + clif->message(fd, msg_fd(fd, 1517)); /// Script could not be unloaded. + return false; + } + + clif->message(fd, msg_fd(fd, 1386)); /// File unloaded. Be aware that... return true; } diff --git a/src/map/mob.c b/src/map/mob.c index 8495dfa06..d65108a62 100644 --- a/src/map/mob.c +++ b/src/map/mob.c @@ -342,10 +342,12 @@ static int mob_parse_dataset(struct spawn_data *data) *------------------------------------------*/ static struct mob_data *mob_spawn_dataset(struct spawn_data *data, int npc_id) { - struct mob_data *md = NULL; nullpo_retr(NULL, data); - CREATE(md, struct mob_data, 1); - md->bl.id= npc->get_new_npc_id(); + + struct mob_data *md = (struct mob_data *)aCalloc(1, sizeof(struct mob_data)); + + memcpy(md->name, data->name, NAME_LENGTH); + md->bl.id = npc->get_new_npc_id(); md->bl.type = BL_MOB; md->bl.m = data->m; md->bl.x = data->x; @@ -353,25 +355,29 @@ static struct mob_data *mob_spawn_dataset(struct spawn_data *data, int npc_id) md->class_ = data->class_; md->state.boss = data->state.boss; md->db = mob->db(md->class_); + md->npc_id = npc_id; + md->spawn_timer = INVALID_TIMER; + md->deletetimer = INVALID_TIMER; + md->skill_idx = -1; + if (data->level > 0 && data->level <= MAX_LEVEL) md->level = data->level; - memcpy(md->name, data->name, NAME_LENGTH); - if (data->state.ai) + + if (data->state.ai > 0) md->special_state.ai = data->state.ai; - if (data->state.size) + + if (data->state.size > 0) md->special_state.size = data->state.size; - if (data->eventname[0] && strlen(data->eventname) >= 4) + + if (data->eventname[0] != '\0' && strlen(data->eventname) >= 4) memcpy(md->npc_event, data->eventname, 50); - if(md->db->status.mode&MD_LOOTER) - md->lootitem = (struct item *)aCalloc(LOOTITEM_SIZE,sizeof(struct item)); - md->npc_id = npc_id; - md->spawn_timer = INVALID_TIMER; - md->deletetimer = INVALID_TIMER; - md->skill_idx = -1; + + if ((md->db->status.mode & MD_LOOTER) == MD_LOOTER) + md->lootitem = (struct item *)aCalloc(LOOTITEM_SIZE, sizeof(struct item)); + status->set_viewdata(&md->bl, md->class_); status->change_init(&md->bl); unit->dataset(&md->bl); - map->addiddb(&md->bl); return md; } @@ -522,21 +528,21 @@ static struct mob_data *mob_once_spawn_sub(struct block_list *bl, int16 m, int16 else strcpy(data.name, DEFAULT_MOB_JNAME); - if (event) + if (event != NULL) safestrncpy(data.eventname, event, sizeof(data.eventname)); - // Locate spot next to player. - if (bl && (x < 0 || y < 0)) + /** Locate spot next to player. */ + if (bl != NULL && (x < 0 || y < 0)) map->search_freecell(bl, m, &x, &y, 1, 1, 0); - // if none found, pick random position on map + /** If none found, pick random position on map. */ if (x <= 0 || x >= map->list[m].xs || y <= 0 || y >= map->list[m].ys) map->search_freecell(NULL, m, &x, &y, -1, -1, 1); data.x = x; data.y = y; - if (!mob->parse_dataset(&data)) + if (mob->parse_dataset(&data) == 0) return NULL; return mob->spawn_dataset(&data, npc_id); diff --git a/src/map/npc.c b/src/map/npc.c index 8aed1d2fc..a1e3a2ca3 100644 --- a/src/map/npc.c +++ b/src/map/npc.c @@ -2940,6 +2940,7 @@ static int npc_unload_ev_label(union DBKey key, struct DBData *data, va_list ap) static int npc_unload_dup_sub(struct npc_data *nd, va_list args) { nullpo_ret(nd); + int src_id = va_arg(args, int); int unload_mobs = va_arg(args, int); @@ -2953,14 +2954,15 @@ static int npc_unload_dup_sub(struct npc_data *nd, va_list args) static void npc_unload_duplicates(struct npc_data *nd, bool unload_mobs) { nullpo_retv(nd); - // passing unload_mobs as int, because va_arg() would promote bool to int and cause compiler warnings - map->foreachnpc(npc->unload_dup_sub, nd->bl.id, unload_mobs ? 1 : 0); + + map->foreachnpc(npc->unload_dup_sub, nd->bl.id, unload_mobs); } //Removes mobs spawned by NPC (monster/areamonster/guardian/bg_monster/atcommand("@monster xy")) static int npc_unload_mob(struct mob_data *md, va_list args) { nullpo_ret(md); + int npc_id = va_arg(args, int); if (md->npc_id == npc_id) { @@ -2978,98 +2980,109 @@ static int npc_unload(struct npc_data *nd, bool single, bool unload_mobs) { nullpo_ret(nd); - if( nd->ud && nd->ud != &npc->base_ud ) { + if (nd->ud != NULL && nd->ud != &npc->base_ud) skill->clear_unitgroup(&nd->bl); - } npc->remove_map(nd); map->deliddb(&nd->bl); - if( single ) + + if (single) strdb_remove(npc->name_db, nd->exname); - if (nd->chat_id) // remove npc chatroom object and kick users + if (nd->chat_id != 0) /// Remove NPC chatroom object and kick users. chat->delete_npc_chat(nd); - npc_chat->finalize(nd); // deallocate npc PCRE data structures + npc_chat->finalize(nd); /// Deallocate NPC PCRE data structures. if (single && nd->path != NULL) { npc->releasepathreference(nd->path); nd->path = NULL; } - if (single && nd->bl.m != -1) + if (single && nd->bl.m != INDEX_NOT_FOUND) map->remove_questinfo(nd->bl.m, nd); + npc->questinfo_clear(nd); - if (nd->src_id == 0 && ( nd->subtype == SHOP || nd->subtype == CASHSHOP)) { - //src check for duplicate shops [Orcao] - aFree(nd->u.shop.shop_item); + if (nd->src_id == 0 && (nd->subtype == SHOP || nd->subtype == CASHSHOP)) { + aFree(nd->u.shop.shop_item); /// src check for duplicate shops. [Orcao] } else if (nd->subtype == SCRIPT) { - struct s_mapiterator *iter; - struct map_session_data *sd = NULL; - char evname[EVENT_NAME_LENGTH]; - struct event_data *ev; + snprintf(evname, ARRAYLENGTH(evname), "%s::OnNPCUnload", nd->exname); - if ((ev = (struct event_data*)strdb_get(npc->ev_db, evname)) != NULL) - script->run_npc(nd->u.scr.script, ev->pos, 0, nd->bl.id); // Run OnNPCUnload - if( single ) { - npc->ev_db->foreach(npc->ev_db,npc->unload_ev,nd->exname); //Clean up all events related - npc->ev_label_db->foreach(npc->ev_label_db,npc->unload_ev_label,nd); + struct event_data *ev = strdb_get(npc->ev_db, evname); + + if (ev != NULL) + script->run_npc(nd->u.scr.script, ev->pos, 0, nd->bl.id); /// Run OnNPCUnload. + + if (single) { + npc->ev_db->foreach(npc->ev_db, npc->unload_ev, nd->exname); /// Clean up all related events. + npc->ev_label_db->foreach(npc->ev_label_db, npc->unload_ev_label, nd); } - iter = mapit_geteachpc(); - for (sd = BL_UCAST(BL_PC, mapit->first(iter)); mapit->exists(iter); sd = BL_UCAST(BL_PC, mapit->next(iter))) { - if (sd->npc_timer_id != INVALID_TIMER ) { + struct s_mapiterator *iter = mapit_geteachpc(); + struct map_session_data *sd = BL_UCAST(BL_PC, mapit->first(iter)); + + for (; mapit->exists(iter); sd = BL_UCAST(BL_PC, mapit->next(iter))) { + if (sd->npc_timer_id != INVALID_TIMER) { const struct TimerData *td = timer->get(sd->npc_timer_id); - if( td && td->id != nd->bl.id ) + if (td != NULL && td->id != nd->bl.id) continue; - if( td && td->data ) + if (td != NULL && td->data != 0) ers_free(npc->timer_event_ers, (void*)td->data); + timer->delete(sd->npc_timer_id, npc->timerevent); sd->npc_timer_id = INVALID_TIMER; } } + mapit->free(iter); if (nd->u.scr.timerid != INVALID_TIMER) { - const struct TimerData *td; - td = timer->get(nd->u.scr.timerid); - if (td && td->data) + const struct TimerData *td = timer->get(nd->u.scr.timerid); + + if (td != NULL && td->data != 0) ers_free(npc->timer_event_ers, (void*)td->data); + timer->delete(nd->u.scr.timerid, npc->timerevent); } - if (nd->u.scr.timer_event) + + if (nd->u.scr.timer_event != NULL) aFree(nd->u.scr.timer_event); + if (nd->src_id == 0) { - if(nd->u.scr.script) { + if (nd->u.scr.script != NULL) { script->free_code(nd->u.scr.script); nd->u.scr.script = NULL; } - if (nd->u.scr.label_list) { + + if (nd->u.scr.label_list != NULL) { aFree(nd->u.scr.label_list); nd->u.scr.label_list = NULL; nd->u.scr.label_list_num = 0; } - if(nd->u.scr.shop) { - if(nd->u.scr.shop->item) { + + if (nd->u.scr.shop != NULL) { + if (nd->u.scr.shop->item != NULL) { for (int i = 0; i < nd->u.scr.shop->items; i ++) { - if (nd->u.scr.shop->item[i].currency) + if (nd->u.scr.shop->item[i].currency != NULL) aFree(nd->u.scr.shop->item[i].currency); } aFree(nd->u.scr.shop->item); } + aFree(nd->u.scr.shop); } } - if( nd->u.scr.guild_id ) + + if (nd->u.scr.guild_id > 0) guild->flag_remove(nd); } - if( nd->ud && nd->ud != &npc->base_ud ) { + if (nd->ud != NULL && nd->ud != &npc->base_ud) { aFree(nd->ud); nd->ud = NULL; } @@ -3078,9 +3091,7 @@ static int npc_unload(struct npc_data *nd, bool single, bool unload_mobs) map->foreachmob(npc->unload_mob, nd->bl.id); HPM->data_store_destroy(&nd->hdata); - aFree(nd); - return 0; } @@ -4464,10 +4475,9 @@ static const char *npc_parse_function(const char *w1, const char *w2, const char *------------------------------------------*/ static void npc_parse_mob2(struct spawn_data *mobspawn) { - int i; - nullpo_retv(mobspawn); - for( i = mobspawn->active; i < mobspawn->num; ++i ) { + + for (int i = mobspawn->active; i < mobspawn->num; ++i) { struct mob_data *md = mob->spawn_dataset(mobspawn, 0); md->spawn = mobspawn; md->spawn->active++; @@ -5631,23 +5641,22 @@ static int npc_reload(void) //Unload all npc in the given file static bool npc_unloadfile(const char *filepath, bool unload_mobs) { + nullpo_retr(false, filepath); + struct DBIterator *iter = db_iterator(npc->name_db); - struct npc_data* nd = NULL; bool found = false; - nullpo_retr(false, filepath); - - for( nd = dbi_first(iter); dbi_exists(iter); nd = dbi_next(iter) ) { - if( nd->path && strcasecmp(nd->path,filepath) == 0 ) { // FIXME: This can break in case-sensitive file systems + for (struct npc_data *nd = dbi_first(iter); dbi_exists(iter); nd = dbi_next(iter)) { + if (nd->path != NULL && strcasecmp(nd->path, filepath) == 0) { // FIXME: This can break in case-sensitive file systems. found = true; - npc->unload_duplicates(nd, unload_mobs);/* unload any npcs which could duplicate this but be in a different file */ + npc->unload_duplicates(nd, unload_mobs); /// Unload any NPC which could duplicate this but be in a different file. npc->unload(nd, true, unload_mobs); } } dbi_destroy(iter); - if( found ) /* refresh event cache */ + if (found) /// Refresh event cache. npc->read_event_script(); return found; -- cgit v1.2.3-60-g2f50