Clean up instance map name generation (#5704)

Put the instance map name generation in a seperate function
Fix some compiler warnings
Fixes #5691
Thanks @RadianFord
This commit is contained in:
Vincent Stumpf 2021-02-25 15:01:44 -08:00 committed by GitHub
parent 7d6c1c9a38
commit ca779dad59
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 26 additions and 20 deletions

View File

@ -822,7 +822,7 @@ int chclif_parse_charselect(int fd, struct char_session_data* sd,uint32 ipl){
WFIFOHEAD(fd, 24);
WFIFOW(fd, 0) = 0x840;
WFIFOW(fd, 2) = 24;
memcpy(WFIFOP(fd, 4), "0", 20); // we can't send it empty (otherwise the list will pop up)
strncpy(WFIFOCP(fd, 4), "0", 20); // we can't send it empty (otherwise the list will pop up)
WFIFOSET(fd, 24);
return 1;
}

View File

@ -4,6 +4,7 @@
#include "instance.hpp"
#include <stdlib.h>
#include <math.h>
#include <yaml-cpp/yaml.h>
#include "../common/cbasetypes.hpp"
@ -708,6 +709,26 @@ int instance_addmap(int instance_id) {
return idata->map.size();
}
/**
* Fills outname with the name of the instance map name
* @param map_id: Mapid to use
* @param instance_id: Instance id
* @param outname: Pointer to allocated memory that will be filled in
*/
void instance_generate_mapname(int map_id, int instance_id, char outname[MAP_NAME_LENGTH]) {
#if MAX_MAP_PER_SERVER > 9999
#error This algorithm is only safe for up to 9999 maps, change at your own risk.
#endif
// Safe up to 9999 maps per map-server
static const int prefix_length = 4;
// Full map name length - prefix length - seperator character - zero termination
static const int suffix_length = MAP_NAME_LENGTH - prefix_length - 1 - 1;
static const int prefix_limit = pow(10, prefix_length);
static const int suffix_limit = pow(10, suffix_length);
safesnprintf(outname, MAP_NAME_LENGTH, "%0*u#%0*u", prefix_length, map_id % prefix_limit, suffix_length, instance_id % suffix_limit);
}
/**
* Returns an instance map ID
* @param m: Source map ID
@ -730,15 +751,8 @@ int16 instance_mapid(int16 m, int instance_id)
for (const auto &it : idata->map) {
if (it.src_m == m) {
char iname[MAP_NAME_LENGTH], alt_name[MAP_NAME_LENGTH];
strcpy(iname, name);
if (!(strchr(iname, '@')) && strlen(iname) > 8) {
memmove(iname, iname + (strlen(iname) - 9), strlen(iname));
snprintf(alt_name, sizeof(alt_name), "%d#%s", (instance_id % 1000), iname);
} else
snprintf(alt_name, sizeof(alt_name), "%.3d%s", (instance_id % 1000), iname);
char alt_name[MAP_NAME_LENGTH];
instance_generate_mapname(m, instance_id, alt_name);
return map_mapname2mapid(alt_name);
}
}

View File

@ -118,6 +118,7 @@ e_instance_enter instance_enter(struct map_session_data *sd, int instance_id, co
bool instance_reqinfo(struct map_session_data *sd, int instance_id);
bool instance_addusers(int instance_id);
bool instance_delusers(int instance_id);
void instance_generate_mapname(int map_id, int instance_id, char outname[MAP_NAME_LENGTH]);
int16 instance_mapid(int16 m, int instance_id);
int instance_addmap(int instance_id);

View File

@ -2724,19 +2724,10 @@ int map_addinstancemap(int src_m, int instance_id)
struct map_data *src_map = map_getmapdata(src_m);
struct map_data *dst_map = map_getmapdata(dst_m);
char iname[MAP_NAME_LENGTH];
strcpy(iname, name);
// Alter the name
// Due to this being custom we only worry about preserving as many characters as necessary for accurate map distinguishing
// This also allows us to maintain complete independence with main map functions
if ((strchr(iname, '@') == nullptr) && strlen(iname) > 8) {
memmove(iname, iname + (strlen(iname) - 9), strlen(iname));
snprintf(dst_map->name, sizeof(dst_map->name), "%d#%s", (instance_id % 1000), iname);
} else
snprintf(dst_map->name, sizeof(dst_map->name), "%.3d%s", (instance_id % 1000), iname);
dst_map->name[MAP_NAME_LENGTH - 1] = '\0';
instance_generate_mapname(src_m, instance_id, dst_map->name);
dst_map->m = dst_m;
dst_map->instance_id = instance_id;