summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorHoraK-FDF <horak-fdf@web.de>2024-06-20 11:40:01 +0200
committerHoraK-FDF <horak-fdf@web.de>2024-06-20 11:40:01 +0200
commitbd43c968392c58d12f1c5ae98da027681cbbed65 (patch)
tree06fd05d409ca07b9f2f80e54a284418ed4cc0089
parent64cb15b1109d409643fb43d6a5560c36205e29e8 (diff)
downloadtmwa-bd43c968392c58d12f1c5ae98da027681cbbed65.tar.gz
tmwa-bd43c968392c58d12f1c5ae98da027681cbbed65.tar.bz2
tmwa-bd43c968392c58d12f1c5ae98da027681cbbed65.tar.xz
tmwa-bd43c968392c58d12f1c5ae98da027681cbbed65.zip
map hash
-rw-r--r--src/map/map.cpp21
-rw-r--r--src/map/map.hpp1
-rw-r--r--src/map/script-fun.cpp35
3 files changed, 57 insertions, 0 deletions
diff --git a/src/map/map.cpp b/src/map/map.cpp
index ff69a56..4b54eb4 100644
--- a/src/map/map.cpp
+++ b/src/map/map.cpp
@@ -1241,6 +1241,19 @@ int map_setipport(MapName name, IP4Address ip, int port)
}
/*==========================================
+ * creates a hash of a map name
+ *------------------------------------------
+ */
+int map_create_hash(char* str, int len) {
+ const int PRIME_CONST = 37;
+ int hash = 0;
+ for (int i = 0; i < len; i++) {
+ hash += (str[i] * (int)pow(PRIME_CONST, i)) % maps_db.size();
+ }
+ return hash;
+}
+
+/*==========================================
* マップ1枚読み込み
*------------------------------------------
*/
@@ -1265,6 +1278,14 @@ bool map_readmap(map_local *m, size_t num, MapName fn)
m->npc_num = 0;
m->users = 0;
+
+ char str[15+1] = { "\0" };
+ std::copy(fn.begin(), fn.end(), str);
+ str[fn.size()] = '\0';
+ int len = strlen(str);
+
+ m->hash = map_create_hash(str, len);
+
really_memzero_this(&m->flag);
if (battle_config.pk_mode)
m->flag.set(MapFlag::PVP, 1);
diff --git a/src/map/map.hpp b/src/map/map.hpp
index 56d07a5..de0e10b 100644
--- a/src/map/map.hpp
+++ b/src/map/map.hpp
@@ -531,6 +531,7 @@ struct map_local : map_abstract
Point save;
Point resave;
int mask;
+ int hash;
Array<dumb_ptr<npc_data>, MAX_NPC_PER_MAP> npc;
};
diff --git a/src/map/script-fun.cpp b/src/map/script-fun.cpp
index 1efa006..7f63408 100644
--- a/src/map/script-fun.cpp
+++ b/src/map/script-fun.cpp
@@ -5524,6 +5524,39 @@ void builtin_getmapmaxy(ScriptState *st)
}
/*==========================================
+ * Get the hash of a map
+ *------------------------------------------
+ */
+static
+void builtin_getmaphash(ScriptState *st)
+{
+ MapName mapname = stringish<MapName>(ZString(conv_str(st, &AARG(0))));
+ P<map_local> m = TRY_UNWRAP(map_mapname2mapid(mapname), return);
+ push_int<ScriptDataInt>(st->stack, m->hash);
+}
+
+/*==========================================
+ * Get the map name from a hash
+ *------------------------------------------
+ */
+static
+void builtin_getmapnamefromhash(ScriptState *st)
+{
+ int hash = conv_num(st, &AARG(0));
+ MapName mapname = stringish<MapName>(ZString(""_s));
+ for (auto& mit : maps_db)
+ {
+ map_local *ml = static_cast<map_local *>(mit.second.get());
+ if (ml->hash == hash)
+ {
+ mapname = ml->name_;
+ break;
+ }
+ }
+ push_str<ScriptDataStr>(st->stack, mapname);
+}
+
+/*==========================================
* Get the NPC's info
*------------------------------------------
*/
@@ -5787,6 +5820,8 @@ BuiltinFunction builtin_functions[] =
BUILTIN(getmap, "?"_s, 's'),
BUILTIN(getmapmaxx, "M"_s, 'i'),
BUILTIN(getmapmaxy, "M"_s, 'i'),
+ BUILTIN(getmaphash, "M"_s, 'i'),
+ BUILTIN(getmapnamefromhash, "i"_s, 's'),
BUILTIN(mapexit, ""_s, '\0'),
BUILTIN(freeloop, "i"_s, '\0'),
BUILTIN(if_then_else, "iii"_s, 'v'),