diff options
Diffstat (limited to 'src/map/map.cpp')
-rw-r--r-- | src/map/map.cpp | 335 |
1 files changed, 93 insertions, 242 deletions
diff --git a/src/map/map.cpp b/src/map/map.cpp index 25933a9..d502fbb 100644 --- a/src/map/map.cpp +++ b/src/map/map.cpp @@ -63,13 +63,16 @@ #include "atcommand.hpp" #include "battle.hpp" +#include "battle_conf.hpp" #include "chrif.hpp" #include "clif.hpp" +#include "globals.hpp" #include "grfio.hpp" #include "itemdb.hpp" #include "magic-interpreter.hpp" // for is_spell inline body #include "magic-stmt.hpp" #include "magic-v2.hpp" +#include "map_conf.hpp" #include "mob.hpp" #include "npc.hpp" #include "npc-parse.hpp" @@ -85,47 +88,16 @@ namespace tmwa { -DMap<BlockId, dumb_ptr<block_list>> id_db; - -UPMap<MapName, map_abstract> maps_db; - -static -DMap<CharName, dumb_ptr<map_session_data>> nick_db; - -struct charid2nick -{ - CharName nick; - int req_id; -}; - -static -Map<CharId, struct charid2nick> charid_db; - -static -int users = 0; -static -Array<dumb_ptr<block_list>, unwrap<BlockId>(MAX_FLOORITEM)> object; -static -BlockId first_free_object_id = BlockId(); - -interval_t autosave_time = DEFAULT_AUTOSAVE_INTERVAL; -int save_settings = 0xFFFF; - -AString motd_txt = "conf/motd.txt"_s; - -const CharName WISP_SERVER_NAME = stringish<CharName>("Server"_s); - -map_local undefined_gat = [](){ map_local rv {}; rv.name_ = stringish<MapName>("undefined.gat"_s); return rv; }(); - void SessionDeleter::operator()(SessionData *sd) { - really_delete1 static_cast<map_session_data *>(sd); + really_delete1 static_cast<map::map_session_data *>(sd); } -VString<49> convert_for_printf(NpcEvent ev) +namespace map { - return STRNPRINTF(50, "%s::%s"_fmt, ev.npc, ev.label); -} +const CharName WISP_SERVER_NAME = stringish<CharName>("Server"_s); + +map_local undefined_gat = [](){ map_local rv {}; rv.name_ = stringish<MapName>("undefined.gat"_s); return rv; }(); /*========================================== * 全map鯖総計での接続数設定 @@ -134,7 +106,7 @@ VString<49> convert_for_printf(NpcEvent ev) */ void map_setusers(int n) { - users = n; + world_user_count = n; } /*========================================== @@ -143,14 +115,9 @@ void map_setusers(int n) */ int map_getusers(void) { - return users; + return world_user_count; } -static -int block_free_lock = 0; -static -std::vector<dumb_ptr<block_list>> block_free; - void MapBlockLock::freeblock(dumb_ptr<block_list> bl) { if (block_free_lock == 0) @@ -175,12 +142,6 @@ MapBlockLock::~MapBlockLock() } } -/// This is a dummy entry that is shared by all the linked lists, -/// so that any entry can unlink itself without worrying about -/// whether it was the the head of the list. -static -struct block_list bl_head; - /*========================================== * map[]のblock_listに追加 * mobは数が多いので別リスト @@ -776,14 +737,14 @@ BlockId map_addflooritem(Item *item_data, int amount, interval_t owner_protection[3]; { - owner_protection[0] = static_cast<interval_t>(battle_config.item_first_get_time); - owner_protection[1] = owner_protection[0] + static_cast<interval_t>(battle_config.item_second_get_time); - owner_protection[2] = owner_protection[1] + static_cast<interval_t>(battle_config.item_third_get_time); + owner_protection[0] = battle_config.item_first_get_time; + owner_protection[1] = owner_protection[0] + battle_config.item_second_get_time; + owner_protection[2] = owner_protection[1] + battle_config.item_third_get_time; } return map_addflooritem_any(item_data, amount, m, x, y, owners, owner_protection, - static_cast<interval_t>(battle_config.flooritem_lifetime), 1); + battle_config.flooritem_lifetime, 1); } /*========================================== @@ -1252,7 +1213,7 @@ int map_setipport(MapName name, IP4Address ip, int port) if (md->gat) { // local -> check data - if (ip != clif_getip() || port != clif_getport()) + if (ip != map_conf.map_ip || port != map_conf.map_port) { PRINTF("from char server : %s -> %s:%d\n"_fmt, name, ip, port); @@ -1399,18 +1360,11 @@ void map_delmap(MapName mapname) constexpr int LOGFILE_SECONDS_PER_CHUNK_SHIFT = 10; static -std::unique_ptr<io::AppendFile> map_logfile; -static -AString map_logfile_name; -static -long map_logfile_index; - -static void map_close_logfile(void) { if (map_logfile) { - AString filename = STRPRINTF("%s.%ld"_fmt, map_logfile_name, map_logfile_index); + AString filename = STRPRINTF("%s.%ld"_fmt, map_conf.log_file, map_logfile_index); const char *args[] = { "gzip", @@ -1438,22 +1392,23 @@ void map_start_logfile(long index) AString filename_buf = STRPRINTF( "%s.%ld"_fmt, - map_logfile_name, + map_conf.log_file, map_logfile_index); map_logfile = make_unique<io::AppendFile>(filename_buf); if (!map_logfile->is_open()) { map_logfile.reset(); - perror(map_logfile_name.c_str()); + perror(map_conf.log_file.c_str()); } } static -void map_set_logfile(AString filename) +void map_set_logfile() { - struct timeval tv; + if (!map_conf.log_file) + return; - map_logfile_name = std::move(filename); + struct timeval tv; gettimeofday(&tv, nullptr); map_start_logfile(tv.tv_sec >> LOGFILE_SECONDS_PER_CHUNK_SHIFT); @@ -1479,127 +1434,6 @@ void map_log(XString line) } static -bool map_config(io::Spanned<XString> w1, io::Spanned<ZString> w2) -{ - struct hostent *h = nullptr; - - { - if (w1.data == "userid"_s) - { - AccountName name = stringish<AccountName>(w2.data); - chrif_setuserid(name); - } - else if (w1.data == "passwd"_s) - { - AccountPass pass = stringish<AccountPass>(w2.data); - chrif_setpasswd(pass); - } - else if (w1.data == "char_ip"_s) - { - h = gethostbyname(w2.data.c_str()); - IP4Address w2ip; - if (h != nullptr) - { - w2ip = IP4Address({ - static_cast<uint8_t>(h->h_addr[0]), - static_cast<uint8_t>(h->h_addr[1]), - static_cast<uint8_t>(h->h_addr[2]), - static_cast<uint8_t>(h->h_addr[3]), - }); - PRINTF("Character server IP address : %s -> %s\n"_fmt, - w2.data, w2ip); - } - else - { - PRINTF("Bad IP value: %s\n"_fmt, w2.data); - return false; - } - chrif_setip(w2ip); - } - else if (w1.data == "char_port"_s) - { - chrif_setport(atoi(w2.data.c_str())); - } - else if (w1.data == "map_ip"_s) - { - h = gethostbyname(w2.data.c_str()); - IP4Address w2ip; - if (h != nullptr) - { - w2ip = IP4Address({ - static_cast<uint8_t>(h->h_addr[0]), - static_cast<uint8_t>(h->h_addr[1]), - static_cast<uint8_t>(h->h_addr[2]), - static_cast<uint8_t>(h->h_addr[3]), - }); - PRINTF("Map server IP address : %s -> %s\n"_fmt, - w2.data, w2ip); - } - else - { - PRINTF("Bad IP value: %s\n"_fmt, w2.data); - return false; - } - clif_setip(w2ip); - } - else if (w1.data == "map_port"_s) - { - clif_setport(atoi(w2.data.c_str())); - } - else if (w1.data == "map"_s) - { - MapName name = VString<15>(w2.data); - map_addmap(name); - } - else if (w1.data == "delmap"_s) - { - MapName name = VString<15>(w2.data); - map_delmap(name); - } - else if (w1.data == "npc"_s) - { - npc_addsrcfile(w2.data); - } - else if (w1.data == "delnpc"_s) - { - npc_delsrcfile(w2.data); - } - else if (w1.data == "autosave_time"_s) - { - autosave_time = std::chrono::seconds(atoi(w2.data.c_str())); - if (autosave_time <= interval_t::zero()) - autosave_time = DEFAULT_AUTOSAVE_INTERVAL; - } - else if (w1.data == "motd_txt"_s) - { - motd_txt = w2.data; - } - else if (w1.data == "mapreg_txt"_s) - { - mapreg_txt = w2.data; - } - else if (w1.data == "gm_log"_s) - { - gm_log = std::move(w2.data); - } - else if (w1.data == "log_file"_s) - { - map_set_logfile(w2.data); - } - else if (w1.data == "import"_s) - { - return load_config_file(w2.data, map_config); - } - else - { - return false; - } - } - - return true; -} - -static void cleanup_sub(dumb_ptr<block_list> bl) { nullpo_retv(bl); @@ -1624,42 +1458,21 @@ void cleanup_sub(dumb_ptr<block_list> bl) } } -/*========================================== - * map鯖終了時処理 - *------------------------------------------ - */ -void term_func(void) +int compare_item(Item *a, Item *b) { - for (auto& mit : maps_db) - { - if (!mit.second->gat) - continue; - P<map_local> map_id = borrow(*mit.second).downcast_to<map_local>(); - - map_foreachinarea(cleanup_sub, - map_id, - 0, 0, - map_id->xs, map_id->ys, - BL::NUL); - } - - for (io::FD i : iter_fds()) - delete_session(get_session(i)); - - map_removenpc(); - - maps_db.clear(); - - do_final_script(); - do_final_itemdb(); - do_final_storage(); + return (a->nameid == b->nameid); +} - map_close_logfile(); +static +bool map_config(io::Spanned<XString> key, io::Spanned<ZString> value) +{ + return parse_map_conf(map_conf, key, value); } -int compare_item(Item *a, Item *b) +static +bool battle_config_(io::Spanned<XString> key, io::Spanned<ZString> value) { - return (a->nameid == b->nameid); + return parse_battle_conf(battle_config, key, value); } static @@ -1668,7 +1481,7 @@ bool map_confs(io::Spanned<XString> key, io::Spanned<ZString> value) if (key.data == "map_conf"_s) return load_config_file(value.data, map_config); if (key.data == "battle_conf"_s) - return battle_config_read(value.data); + return load_config_file(value.data, battle_config_); if (key.data == "atcommand_conf"_s) return atcommand_config_read(value.data); @@ -1687,16 +1500,72 @@ bool map_confs(io::Spanned<XString> key, io::Spanned<ZString> value) return load_resnametable(value.data); if (key.data == "const_db"_s) return read_constdb(value.data); - PRINTF("unknown map conf key: %s\n"_fmt, AString(key.data)); + key.span.error("Unknown meta-key for map server"_s); return false; } +int map_scriptcont(dumb_ptr<map_session_data> sd, BlockId id) +{ + dumb_ptr<block_list> bl = map_id2bl(id); + + if (!bl) + return 0; + + switch (bl->bl_type) + { + case BL::NPC: + return npc_scriptcont(sd, id); + case BL::SPELL: + magic::spell_execute_script(bl->is_spell()); + break; + } + + return 0; +} +} // namespace map + +/*========================================== + * map鯖終了時処理 + *------------------------------------------ + */ +void term_func(void) +{ + using namespace tmwa::map; + for (auto& mit : maps_db) + { + if (!mit.second->gat) + continue; + P<map_local> map_id = borrow(*mit.second).downcast_to<map_local>(); + + map_foreachinarea(cleanup_sub, + map_id, + 0, 0, + map_id->xs, map_id->ys, + BL::NUL); + } + + for (io::FD i : iter_fds()) + delete_session(get_session(i)); + + map_removenpc(); + + maps_db.clear(); + + do_final_script(); + do_final_itemdb(); + do_final_storage(); + + map_close_logfile(); +} + /*====================================================== * Map-Server Init and Command-line Arguments [Valaris] *------------------------------------------------------ */ int do_init(Slice<ZString> argv) { + using namespace tmwa::map; + ZString argv0 = argv.pop_front(); runflag &= magic::magic_init0(); @@ -1744,7 +1613,8 @@ int do_init(Slice<ZString> argv) if (!loaded_config_yet) runflag &= load_config_file("conf/tmwa-map.conf"_s, map_confs); - battle_config_check(); + map_set_logfile(); + runflag &= map_readallmap(); do_init_chrif(); @@ -1762,26 +1632,7 @@ int do_init(Slice<ZString> argv) PRINTF("The server is running in " SGR_BOLD SGR_RED "PK Mode" SGR_RESET "\n"_fmt); PRINTF("The map-server is " SGR_BOLD SGR_GREEN "ready" SGR_RESET " (Server is listening on the port %d).\n\n"_fmt, - clif_getport()); - - return 0; -} - -int map_scriptcont(dumb_ptr<map_session_data> sd, BlockId id) -{ - dumb_ptr<block_list> bl = map_id2bl(id); - - if (!bl) - return 0; - - switch (bl->bl_type) - { - case BL::NPC: - return npc_scriptcont(sd, id); - case BL::SPELL: - magic::spell_execute_script(bl->is_spell()); - break; - } + map_conf.map_port); return 0; } |