diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 7907f82..775df4e 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -194,6 +194,7 @@ set(common_SRCS string.c bridge_temp.cpp servercommand.c + world.c log.cpp content_sao.cpp mapgen/mapgen.cpp @@ -450,7 +451,7 @@ else() set(CMAKE_CXX_FLAGS_RELEASE "${OPT_FLAGS} ${SAFETY_FLAGS} -Wall -DNDEBUG -pipe -fpermissive -Wno-write-strings") set(CMAKE_C_FLAGS_RELEASE "${OPT_FLAGS} ${SAFETY_FLAGS} -Wall -DNDEBUG -pipe") set(CMAKE_CXX_FLAGS_DEBUG "${SAFETY_FLAGS} -Wall -O0 -g3 -ggdb -fpermissive -Wno-write-strings") - set(CMAKE_C_FLAGS_DEBUG ${CMAKE_CXX_FLAGS_DEBUG}) + set(CMAKE_C_FLAGS_DEBUG "${SAFETY_FLAGS} -Wall -O0 -g3 -ggdb") set(CMAKE_EXE_LINKER_FLAGS_RELEASE "${OPT_LDFLAGS} ${SAFETY_LDFLAGS}") set(CMAKE_MODULE_LINKER_FLAGS_RELEASE ${CMAKE_EXE_LINKER_FLAGS_RELEASE}) diff --git a/src/common.h b/src/common.h index 2b06256..2155006 100644 --- a/src/common.h +++ b/src/common.h @@ -113,6 +113,7 @@ int config_load_command(command_context_t *ctx, array_t *args); int config_ignore_command(command_context_t *ctx, array_t *args); void config_init(int argc, char** argv); void config_save(char* section, char* type, char* file); +void config_clear(char* section); /* defined in config_default.c */ void config_default_init(void); @@ -183,6 +184,13 @@ int command_adduser(command_context_t *ctx, array_t *args); int command_clearobjects(command_context_t *ctx, array_t *args); int command_setpassword(command_context_t *ctx, array_t *args); +/* defined in world.c */ +int world_create(char* name); +int world_load(char* name); +void world_unload(void); +int world_init(char* name); +void world_exit(void); + #ifdef __cplusplus } #include diff --git a/src/config.c b/src/config.c index 542f201..f63f745 100644 --- a/src/config.c +++ b/src/config.c @@ -408,3 +408,24 @@ void config_save(char* section, char* type, char* file) file_flush(f); file_free(f); } + +/* clears all config values for section (i.e. "world.*") to defaults */ +void config_clear(char* section) +{ + nvp_t *n; + int l; + + /* don't clear everything, only sections */ + if (!section) + return; + + l = strlen(section); + n = config.items; + while (n) { + if (n->value && !strncmp(n->name,section,l)) { + free(n->value); + n->value = NULL; + } + n = n->next; + } +} diff --git a/src/main.cpp b/src/main.cpp index a60edb3..7c8bd2a 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -791,9 +791,6 @@ int main(int argc, char *argv[]) Game parameters */ - /* TODO: configise this */ - path_world_setter((char*)"default"); - // Run dedicated server if asked to if (config_get_bool("server")) { DSTACK("Dedicated server branch"); @@ -801,6 +798,9 @@ int main(int argc, char *argv[]) // Create time getter g_timegetter = new SimpleTimeGetter(); + char* v = config_get("server.world"); + world_init(v); + // Create server Server server; server.start(); @@ -808,6 +808,8 @@ int main(int argc, char *argv[]) // Run server dedicated_server_loop(server, kill); + world_exit(); + config_save(NULL,NULL,NULL); return 0; @@ -1208,14 +1210,17 @@ int main(int argc, char *argv[]) /* Run game */ - the_game( - kill, - input, - device, - font, - password, - error_message - ); + if (!world_init(NULL)) { + the_game( + kill, + input, + device, + font, + password, + error_message + ); + world_exit(); + } #if USE_AUDIO == 1 sound_stop_effects(0); #endif diff --git a/src/servermain.cpp b/src/servermain.cpp index b157a6b..83e08a0 100644 --- a/src/servermain.cpp +++ b/src/servermain.cpp @@ -231,8 +231,7 @@ int main(int argc, char *argv[]) std::cout< +* +* This program is free software: you can redistribute it and/or modify +* it under the terms of the GNU General Public License as published by +* the Free Software Foundation, either version 3 of the License, or +* (at your option) any later version. +* +* This program is distributed in the hope that it will be useful, but +* WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. +* See the GNU General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with this program. If not, see +************************************************************************/ + +#include "common.h" +#include "path.h" + +static int world_exists(char* name) +{ + char buff[2048]; + char nbuff[256]; + + if (!name) + return 0; + + name = trim(name); + + if (str_sanitise(nbuff,256,name) < 1) + return 0; + + if (!path_get("worlds",nbuff,1,buff,2048)) + return 0; + + return 1; +} + +int world_create(char* name) +{ + char buff[2048]; + char nbuff[256]; + char nbuff1[256]; + int i; + + if (!name) + name = "New World"; + + name = trim(name); + if (!name[0]) + name = "New World"; + + if (str_sanitise(nbuff,256,name) < 1) + return 1; + + if (!path_get("worlds",nbuff,1,buff,2048)) { + if (!path_get("worlds",nbuff,0,buff,2048)) + return 1; + + config_set("world.path",buff); + config_set("world.name",name); + + return path_create("world","players"); + } + + for (i=1; i<100; i++) { + snprintf(nbuff1,256,"%s_%d",nbuff,i); + if (!path_get("worlds",nbuff1,1,buff,2048)) { + if (!path_get("worlds",nbuff1,0,buff,2048)) + return 1; + + config_set("world.path",nbuff1); + snprintf(nbuff,256,"%s %d",name,i); + config_set("world.name",nbuff); + + return path_create("world","players"); + } + } + + return 1; +} + +int world_load(char* name) +{ + char buff[2048]; + char nbuff[256]; + + config_clear("world"); + + if (!name) { +#ifdef SERVER + name = config_get("server.world"); +#else + name = config_get("client.world"); +#endif + if (!name) + return 1; + } + + name = trim(name); + snprintf(nbuff,256,"%s",name); + + if (str_sanitise(buff,2048,nbuff) < 1) + return 1; + + config_set("world.path",buff); + config_set("world.name",nbuff); + + if (path_get("world","world.cfg",1,buff,2048)) { + config_load("world","world.cfg"); + }else{ + vlprintf(CN_WARN,"Unknown world config: using defaults"); + } + + config_set("world.version",VERSION_STRING); + +#ifdef SERVER + config_set("server.world",nbuff); +#else + config_set("client.world",nbuff); +#endif + + return 0; +} + +/* save the world data, then clear all world.* config */ +void world_unload() +{ + config_save("world","world","world.cfg"); + config_clear("world"); +} + +/* initialise and/or create a world */ +int world_init(char* name) +{ + if (!world_exists(name)) { + if (world_create(name)) + return 1; + } + + if (world_load(name)) + return 1; + + /* TODO: init server/environment/etc */ + + + return 0; +} + +/* shutdown, save, clear, and free the current world */ +void world_exit() +{ + /* TODO: shutdown server/environment/etc */ + + world_unload(); +}