… | |
… | |
23 | */ |
23 | */ |
24 | |
24 | |
25 | #include <global.h> |
25 | #include <global.h> |
26 | #include <object.h> |
26 | #include <object.h> |
27 | #include <tod.h> |
27 | #include <tod.h> |
28 | |
|
|
29 | #ifdef HAVE_DES_H |
|
|
30 | # include <des.h> |
|
|
31 | #else |
|
|
32 | # ifdef HAVE_CRYPT_H |
|
|
33 | # include <crypt.h> |
|
|
34 | # endif |
|
|
35 | #endif |
|
|
36 | |
28 | |
37 | #include <sproto.h> |
29 | #include <sproto.h> |
38 | #include <time.h> |
30 | #include <time.h> |
39 | |
31 | |
40 | #include <../random_maps/random_map.h> |
32 | #include <../random_maps/random_map.h> |
… | |
… | |
211 | contr->maplevel = newmap->path; |
203 | contr->maplevel = newmap->path; |
212 | contr->count = 0; |
204 | contr->count = 0; |
213 | } |
205 | } |
214 | |
206 | |
215 | /* Update any golems */ |
207 | /* Update any golems */ |
216 | if (type == PLAYER && contr->ranges[range_golem]) |
208 | if (type == PLAYER) |
217 | { |
209 | if (object *golem = contr->golem) |
218 | int i = find_free_spot (contr->ranges[range_golem], newmap, |
|
|
219 | x, y, 1, SIZEOFFREE); |
|
|
220 | |
|
|
221 | if (i == -1) |
|
|
222 | { |
210 | { |
223 | contr->ranges[range_golem]->destroy (); |
211 | int i = find_free_spot (golem, newmap, x, y, 1, SIZEOFFREE); |
224 | contr->ranges[range_golem] = 0; |
212 | |
225 | } |
213 | if (i < 0) |
|
|
214 | golem->destroy (); |
226 | else |
215 | else |
227 | { |
|
|
228 | for (object *tmp = contr->ranges[range_golem]; tmp; tmp = tmp->more) |
|
|
229 | { |
216 | { |
230 | tmp->x = x + freearr_x[i] + (tmp->arch ? 0 : tmp->arch->clone.x); |
217 | newmap->insert (golem, x + freearr_x[i], y + freearr_y[i]); |
231 | tmp->y = y + freearr_y[i] + (tmp->arch ? 0 : tmp->arch->clone.y); |
218 | golem->direction = find_dir_2 (x - golem->x, y - golem->y); |
232 | tmp->map = newmap; |
|
|
233 | } |
219 | } |
234 | |
|
|
235 | insert_ob_in_map (contr->ranges[range_golem], newmap, NULL, 0); |
|
|
236 | contr->ranges[range_golem]->direction = |
|
|
237 | find_dir_2 (x - contr->ranges[range_golem]->x, y - contr->ranges[range_golem]->y); |
|
|
238 | } |
220 | } |
239 | } |
|
|
240 | |
221 | |
241 | /* since the players map is already loaded, we don't need to worry |
222 | /* since the players map is already loaded, we don't need to worry |
242 | * about pending objects. |
223 | * about pending objects. |
243 | */ |
224 | */ |
244 | remove_all_pets (newmap); |
225 | remove_all_pets (newmap); |
… | |
… | |
252 | * is needed after the players have been updated. |
233 | * is needed after the players have been updated. |
253 | */ |
234 | */ |
254 | static void |
235 | static void |
255 | process_players1 () |
236 | process_players1 () |
256 | { |
237 | { |
257 | int flag; |
|
|
258 | |
|
|
259 | /* Basically, we keep looping until all the players have done their actions. */ |
238 | /* Basically, we keep looping until all the players have done their actions. */ |
260 | for (flag = 1; flag != 0;) |
239 | for (int flag = 1; flag != 0;) |
261 | { |
240 | { |
262 | flag = 0; |
241 | flag = 0; |
263 | for_all_players (pl) |
242 | for_all_players (pl) |
264 | { |
243 | { |
265 | pl->refcnt_chk (); |
244 | pl->refcnt_chk (); |
266 | |
245 | |
267 | if (!pl->ob || !pl->ns || !pl->ob->active) |
246 | if (!pl->ob || !pl->ns || !pl->ob->active) |
268 | continue; |
247 | continue; |
269 | |
248 | |
270 | if (pl->ob->speed_left > 0) |
249 | if (pl->ob->speed_left > 0 && pl->ns) |
271 | if (handle_newcs_player (pl->ob)) |
250 | if (handle_newcs_player (pl->ob)) |
272 | flag = 1; |
251 | flag = 1; |
273 | |
252 | |
274 | /* If the player is not actively playing, don't make a |
253 | /* If the player is not actively playing, don't make a |
275 | * backup save - nothing to save anyway. Plus, the |
254 | * backup save - nothing to save anyway. Plus, the |
… | |
… | |
344 | LOG (llevError, "BUG: process_events(): Free object on list\n"); |
323 | LOG (llevError, "BUG: process_events(): Free object on list\n"); |
345 | op->set_speed (0); |
324 | op->set_speed (0); |
346 | continue; |
325 | continue; |
347 | } |
326 | } |
348 | |
327 | |
349 | /* I've seen occasional crashes due to this - the object is removed, |
|
|
350 | * and thus the map it points to (last map it was on) may be bogus |
|
|
351 | * The real bug is to try to find out the cause of this - someone |
|
|
352 | * is probably calling remove_ob without either an insert_ob or |
|
|
353 | * free_object afterwards, leaving an object dangling. But I'd |
|
|
354 | * rather log this and continue on instead of crashing. |
|
|
355 | * Don't remove players - when a player quits, the object is in |
|
|
356 | * sort of a limbo, of removed, but something we want to keep |
|
|
357 | * around. |
|
|
358 | */ |
|
|
359 | if (QUERY_FLAG (op, FLAG_REMOVED) && op->type != PLAYER && op->map && op->map->in_memory != MAP_IN_MEMORY) |
|
|
360 | { |
|
|
361 | LOG (llevError, "BUG: process_events(): Removed object on list\n"); |
|
|
362 | char *dump = dump_object (op); |
|
|
363 | LOG (llevError, dump); |
|
|
364 | free (dump); |
|
|
365 | op->destroy (); |
|
|
366 | continue; |
|
|
367 | } |
|
|
368 | |
|
|
369 | if (!op->has_active_speed ()) |
328 | if (!op->has_active_speed ()) |
370 | { |
329 | { |
371 | LOG (llevError, "BUG: process_events(): Object %s has no speed (%f), " |
330 | LOG (llevError, "BUG: process_events(): Object %s has no speed (%f), " |
372 | "but is on active list\n", op->debug_desc (), op->speed); |
331 | "but is on active list\n", op->debug_desc (), op->speed); |
373 | op->set_speed (0); |
332 | op->set_speed (0); |
374 | continue; |
333 | continue; |
375 | } |
334 | } |
376 | |
335 | |
377 | if (op->map == NULL && op->env == NULL && op->name && op->type != MAP) |
336 | if (op->flag [FLAG_REMOVED]) |
378 | { |
337 | { |
379 | LOG (llevError, "BUG: process_events(): Object without map or " |
338 | LOG (llevError, "BUG: process_events(): removed object is on active list: %s\n", |
380 | "inventory is on active list: %s (%d)\n", &op->name, op->count); |
339 | op->debug_desc ()); |
381 | op->set_speed (0); |
340 | op->set_speed (0); |
382 | continue; |
341 | continue; |
383 | } |
342 | } |
384 | |
343 | |
385 | /* Animate the object. Bug or feature that anim_speed |
344 | /* Animate the object. Bug or feature that anim_speed |
386 | * is based on ticks, and not the creatures speed? |
345 | * is based on ticks, and not the creatures speed? |
387 | */ |
346 | */ |
388 | if (op->anim_speed && op->last_anim >= op->anim_speed) |
347 | if (op->anim_speed && op->last_anim >= op->anim_speed) |
389 | { |
348 | { |
390 | if ((op->type == PLAYER)) |
349 | animate_object (op, op->type == PLAYER ? op->facing : op->direction); |
391 | animate_object (op, op->facing); |
|
|
392 | else |
|
|
393 | animate_object (op, op->direction); |
|
|
394 | |
350 | |
395 | op->last_anim = 1; |
351 | op->last_anim = 1; |
396 | } |
352 | } |
397 | else |
353 | else |
398 | op->last_anim++; |
354 | op->last_anim++; |
… | |
… | |
449 | cleanup_inform (const char *cause, bool make_core) |
405 | cleanup_inform (const char *cause, bool make_core) |
450 | { |
406 | { |
451 | int flags = NDI_UNIQUE | NDI_ALL | (make_core ? NDI_RED : NDI_GREEN); |
407 | int flags = NDI_UNIQUE | NDI_ALL | (make_core ? NDI_RED : NDI_GREEN); |
452 | |
408 | |
453 | new_draw_info_format (flags, 0, 0, "The server will now shutdown.\n"); |
409 | new_draw_info_format (flags, 0, 0, "The server will now shutdown.\n"); |
454 | new_draw_info_format (flags, 0, 0, "Cause for this shtudown: %s\n", cause); |
410 | new_draw_info_format (flags, 0, 0, "Cause for this shutdown: %s\n", cause); |
455 | |
411 | |
456 | if (make_core) |
412 | if (make_core) |
457 | new_draw_info_format (flags, 0, 0, "This is considered a crash.\n"); |
413 | new_draw_info_format (flags, 0, 0, "This is considered a crash, but all maps and players have been saved.\n"); |
458 | else |
414 | else |
459 | new_draw_info_format (flags, 0, 0, "This is considered to be a clean shutdown.\n"); |
415 | new_draw_info_format (flags, 0, 0, "This is considered to be a clean shutdown, and all maps and players will be saved now.\n"); |
460 | |
416 | |
461 | new_draw_info_format (flags, 0, 0, "%s\n", CLEANUP_MESSAGE); |
417 | new_draw_info_format (flags, 0, 0, "%s\n", CLEANUP_MESSAGE); |
462 | |
418 | |
463 | flush_sockets (); |
419 | flush_sockets (); |
464 | } |
420 | } |
… | |
… | |
491 | LOG (llevDebug, "cleanup done.\n"); |
447 | LOG (llevDebug, "cleanup done.\n"); |
492 | |
448 | |
493 | if (make_core) |
449 | if (make_core) |
494 | { |
450 | { |
495 | cleanup_inform (cause, make_core); |
451 | cleanup_inform (cause, make_core); |
|
|
452 | cfperl_cleanup (make_core); |
496 | _exit (1); |
453 | _exit (1); |
497 | } |
454 | } |
498 | else |
455 | else |
|
|
456 | { |
|
|
457 | new_draw_info_format (NDI_UNIQUE | NDI_ALL | NDI_GREEN, 0, 0, "Maps and players successfully saved, exiting.\n"); |
|
|
458 | new_draw_info_format (NDI_UNIQUE | NDI_ALL | NDI_GREEN, 0, 0, "And again: " CLEANUP_MESSAGE "\n"); |
|
|
459 | flush_sockets (); |
|
|
460 | cfperl_cleanup (make_core); |
499 | _exit (0); |
461 | _exit (0); |
|
|
462 | } |
500 | } |
463 | } |
501 | |
464 | |
502 | int |
465 | int |
503 | forbid_play (void) |
466 | forbid_play (void) |
504 | { |
467 | { |
… | |
… | |
560 | * done on the same tick, but that will happen very infrequently |
523 | * done on the same tick, but that will happen very infrequently |
561 | * |
524 | * |
562 | * I also think this code makes it easier to see how often we really are |
525 | * I also think this code makes it easier to see how often we really are |
563 | * doing the various things. |
526 | * doing the various things. |
564 | */ |
527 | */ |
565 | |
|
|
566 | extern unsigned long todtick; |
|
|
567 | |
|
|
568 | void |
528 | void |
569 | do_specials (void) |
529 | do_specials (void) |
570 | { |
530 | { |
571 | if (!(pticks % PTICKS_PER_CLOCK)) |
531 | if (!(pticks % TICKS_PER_HOUR)) |
572 | tick_the_clock (); |
532 | adjust_daylight (); |
573 | |
533 | |
574 | if (!(pticks % 7)) |
534 | if (!(pticks % 7)) |
575 | shstr::gc (); |
535 | shstr::gc (); |
576 | |
536 | |
577 | if (!(pticks % 2503)) |
537 | if (!(pticks % 2503)) |
… | |
… | |
604 | attachable::check_mortals (); |
564 | attachable::check_mortals (); |
605 | |
565 | |
606 | ++pticks; |
566 | ++pticks; |
607 | } |
567 | } |
608 | |
568 | |
|
|
569 | #if 0 |
|
|
570 | // used fro benchmarking (x86/amd64-specific) |
|
|
571 | typedef unsigned long tval; |
|
|
572 | typedef unsigned long long stamp64; |
|
|
573 | |
|
|
574 | extern inline tval |
|
|
575 | stamp (void) |
|
|
576 | { |
|
|
577 | tval tsc; |
|
|
578 | asm volatile ("rdtsc":"=a" (tsc)::"edx"); |
|
|
579 | |
|
|
580 | return tsc; |
|
|
581 | } |
|
|
582 | |
|
|
583 | extern inline tval |
|
|
584 | measure (tval t) |
|
|
585 | { |
|
|
586 | tval tsc; |
|
|
587 | asm volatile ("rdtsc":"=a" (tsc)::"edx"); |
|
|
588 | |
|
|
589 | if (tsc > t) |
|
|
590 | return tsc - t; |
|
|
591 | else |
|
|
592 | return t - tsc; |
|
|
593 | } |
|
|
594 | |
609 | int |
595 | int |
610 | main (int argc, char **argv) |
596 | main (int argc, char **argv) |
611 | { |
597 | { |
|
|
598 | rand_gen rg(0); |
|
|
599 | tval fastest = 0x7fffffff; |
|
|
600 | for (int loop = 10000; loop--; ) |
|
|
601 | { |
|
|
602 | tval s = stamp (); |
|
|
603 | volatile int i = rg.get_int(25); |
|
|
604 | fastest = min (fastest, measure (s)); |
|
|
605 | } |
|
|
606 | |
|
|
607 | //printf ("fastest %d\n", fastest); |
|
|
608 | for (int i = 0; i < 1024*1024*3; ++i) |
|
|
609 | { |
|
|
610 | char c = rg.get_int (256); |
|
|
611 | write (2, &c, 1); |
|
|
612 | } |
|
|
613 | } |
|
|
614 | |
|
|
615 | #else |
|
|
616 | |
|
|
617 | // normal main |
|
|
618 | int |
|
|
619 | main (int argc, char **argv) |
|
|
620 | { |
612 | settings.argc = argc; |
621 | settings.argc = argc; |
613 | settings.argv = argv; |
622 | settings.argv = argv; |
614 | |
623 | |
615 | init (argc, argv); |
624 | init (argc, argv); |
616 | |
625 | |
617 | initPlugins (); |
626 | initPlugins (); |
618 | |
627 | |
619 | for (;;) |
628 | for (;;) |
620 | cfperl_main (); |
629 | cfperl_main (); |
621 | } |
630 | } |
|
|
631 | #endif |
622 | |
632 | |