Skip to content

Commit

Permalink
HPM: mapindex interface
Browse files Browse the repository at this point in the history
Signed-off-by: shennetsind <ind@henn.et>
  • Loading branch information
shennetsind committed Dec 7, 2013
1 parent 1f1930c commit d4a58d2
Show file tree
Hide file tree
Showing 14 changed files with 226 additions and 194 deletions.
42 changes: 21 additions & 21 deletions src/char/char.c
Expand Up @@ -1073,7 +1073,7 @@ int mmo_chars_fromsql(struct char_session_data* sd, uint8* buf)
}

for( i = 0; i < MAX_CHARS && SQL_SUCCESS == SQL->StmtNextRow(stmt); i++ ) {
p.last_point.map = mapindex_name2id(last_map);
p.last_point.map = mapindex->name2id(last_map);
sd->found_char[p.slot] = p.char_id;
sd->unban_time[p.slot] = unban_time;
j += mmo_char_tobuf(WBUFP(buf, j), &p);
Expand Down Expand Up @@ -1199,17 +1199,17 @@ int mmo_char_fromsql(int char_id, struct mmo_charstatus* p, bool load_everything

account_id = p->account_id;

p->last_point.map = mapindex_name2id(last_map);
p->save_point.map = mapindex_name2id(save_map);
p->last_point.map = mapindex->name2id(last_map);
p->save_point.map = mapindex->name2id(save_map);

if( p->last_point.map == 0 ) {
p->last_point.map = (unsigned short)strdb_iget(mapindex_db, MAP_DEFAULT);
p->last_point.map = (unsigned short)strdb_iget(mapindex->db, MAP_DEFAULT);
p->last_point.x = MAP_DEFAULT_X;
p->last_point.y = MAP_DEFAULT_Y;
}

if( p->save_point.map == 0 ) {
p->save_point.map = (unsigned short)strdb_iget(mapindex_db, MAP_DEFAULT);
p->save_point.map = (unsigned short)strdb_iget(mapindex->db, MAP_DEFAULT);
p->save_point.x = MAP_DEFAULT_X;
p->save_point.y = MAP_DEFAULT_Y;
}
Expand All @@ -1233,7 +1233,7 @@ int mmo_char_fromsql(int char_id, struct mmo_charstatus* p, bool load_everything
SqlStmt_ShowDebug(stmt);

for( i = 0; i < MAX_MEMOPOINTS && SQL_SUCCESS == SQL->StmtNextRow(stmt); ++i ) {
tmp_point.map = mapindex_name2id(point_map);
tmp_point.map = mapindex->name2id(point_map);
memcpy(&p->memo_point[i], &tmp_point, sizeof(tmp_point));
}
strcat(t_msg, " memo");
Expand Down Expand Up @@ -1913,7 +1913,7 @@ int mmo_char_tobuf(uint8* buffer, struct mmo_charstatus* p) {
offset += 2;
#endif
#if (PACKETVER >= 20100720 && PACKETVER <= 20100727) || PACKETVER >= 20100803
mapindex_getmapname_ext(mapindex_id2name(p->last_point.map), (char*)WBUFP(buf,108));
mapindex->getmapname_ext(mapindex_id2name(p->last_point.map), (char*)WBUFP(buf,108));
offset += MAP_NAME_LENGTH_EXT;
#endif
#if PACKETVER >= 20100803
Expand Down Expand Up @@ -4140,22 +4140,22 @@ int parse_char(int fd)
WFIFOSET(fd,3);
break;
}
if ((i = search_mapserver((j=mapindex_name2id(MAP_PRONTERA)),-1,-1)) >= 0) {
if ((i = search_mapserver((j=mapindex->name2id(MAP_PRONTERA)),-1,-1)) >= 0) {
cd->last_point.x = 273;
cd->last_point.y = 354;
} else if ((i = search_mapserver((j=mapindex_name2id(MAP_GEFFEN)),-1,-1)) >= 0) {
} else if ((i = search_mapserver((j=mapindex->name2id(MAP_GEFFEN)),-1,-1)) >= 0) {
cd->last_point.x = 120;
cd->last_point.y = 100;
} else if ((i = search_mapserver((j=mapindex_name2id(MAP_MORROC)),-1,-1)) >= 0) {
} else if ((i = search_mapserver((j=mapindex->name2id(MAP_MORROC)),-1,-1)) >= 0) {
cd->last_point.x = 160;
cd->last_point.y = 94;
} else if ((i = search_mapserver((j=mapindex_name2id(MAP_ALBERTA)),-1,-1)) >= 0) {
} else if ((i = search_mapserver((j=mapindex->name2id(MAP_ALBERTA)),-1,-1)) >= 0) {
cd->last_point.x = 116;
cd->last_point.y = 57;
} else if ((i = search_mapserver((j=mapindex_name2id(MAP_PAYON)),-1,-1)) >= 0) {
} else if ((i = search_mapserver((j=mapindex->name2id(MAP_PAYON)),-1,-1)) >= 0) {
cd->last_point.x = 87;
cd->last_point.y = 117;
} else if ((i = search_mapserver((j=mapindex_name2id(MAP_IZLUDE)),-1,-1)) >= 0) {
} else if ((i = search_mapserver((j=mapindex->name2id(MAP_IZLUDE)),-1,-1)) >= 0) {
cd->last_point.x = 94;
cd->last_point.y = 103;
} else {
Expand Down Expand Up @@ -4189,7 +4189,7 @@ int parse_char(int fd)
WFIFOHEAD(fd,28);
WFIFOW(fd,0) = 0x71;
WFIFOL(fd,2) = cd->char_id;
mapindex_getmapname_ext(mapindex_id2name(cd->last_point.map), (char*)WFIFOP(fd,6));
mapindex->getmapname_ext(mapindex_id2name(cd->last_point.map), (char*)WFIFOP(fd,6));
subnet_map_ip = lan_subnetcheck(ipl); // Advanced subnet check [LuzZza]
WFIFOL(fd,22) = htonl((subnet_map_ip) ? subnet_map_ip : server[i].ip);
WFIFOW(fd,26) = ntows(htons(server[i].port)); // [!] LE byte order here [!]
Expand Down Expand Up @@ -5014,7 +5014,7 @@ int char_config_read(const char* cfgName)
int x, y;
if (sscanf(w2, "%15[^,],%d,%d", map, &x, &y) < 3)
continue;
start_point.map = mapindex_name2id(map);
start_point.map = mapindex->name2id(map);
if (!start_point.map)
ShowError("Specified start_point %s not found in map-index cache.\n", map);
start_point.x = x;
Expand Down Expand Up @@ -5122,7 +5122,7 @@ void do_final(void) {
}

SQL->Free(sql_handle);
mapindex_final();
mapindex->final();

for(i = 0; i < MAX_MAP_SERVERS; i++ )
if( server[i].map )
Expand Down Expand Up @@ -5169,13 +5169,13 @@ int do_init(int argc, char **argv) {
for(i = 0; i < MAX_MAP_SERVERS; i++ )
server[i].map = NULL;

//Read map indexes
mapindex_init();
start_point.map = mapindex_name2id("new_zone01");


mapindex_defaults();
pincode_defaults();

//Read map indexes
mapindex->init();
start_point.map = mapindex->name2id("new_zone01");

char_config_read((argc < 2) ? CHAR_CONF_NAME : argv[1]);
char_lan_config_read((argc > 3) ? argv[3] : LAN_CONF_NAME);
sql_config_read(SQL_CONF_NAME);
Expand Down
2 changes: 1 addition & 1 deletion src/char/int_party.c
Expand Up @@ -241,7 +241,7 @@ struct party_data *inter_party_fromsql(int party_id)
SQL->GetData(sql_handle, 1, &data, NULL); m->char_id = atoi(data);
SQL->GetData(sql_handle, 2, &data, &len); memcpy(m->name, data, min(len, NAME_LENGTH));
SQL->GetData(sql_handle, 3, &data, NULL); m->lv = atoi(data);
SQL->GetData(sql_handle, 4, &data, NULL); m->map = mapindex_name2id(data);
SQL->GetData(sql_handle, 4, &data, NULL); m->map = mapindex->name2id(data);
SQL->GetData(sql_handle, 5, &data, NULL); m->online = (atoi(data) ? 1 : 0);
SQL->GetData(sql_handle, 6, &data, NULL); m->class_ = atoi(data);
m->leader = (m->account_id == leader_id && m->char_id == leader_char ? 1 : 0);
Expand Down
92 changes: 52 additions & 40 deletions src/common/mapindex.c
Expand Up @@ -13,19 +13,8 @@
#include <stdio.h>
#include <stdlib.h>

struct _indexes {
char name[MAP_NAME_LENGTH]; //Stores map name
} indexes[MAX_MAPINDEX];

int max_index = 0;

char mapindex_cfgfile[80] = "db/map_index.txt";

#define mapindex_exists_sub(id) (indexes[id].name[0] != '\0')

bool mapindex_exists(int id) {
return mapindex_exists_sub(id);
}
/* mapindex.c interface source */
struct mapindex_interface mapindex_s;

/// Retrieves the map name from 'string' (removing .gat extension if present).
/// Result gets placed either into 'buf' or in a static local buffer.
Expand Down Expand Up @@ -83,9 +72,8 @@ int mapindex_addmap(int index, const char* name) {
char map_name[MAP_NAME_LENGTH];

if (index == -1){
for (index = 1; index < max_index; index++) {
//if (strcmp(indexes[index].name,"#CLEARED#")==0)
if (indexes[index].name[0] == '\0')
for (index = 1; index < mapindex->num; index++) {
if (mapindex->list[index].name[0] == '\0')
break;
}
}
Expand All @@ -95,7 +83,7 @@ int mapindex_addmap(int index, const char* name) {
return 0;
}

mapindex_getmapname(name, map_name);
mapindex->getmapname(name, map_name);

if (map_name[0] == '\0') {
ShowError("(mapindex_add) Cannot add maps with no name.\n");
Expand All @@ -107,15 +95,16 @@ int mapindex_addmap(int index, const char* name) {
return 0;
}

if (mapindex_exists_sub(index)) {
ShowWarning("(mapindex_add) Overriding index %d: map \"%s\" -> \"%s\"\n", index, indexes[index].name, map_name);
strdb_remove(mapindex_db, indexes[index].name);
if (mapindex_exists(index)) {
ShowWarning("(mapindex_add) Overriding index %d: map \"%s\" -> \"%s\"\n", index, mapindex->list[index].name, map_name);
strdb_remove(mapindex->db, mapindex->list[index].name);
}

safestrncpy(indexes[index].name, map_name, MAP_NAME_LENGTH);
strdb_iput(mapindex_db, map_name, index);
if (max_index <= index)
max_index = index+1;
safestrncpy(mapindex->list[index].name, map_name, MAP_NAME_LENGTH);
strdb_iput(mapindex->db, map_name, index);

if (mapindex->num <= index)
mapindex->num = index+1;

return index;
}
Expand All @@ -124,21 +113,21 @@ unsigned short mapindex_name2id(const char* name) {
int i;
char map_name[MAP_NAME_LENGTH];

mapindex_getmapname(name, map_name);
mapindex->getmapname(name, map_name);

if( (i = strdb_iget(mapindex_db, map_name)) )
if( (i = strdb_iget(mapindex->db, map_name)) )
return i;

ShowDebug("mapindex_name2id: Map \"%s\" not found in index list!\n", map_name);
return 0;
}

const char* mapindex_id2name_sub(unsigned short id,const char *file, int line, const char *func) {
if (id > MAX_MAPINDEX || !mapindex_exists_sub(id)) {
if (id > MAX_MAPINDEX || !mapindex_exists(id)) {
ShowDebug("mapindex_id2name: Requested name for non-existant map index [%d] in cache. %s:%s:%d\n", id,file,func,line);
return indexes[0].name; // dummy empty string so that the callee doesn't crash
return mapindex->list[0].name; // dummy empty string so that the callee doesn't crash
}
return indexes[id].name;
return mapindex->list[id].name;
}

int mapindex_init(void) {
Expand All @@ -148,12 +137,13 @@ int mapindex_init(void) {
int index, total = 0;
char map_name[12];

if( ( fp = fopen(mapindex_cfgfile,"r") ) == NULL ){
ShowFatalError("Unable to read mapindex config file %s!\n", mapindex_cfgfile);
if( ( fp = fopen(mapindex->config_file,"r") ) == NULL ){
ShowFatalError("Unable to read mapindex config file %s!\n", mapindex->config_file);
exit(EXIT_FAILURE); //Server can't really run without this file.
}
memset (&indexes, 0, sizeof (indexes));
mapindex_db = strdb_alloc(DB_OPT_DUP_KEY, MAP_NAME_LENGTH);

mapindex->db = strdb_alloc(DB_OPT_DUP_KEY, MAP_NAME_LENGTH);

while(fgets(line, sizeof(line), fp)) {
if(line[0] == '/' && line[1] == '/')
continue;
Expand All @@ -162,7 +152,7 @@ int mapindex_init(void) {
case 1: //Map with no ID given, auto-assign
index = last_index+1;
case 2: //Map with ID given
mapindex_addmap(index,map_name);
mapindex->addmap(index,map_name);
total++;
break;
default:
Expand All @@ -172,18 +162,40 @@ int mapindex_init(void) {
}
fclose(fp);

if( !strdb_iget(mapindex_db, MAP_DEFAULT) ) {
if( !strdb_iget(mapindex->db, MAP_DEFAULT) ) {
ShowError("mapindex_init: MAP_DEFAULT '%s' not found in cache! update mapindex.h MAP_DEFAULT var!!!\n",MAP_DEFAULT);
}

return total;
}

int mapindex_removemap(int index){
strdb_remove(mapindex_db, indexes[index].name);
indexes[index].name[0] = '\0';
return 0;
void mapindex_removemap(int index){
strdb_remove(mapindex->db, mapindex->list[index].name);
mapindex->list[index].name[0] = '\0';
}

void mapindex_final(void) {
db_destroy(mapindex_db);
db_destroy(mapindex->db);
}

void mapindex_defaults(void) {
mapindex = &mapindex_s;

/* TODO: place it in inter-server.conf? */
snprintf(mapindex->config_file, 80, "%s","db/map_index.txt");
/* */
mapindex->db = NULL;
mapindex->num = 0;
memset (&mapindex->list, 0, sizeof (mapindex->list));

/* */
mapindex->init = mapindex_init;
mapindex->final = mapindex_final;
/* */
mapindex->addmap = mapindex_addmap;
mapindex->removemap = mapindex_removemap;
mapindex->getmapname = mapindex_getmapname;
mapindex->getmapname_ext = mapindex_getmapname_ext;
mapindex->name2id = mapindex_name2id;
mapindex->id2name = mapindex_id2name_sub;
}
54 changes: 38 additions & 16 deletions src/common/mapindex.h
Expand Up @@ -6,17 +6,16 @@
#define _MAPINDEX_H_

#include "../common/db.h"
#include "../common/mmo.h"

#define MAX_MAPINDEX 2000

/* wohoo, someone look at all those |: map_default could (or *should*) be a char-server.conf */

// When a map index search fails, return results from what map? default:prontera
#define MAP_DEFAULT "prontera"
#define MAP_DEFAULT_X 150
#define MAP_DEFAULT_Y 150
DBMap *mapindex_db;

//File in charge of assigning a numberic ID to each map in existance for space saving when passing map info between servers.
extern char mapindex_cfgfile[80];

#define MAX_MAPINDEX 2000

//Some definitions for the mayor city maps.
#define MAP_PRONTERA "prontera"
Expand Down Expand Up @@ -56,16 +55,39 @@ extern char mapindex_cfgfile[80];
#define MAP_MALAYA "malaya"
#define MAP_ECLAGE "eclage"

bool mapindex_exists(int id);
const char* mapindex_getmapname(const char* string, char* output);
const char* mapindex_getmapname_ext(const char* string, char* output);
unsigned short mapindex_name2id(const char*);
#define mapindex_id2name(n) mapindex_id2name_sub((n),__FILE__, __LINE__, __func__)
const char* mapindex_id2name_sub(unsigned short,const char *file, int line, const char *func);
int mapindex_init(void);
void mapindex_final(void);
#define mapindex_id2name(n) mapindex->id2name((n),__FILE__, __LINE__, __func__)
#define mapindex_exists(n) ( mapindex->list[(n)].name[0] != '\0' )

/**
* mapindex.c interface
**/
struct mapindex_interface {
char config_file[80];
/* mapname (str) -> index (int) */
DBMap *db;
/* number of entries in the index table */
int num;
/* index list -- since map server map count is *unlimited* this should be too */
struct {
char name[MAP_NAME_LENGTH];
} list[MAX_MAPINDEX];
/* */
int (*init) (void);
void (*final) (void);
/* */
int (*addmap) (int index, const char* name);
void (*removemap) (int index);
const char* (*getmapname) (const char* string, char* output);
/* TODO: server shouldn't be handling the extension, game client automatically adds .gat/.rsw/.whatever
* and there are official map names taking advantage of it that we cant support due to the .gat room being saved */
const char* (*getmapname_ext) (const char* string, char* output);
/* TODO: Hello World! make up your mind, this thing is int on some places and unsigned short on others */
unsigned short (*name2id) (const char*);
const char* (*id2name) (unsigned short,const char *file, int line, const char *func);
};

struct mapindex_interface *mapindex;

int mapindex_addmap(int index, const char* name);
int mapindex_removemap(int index);
void mapindex_defaults(void);

#endif /* _MAPINDEX_H_ */

0 comments on commit d4a58d2

Please sign in to comment.