some meshgen and map rendering updates
[voxelands-alt.git] / src / core / world.c
blobc2b578a40208c388e0abfd04a51ce3ec9844c4b4
1 /************************************************************************
2 * world.c
3 * voxelands - 3d voxel world sandbox game
4 * Copyright (C) Lisa 'darkrose' Milne 2016 <lisa@ltmnet.com>
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14 * See the GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>
18 ************************************************************************/
20 #include "common.h"
21 #include "path.h"
23 #include "map.h"
25 #include <stdio.h>
28 static struct {
29 } world_data = {
33 static int world_create_new()
35 char buff[2048];
36 char name[256];
37 int i;
39 if (!path_get("worlds","new_world",1,buff,2048)) {
40 if (!path_get("worlds","new_world",0,buff,2048))
41 return 1;
43 command_execf("set world.path %s",buff);
44 command_execf("set world.name New World");
46 return 0;
49 for (i=1; i<100; i++) {
50 snprintf(name,256,"new_world_%d",i);
51 if (!path_get("worlds",name,1,buff,2048)) {
52 if (!path_get("worlds",name,0,buff,2048))
53 return 1;
55 command_execf("set world.path %s",buff);
56 command_execf("set world.name New World %d",i);
58 return 0;
62 return 1;
65 static void world_clear_cfg()
67 config_set("world.name",NULL);
68 config_set("world.path",NULL);
69 config_set("world.map.seed",NULL);
72 /* initialise and/or create a world */
73 int world_init(char* name)
75 char buff[2048];
77 world_clear_cfg();
79 if (!name) {
80 if (world_create_new())
81 return 1;
82 }else{
83 if (str_sanitise(buff,2048,name) < 1)
84 return 1;
86 vlprintf(CN_INFO,"world: '%s' '%s'",name,buff);
88 command_execf("set world.path %s",buff);
89 command_execf("set world.name %s",name);
92 /* TODO: init environment before loading config, so that env commands work */
94 if (path_get("world","world.cfg",1,buff,2048)) {
95 config_load(buff);
96 }else{
97 /* TODO: setup world config from ui/config options */
98 if (config_get("map.seed")) {
99 int s = config_get_int("map.seed");
100 config_set_int("world.map.seed",s);
104 /* TODO: setup new/missing world config */
106 if (!config_get("world.map.seed")) {
107 int s = math_rand_range(-999999,999999);
108 config_set_int("world.map.seed",s);
109 mapgen_seed(s);
110 }else{
111 int s = config_get_int("world.map.seed");
112 mapgen_seed(s);
115 /* TODO: add any server-side map triggers */
117 map_init();
119 return 0;
122 /* shutdown, save, clear, and free the current world */
123 void world_exit()
125 map_exit();
126 config_save("world","world","world.cfg");
127 world_clear_cfg();
128 map_trigger_clear();