| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
#include "global.h" |
| 20 |
|
| 21 |
#include <string.h> |
| 22 |
|
| 23 |
#include "object.h" |
| 24 |
#include "output_file.h" |
| 25 |
#include "sproto.h" |
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
static void write_map_log(void) { |
| 34 |
FILE *fp; |
| 35 |
OutputFile of; |
| 36 |
mapstruct *map; |
| 37 |
char buf[MAX_BUF]; |
| 38 |
long current_time = time(NULL); |
| 39 |
|
| 40 |
snprintf(buf, sizeof(buf), "%s/temp.maps", settings.localdir); |
| 41 |
fp = of_open(&of, buf); |
| 42 |
if (fp == NULL) |
| 43 |
return; |
| 44 |
for (map = first_map; map != NULL; map = map->next) { |
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
if (map->in_memory != MAP_IN_MEMORY |
| 49 |
&& (map->tmpname != NULL) |
| 50 |
&& (strncmp(map->path, "/random", 7))) { |
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
fprintf(fp, "%s:%s:%ld:0:0:%d:0:%d\n", map->path, map->tmpname, |
| 57 |
(map->reset_time == (uint32_t)-1 ? -1 : map->reset_time-current_time), |
| 58 |
map->difficulty, |
| 59 |
map->darkness); |
| 60 |
} |
| 61 |
} |
| 62 |
of_close(&of); |
| 63 |
} |
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
void read_map_log(void) { |
| 71 |
FILE *fp; |
| 72 |
mapstruct *map; |
| 73 |
char buf[MAX_BUF]; |
| 74 |
int do_los, darkness, lock; |
| 75 |
long sec = seconds(); |
| 76 |
|
| 77 |
snprintf(buf, sizeof(buf), "%s/temp.maps", settings.localdir); |
| 78 |
if (!(fp = fopen(buf, "r"))) { |
| 79 |
LOG(llevDebug, "Could not open %s for reading\n", buf); |
| 80 |
return; |
| 81 |
} |
| 82 |
while (fgets(buf, MAX_BUF, fp) != NULL) { |
| 83 |
char *tmp[3]; |
| 84 |
|
| 85 |
map = get_linked_map(); |
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
if (split_string(buf, tmp, sizeof(tmp)/sizeof(*tmp), ':') != 3) { |
| 91 |
LOG(llevDebug, "%s/temp.maps: ignoring invalid line: %s\n", settings.localdir, buf); |
| 92 |
continue; |
| 93 |
} |
| 94 |
|
| 95 |
safe_strncpy(map->path, tmp[0], sizeof(map->path)); |
| 96 |
map->tmpname = strdup_local(tmp[1]); |
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
sscanf(tmp[2], "%u:%d:%d:%hu:%d:%d\n", &map->reset_time, &lock, &lock, &map->difficulty, &do_los, &darkness); |
| 103 |
|
| 104 |
map->in_memory = MAP_SWAPPED; |
| 105 |
map->darkness = darkness; |
| 106 |
map->timeout = 0; |
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
map->reset_time += sec; |
| 117 |
if (map->reset_time > (sec+MAP_MAXRESET)) |
| 118 |
map->reset_time = 0; |
| 119 |
} |
| 120 |
fclose(fp); |
| 121 |
} |
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
int swap_map(mapstruct *map) { |
| 136 |
player *pl; |
| 137 |
int res; |
| 138 |
|
| 139 |
if (map->in_memory != MAP_IN_MEMORY) { |
| 140 |
LOG(llevError, "Tried to swap out map which was not in memory.\n"); |
| 141 |
return SAVE_ERROR_NOT_IN_MEMORY; |
| 142 |
} |
| 143 |
for (pl = first_player; pl != NULL; pl = pl->next) |
| 144 |
if (pl->ob == NULL || (!(QUERY_FLAG(pl->ob, FLAG_REMOVED)) && pl->ob->map == map)) |
| 145 |
break; |
| 146 |
|
| 147 |
if (pl != NULL) { |
| 148 |
LOG(llevDebug, "Wanted to swap out map with player.\n"); |
| 149 |
map->timeout = 0; |
| 150 |
return SAVE_ERROR_PLAYER; |
| 151 |
} |
| 152 |
pets_remove_all(); |
| 153 |
|
| 154 |
|
| 155 |
if (!map->fixed_resettime) |
| 156 |
set_map_reset_time(map); |
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
if (map->reset_time <= seconds()) { |
| 162 |
mapstruct *oldmap = map; |
| 163 |
|
| 164 |
LOG(llevDebug, "Resetting map %s.\n", map->path); |
| 165 |
|
| 166 |
execute_global_event(EVENT_MAPRESET, map); |
| 167 |
map = map->next; |
| 168 |
delete_map(oldmap); |
| 169 |
return SAVE_ERROR_OK; |
| 170 |
} |
| 171 |
|
| 172 |
if ((res = save_map(map, SAVE_MODE_NORMAL)) < 0) { |
| 173 |
LOG(llevError, "Failed to swap map %s.\n", map->path); |
| 174 |
|
| 175 |
draw_ext_info_format(NDI_ALL_DMS|NDI_UNIQUE|NDI_RED, -1, NULL, MSG_TYPE_ADMIN, MSG_TYPE_ADMIN_LOADSAVE, |
| 176 |
"Failed to swap map %s!", map->path); |
| 177 |
|
| 178 |
map->in_memory = MAP_IN_MEMORY; |
| 179 |
return res; |
| 180 |
} |
| 181 |
|
| 182 |
map->timeout = 0; |
| 183 |
free_map(map); |
| 184 |
|
| 185 |
if (settings.recycle_tmp_maps == TRUE) |
| 186 |
write_map_log(); |
| 187 |
|
| 188 |
return SAVE_ERROR_OK; |
| 189 |
} |
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
void check_active_maps(void) { |
| 195 |
|
| 196 |
|
| 197 |
int num_to_swap = 1; |
| 198 |
|
| 199 |
mapstruct *map, *next; |
| 200 |
for (map = first_map; map != NULL; map = next) { |
| 201 |
next = map->next; |
| 202 |
if (map->in_memory == MAP_IN_MEMORY) { |
| 203 |
|
| 204 |
if (map->timeout > 1) { |
| 205 |
map->timeout -= 1; |
| 206 |
} else if (num_to_swap > 0 && map->timeout == 1) { |
| 207 |
swap_map(map); |
| 208 |
num_to_swap -= 1; |
| 209 |
} |
| 210 |
} |
| 211 |
} |
| 212 |
} |
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
int players_on_map(mapstruct *m, int show_all) { |
| 228 |
player *pl; |
| 229 |
int nr = 0; |
| 230 |
|
| 231 |
for (pl = first_player; pl != NULL; pl = pl->next) |
| 232 |
if (pl->ob != NULL |
| 233 |
&& !QUERY_FLAG(pl->ob, FLAG_REMOVED) |
| 234 |
&& pl->ob->map == m |
| 235 |
&& (show_all || !pl->hidden)) |
| 236 |
nr++; |
| 237 |
return nr; |
| 238 |
} |
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
void flush_old_maps(void) { |
| 246 |
mapstruct *m, *oldmap; |
| 247 |
long sec; |
| 248 |
sec = seconds(); |
| 249 |
|
| 250 |
m = first_map; |
| 251 |
while (m) { |
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
if ((m->in_memory == MAP_IN_MEMORY) |
| 256 |
&& (m->timeout == 0) |
| 257 |
&& !players_on_map(m, TRUE)) { |
| 258 |
set_map_timeout(m); |
| 259 |
} |
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
if ((m->unique || m->is_template) && m->in_memory == MAP_SWAPPED) { |
| 272 |
LOG(llevDebug, "Resetting map %s.\n", m->path); |
| 273 |
oldmap = m; |
| 274 |
m = m->next; |
| 275 |
delete_map(oldmap); |
| 276 |
} else if (m->in_memory != MAP_SWAPPED |
| 277 |
|| m->tmpname == NULL |
| 278 |
|| sec < m->reset_time) { |
| 279 |
m = m->next; |
| 280 |
} else { |
| 281 |
LOG(llevDebug, "Resetting map %s.\n", m->path); |
| 282 |
|
| 283 |
execute_global_event(EVENT_MAPRESET, m); |
| 284 |
clean_tmp_map(m); |
| 285 |
oldmap = m; |
| 286 |
m = m->next; |
| 287 |
delete_map(oldmap); |
| 288 |
} |
| 289 |
} |
| 290 |
} |