From 593ac627574c71890bb68859a805a44b48d0510b Mon Sep 17 00:00:00 2001 From: Jey Date: Sun, 1 Oct 2017 18:25:16 +0200 Subject: [PATCH 1/2] Convert mob.c to mob.cpp. --- src/map/map-server.vcxproj | 4 +++- src/map/map-server.vcxproj.filters | 2 +- src/map/{mob.c => mob.cpp} | 38 +++++++++++++++++++----------- src/map/mob.h | 6 ++--- src/map/script.cpp | 8 +++---- 5 files changed, 35 insertions(+), 23 deletions(-) rename src/map/{mob.c => mob.cpp} (99%) diff --git a/src/map/map-server.vcxproj b/src/map/map-server.vcxproj index 061514db99..816f7ee27f 100644 --- a/src/map/map-server.vcxproj +++ b/src/map/map-server.vcxproj @@ -233,7 +233,9 @@ - + + CompileAsCpp + diff --git a/src/map/map-server.vcxproj.filters b/src/map/map-server.vcxproj.filters index ac66e5acb0..eef7d1bfbc 100644 --- a/src/map/map-server.vcxproj.filters +++ b/src/map/map-server.vcxproj.filters @@ -217,7 +217,7 @@ Source Files - + Source Files diff --git a/src/map/mob.c b/src/map/mob.cpp similarity index 99% rename from src/map/mob.c rename to src/map/mob.cpp index 1882d4f02e..3568b3c1bc 100644 --- a/src/map/mob.c +++ b/src/map/mob.cpp @@ -29,6 +29,10 @@ #include #include +#ifdef __cplusplus +extern "C" { +#endif + #define ACTIVE_AI_RANGE 2 //Distance added on top of 'AREA_SIZE' at which mobs enter active AI mode. #define IDLE_SKILL_INTERVAL 10 //Active idle skills should be triggered every 1 second (1000/MIN_MOBTHINKTIME) @@ -580,7 +584,7 @@ bool mob_ksprotected (struct block_list *src, struct block_list *target) return false; } -struct mob_data *mob_once_spawn_sub(struct block_list *bl, int16 m, int16 x, int16 y, const char *mobname, int mob_id, const char *event, unsigned int size, unsigned int ai) +struct mob_data *mob_once_spawn_sub(struct block_list *bl, int16 m, int16 x, int16 y, const char *mobname, int mob_id, const char *event, unsigned int size, enum mob_ai ai) { struct spawn_data data; @@ -622,7 +626,7 @@ struct mob_data *mob_once_spawn_sub(struct block_list *bl, int16 m, int16 x, int /*========================================== * Spawn a single mob on the specified coordinates. *------------------------------------------*/ -int mob_once_spawn(struct map_session_data* sd, int16 m, int16 x, int16 y, const char* mobname, int mob_id, int amount, const char* event, unsigned int size, unsigned int ai) +int mob_once_spawn(struct map_session_data* sd, int16 m, int16 x, int16 y, const char* mobname, int mob_id, int amount, const char* event, unsigned int size, enum mob_ai ai) { struct mob_data* md = NULL; int count, lv; @@ -674,7 +678,7 @@ int mob_once_spawn(struct map_session_data* sd, int16 m, int16 x, int16 y, const /*========================================== * Spawn mobs in the specified area. *------------------------------------------*/ -int mob_once_spawn_area(struct map_session_data* sd, int16 m, int16 x0, int16 y0, int16 x1, int16 y1, const char* mobname, int mob_id, int amount, const char* event, unsigned int size, unsigned int ai) +int mob_once_spawn_area(struct map_session_data* sd, int16 m, int16 x0, int16 y0, int16 x1, int16 y1, const char* mobname, int mob_id, int amount, const char* event, unsigned int size, enum mob_ai ai) { int i, max, id = 0; int lx = -1, ly = -1; @@ -1228,7 +1232,7 @@ static int mob_ai_sub_hard_activesearch(struct block_list *bl,va_list ap) nullpo_ret(bl); md=va_arg(ap,struct mob_data *); target= va_arg(ap,struct block_list**); - mode= va_arg(ap,enum e_mode); + mode= static_cast(va_arg(ap, int)); //If can't seek yet, not an enemy, or you can't attack it, skip. if ((*target) == bl || !status_check_skilluse(&md->bl, bl, 0, 0)) @@ -3764,7 +3768,7 @@ int mob_clone_spawn(struct map_session_data *sd, int16 m, int16 x, int16 y, cons if (mode) //User provided mode. status->mode = mode; else if (flag&1) //Friendly Character, remove looting. - status->mode &= ~MD_LOOTER; + status->mode = (enum e_mode)(status->mode&(~MD_LOOTER)); status->hp = status->max_hp; status->sp = status->max_sp; memcpy(&db->vd, &sd->vd, sizeof(struct view_data)); @@ -4125,9 +4129,9 @@ static bool mob_parse_dbrow(char** str) return false; } - status->mode = (int)strtol(str[25], NULL, 0); + status->mode = (enum e_mode)strtol(str[25], NULL, 0); if (!battle_config.monster_active_enable) - status->mode &= ~MD_AGGRESSIVE; + status->mode = (enum e_mode)(status->mode&(~MD_AGGRESSIVE)); if (status_has_mode(status,MD_STATUS_IMMUNE|MD_KNOCKBACK_IMMUNE|MD_DETECTOR)) status->class_ = CLASS_BOSS; @@ -4279,7 +4283,7 @@ static int mob_read_sqldb(void) // free the query result Sql_FreeResult(mmysql_handle); - ShowStatus("Done reading '"CL_WHITE"%lu"CL_RESET"' entries in '"CL_WHITE"%s"CL_RESET"'.\n", count, mob_db_name[fi]); + ShowStatus("Done reading '" CL_WHITE "%lu" CL_RESET "' entries in '" CL_WHITE "%s" CL_RESET "'.\n", count, mob_db_name[fi]); } return 0; } @@ -4719,13 +4723,14 @@ static int mob_read_sqlskilldb(void) while( SQL_SUCCESS == Sql_NextRow(mmysql_handle) ) { // wrap the result into a TXT-compatible format char* str[19]; - char* dummy = ""; + char dummy[255] = ""; int i; ++lines; for( i = 0; i < 19; ++i ) { Sql_GetData(mmysql_handle, i, &str[i], NULL); - if( str[i] == NULL ) str[i] = dummy; // get rid of NULL columns + if( str[i] == NULL ) + str[i] = dummy; // get rid of NULL columns } if (!mob_parse_row_mobskilldb(str, 19, count)) @@ -4737,7 +4742,7 @@ static int mob_read_sqlskilldb(void) // free the query result Sql_FreeResult(mmysql_handle); - ShowStatus("Done reading '"CL_WHITE"%lu"CL_RESET"' entries in '"CL_WHITE"%s"CL_RESET"'.\n", count, mob_skill_db_name[fi]); + ShowStatus("Done reading '" CL_WHITE "%lu" CL_RESET "' entries in '" CL_WHITE "%s" CL_RESET "'.\n", count, mob_skill_db_name[fi]); } return 0; } @@ -5170,7 +5175,7 @@ static void mob_load(void) int i; const char* dbsubpath[] = { "", - "/"DBIMPORT, + "/" DBIMPORT, }; // First we parse all the possible monsters to add additional data in the second loop @@ -5244,8 +5249,8 @@ void mob_db_load(bool is_reload){ if( !is_reload ) { // on mobdbreload it's not neccessary to execute this // item ers needs to be allocated only once - item_drop_ers = ers_new(sizeof(struct item_drop),"mob.c::item_drop_ers",ERS_OPT_CLEAN); - item_drop_list_ers = ers_new(sizeof(struct item_drop_list),"mob.c::item_drop_list_ers",ERS_OPT_NONE); + item_drop_ers = ers_new(sizeof(struct item_drop),"mob.cpp::item_drop_ers",ERS_OPT_CLEAN); + item_drop_list_ers = ers_new(sizeof(struct item_drop_list),"mob.cpp::item_drop_list_ers",ERS_OPT_NONE); } mob_item_drop_ratio = idb_alloc(DB_OPT_BASE); mob_skill_db = idb_alloc(DB_OPT_BASE); @@ -5375,3 +5380,8 @@ void do_final_mob(bool is_reload){ ers_destroy(item_drop_list_ers); } } + +#ifdef __cplusplus +} +#endif + diff --git a/src/map/mob.h b/src/map/mob.h index 99f440549f..89596e0cf3 100644 --- a/src/map/mob.h +++ b/src/map/mob.h @@ -302,13 +302,13 @@ struct view_data* mob_get_viewdata(int mob_id); void mob_set_dynamic_viewdata( struct mob_data* md ); void mob_free_dynamic_viewdata( struct mob_data* md ); -struct mob_data *mob_once_spawn_sub(struct block_list *bl, int16 m, int16 x, int16 y, const char *mobname, int mob_id, const char *event, unsigned int size, unsigned int ai); +struct mob_data *mob_once_spawn_sub(struct block_list *bl, int16 m, int16 x, int16 y, const char *mobname, int mob_id, const char *event, unsigned int size, enum mob_ai ai); int mob_once_spawn(struct map_session_data* sd, int16 m, int16 x, int16 y, - const char* mobname, int mob_id, int amount, const char* event, unsigned int size, unsigned int ai); + const char* mobname, int mob_id, int amount, const char* event, unsigned int size, enum mob_ai ai); int mob_once_spawn_area(struct map_session_data* sd, int16 m, - int16 x0, int16 y0, int16 x1, int16 y1, const char* mobname, int mob_id, int amount, const char* event, unsigned int size, unsigned int ai); + int16 x0, int16 y0, int16 x1, int16 y1, const char* mobname, int mob_id, int amount, const char* event, unsigned int size, enum mob_ai ai); bool mob_ksprotected (struct block_list *src, struct block_list *target); diff --git a/src/map/script.cpp b/src/map/script.cpp index d4731d516d..6556854d70 100644 --- a/src/map/script.cpp +++ b/src/map/script.cpp @@ -10186,7 +10186,7 @@ BUILDIN_FUNC(monster) int amount = script_getnum(st,7); const char* event = ""; unsigned int size = SZ_SMALL; - unsigned int ai = AI_NONE; + enum mob_ai ai = AI_NONE; struct map_session_data* sd; int16 m; @@ -10206,7 +10206,7 @@ BUILDIN_FUNC(monster) } if (script_hasdata(st, 10)) { - ai = script_getnum(st, 10); + ai = static_cast(script_getnum(st, 10)); if (ai >= AI_MAX) { ShowWarning("buildin_monster: Attempted to spawn non-existing ai %d for monster class %d\n", ai, class_); return SCRIPT_CMD_FAILURE; @@ -10287,7 +10287,7 @@ BUILDIN_FUNC(areamonster) int amount = script_getnum(st,9); const char* event = ""; unsigned int size = SZ_SMALL; - unsigned int ai = AI_NONE; + enum mob_ai ai = AI_NONE; struct map_session_data* sd; int16 m; @@ -10307,7 +10307,7 @@ BUILDIN_FUNC(areamonster) } if (script_hasdata(st, 12)) { - ai = script_getnum(st, 12); + ai = static_cast(script_getnum(st, 12)); if (ai >= AI_MAX) { ShowWarning("buildin_monster: Attempted to spawn non-existing ai %d for monster class %d\n", ai, class_); return SCRIPT_CMD_FAILURE; From a5719e722817f6f38331de018e59a0bebd8c5b08 Mon Sep 17 00:00:00 2001 From: Jey Date: Sun, 1 Oct 2017 18:32:52 +0200 Subject: [PATCH 2/2] Prefer static_casts instead of C casts. --- src/map/mob.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/map/mob.cpp b/src/map/mob.cpp index 3568b3c1bc..e1bb5c79b8 100644 --- a/src/map/mob.cpp +++ b/src/map/mob.cpp @@ -3768,7 +3768,7 @@ int mob_clone_spawn(struct map_session_data *sd, int16 m, int16 x, int16 y, cons if (mode) //User provided mode. status->mode = mode; else if (flag&1) //Friendly Character, remove looting. - status->mode = (enum e_mode)(status->mode&(~MD_LOOTER)); + status->mode = static_cast(status->mode&(~MD_LOOTER)); status->hp = status->max_hp; status->sp = status->max_sp; memcpy(&db->vd, &sd->vd, sizeof(struct view_data)); @@ -4129,9 +4129,9 @@ static bool mob_parse_dbrow(char** str) return false; } - status->mode = (enum e_mode)strtol(str[25], NULL, 0); + status->mode = static_cast(strtol(str[25], NULL, 0)); if (!battle_config.monster_active_enable) - status->mode = (enum e_mode)(status->mode&(~MD_AGGRESSIVE)); + status->mode = static_cast(status->mode&(~MD_AGGRESSIVE)); if (status_has_mode(status,MD_STATUS_IMMUNE|MD_KNOCKBACK_IMMUNE|MD_DETECTOR)) status->class_ = CLASS_BOSS;