ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/server/main.C
(Generate patch)

Comparing deliantra/server/server/main.C (file contents):
Revision 1.85 by root, Mon Jan 15 02:39:41 2007 UTC vs.
Revision 1.104 by root, Thu May 17 14:14:55 2007 UTC

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 */
254static void 235static void
255process_players1 () 236process_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.f && 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
282 } /* end of for loop for all the players */ 261 } /* end of for loop for all the players */
283 } /* for flag */ 262 } /* for flag */
284 263
285 for_all_players (pl) 264 for_all_players (pl)
286 { 265 {
266 object *ob = pl->ob;
267
287 if (!pl->ob || !pl->ns || !pl->ob->active) 268 if (!ob || !pl->ns || !ob->active)
288 continue; 269 continue;
289 270
290 if (settings.casting_time) 271 // process_events might steal us one speed slot, so adjust accordingly
291 { 272 if (ob->speed_left > 0.f)
292 if (pl->ob->casting_time > 0) 273 ++ob->speed_left;
293 {
294 pl->ob->casting_time--;
295 pl->ob->start_holding = 1;
296 }
297 274
298 /* set spell_state so we can update the range in stats field */
299 if ((pl->ob->casting_time == 0) && (pl->ob->start_holding == 1))
300 pl->ob->start_holding = 0;
301 }
302
303 do_some_living (pl->ob); 275 do_some_living (ob);
304 } 276 }
305} 277}
306 278
307static void 279static void
308process_players2 () 280process_players2 ()
309{ 281{
310 /* Then check if any players should use weapon-speed instead of speed */ 282 /* Then check if any players should use weapon-speed instead of speed */
311 for_all_players (pl) 283 for_all_players (pl)
312 { 284 {
313 /* The code that did weapon_sp handling here was out of place -
314 * this isn't called until after the player has finished there
315 * actions, and is thus out of place. All we do here is bounds
316 * checking.
317 */
318 if (pl->has_hit) 285 if (pl->has_hit)
319 { 286 {
287 pl->ob->speed_left = min (pl->weapon_speed (), pl->ob->speed_left + pl->weapon_speed ());
288
320 if (pl->ob->speed_left > pl->weapon_sp) 289 if (pl->ob->speed_left > 0.f)
321 pl->ob->speed_left = pl->weapon_sp;
322
323 /* This needs to be here - if the player is running, we need to
324 * clear this each tick, but new commands are not being received
325 * so execute_newserver_command() is never called
326 */
327 pl->has_hit = 0; 290 pl->has_hit = 0;
328 } 291 }
329 else if (pl->ob->speed_left > pl->ob->speed) 292 else
330 pl->ob->speed_left = pl->ob->speed; 293 pl->ob->speed_left = min (pl->ob->speed, pl->ob->speed_left + pl->ob->speed);
331 } 294 }
332} 295}
333 296
334void 297void
335process_events () 298process_events ()
341 /* Now process op */ 304 /* Now process op */
342 if (QUERY_FLAG (op, FLAG_FREED)) 305 if (QUERY_FLAG (op, FLAG_FREED))
343 { 306 {
344 LOG (llevError, "BUG: process_events(): Free object on list\n"); 307 LOG (llevError, "BUG: process_events(): Free object on list\n");
345 op->set_speed (0); 308 op->set_speed (0);
346 continue;
347 }
348
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; 309 continue;
367 } 310 }
368 311
369 if (!op->has_active_speed ()) 312 if (!op->has_active_speed ())
370 { 313 {
372 "but is on active list\n", op->debug_desc (), op->speed); 315 "but is on active list\n", op->debug_desc (), op->speed);
373 op->set_speed (0); 316 op->set_speed (0);
374 continue; 317 continue;
375 } 318 }
376 319
377 if (op->map == NULL && op->env == NULL && op->name && op->type != MAP) 320 if (op->flag [FLAG_REMOVED])
378 { 321 {
379 LOG (llevError, "BUG: process_events(): Object without map or " 322 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); 323 op->debug_desc ());
381 op->set_speed (0); 324 op->set_speed (0);
382 continue; 325 continue;
383 } 326 }
384 327
385 /* Animate the object. Bug or feature that anim_speed 328 /* Animate the object. Bug or feature that anim_speed
386 * is based on ticks, and not the creatures speed? 329 * is based on ticks, and not the creatures speed?
387 */ 330 */
388 if (op->anim_speed && op->last_anim >= op->anim_speed) 331 if (op->anim_speed && op->last_anim >= op->anim_speed)
389 { 332 {
390 if ((op->type == PLAYER))
391 animate_object (op, op->facing);
392 else
393 animate_object (op, op->direction); 333 animate_object (op, op->contr ? op->facing : op->direction);
394
395 op->last_anim = 1; 334 op->last_anim = 1;
396 } 335 }
397 else 336 else
398 op->last_anim++; 337 ++op->last_anim;
399 338
400 if (op->speed_left > 0) 339 if (op->speed_left > 0.f)
401 {
402#if 0
403 /* I've seen occasional crashes in move_symptom() with it
404 * crashing because op is removed - add some debugging to
405 * track if it is removed at this point.
406 * This unfortunately is a bit too verbose it seems - not sure
407 * why - I think what happens is a map is freed or something and
408 * some objects get 'lost' - removed never to be reclaimed.
409 * removed objects generally shouldn't exist.
410 */
411 if (QUERY_FLAG (op, FLAG_REMOVED))
412 { 340 {
413 LOG (llevDebug, "process_events: calling process_object with removed object %s\n", op->name ? op->name : "null");
414 }
415#endif
416 --op->speed_left; 341 --op->speed_left;
417 process_object (op); 342 process_object (op);
418 343
419 if (op->destroyed ()) 344 if (op->destroyed ())
420 continue; 345 continue;
421 } 346 }
422 347
423 if (settings.casting_time == TRUE && op->casting_time > 0) 348 if (!op->contr)
424 op->casting_time--; 349 op->speed_left = min (abs (op->speed), op->speed_left + abs (op->speed));
425
426 if (op->speed_left <= 0)
427 op->speed_left += FABS (op->speed);
428 } 350 }
429 351
430 process_players2 (); 352 process_players2 ();
431} 353}
432 354
449cleanup_inform (const char *cause, bool make_core) 371cleanup_inform (const char *cause, bool make_core)
450{ 372{
451 int flags = NDI_UNIQUE | NDI_ALL | (make_core ? NDI_RED : NDI_GREEN); 373 int flags = NDI_UNIQUE | NDI_ALL | (make_core ? NDI_RED : NDI_GREEN);
452 374
453 new_draw_info_format (flags, 0, 0, "The server will now shutdown.\n"); 375 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); 376 new_draw_info_format (flags, 0, 0, "Cause for this shutdown: %s\n", cause);
455 377
456 if (make_core) 378 if (make_core)
457 new_draw_info_format (flags, 0, 0, "This is considered a crash.\n"); 379 new_draw_info_format (flags, 0, 0, "This is considered a crash, but all maps and players have been saved.\n");
458 else 380 else
459 new_draw_info_format (flags, 0, 0, "This is considered to be a clean shutdown.\n"); 381 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 382
461 new_draw_info_format (flags, 0, 0, "%s\n", CLEANUP_MESSAGE); 383 new_draw_info_format (flags, 0, 0, "%s\n", CLEANUP_MESSAGE);
462 384
463 flush_sockets (); 385 flush_sockets ();
464} 386}
491 LOG (llevDebug, "cleanup done.\n"); 413 LOG (llevDebug, "cleanup done.\n");
492 414
493 if (make_core) 415 if (make_core)
494 { 416 {
495 cleanup_inform (cause, make_core); 417 cleanup_inform (cause, make_core);
418 cfperl_cleanup (make_core);
496 _exit (1); 419 _exit (1);
497 } 420 }
498 else 421 else
422 {
423 new_draw_info_format (NDI_UNIQUE | NDI_ALL | NDI_GREEN, 0, 0, "Maps and players successfully saved, exiting.\n");
424 new_draw_info_format (NDI_UNIQUE | NDI_ALL | NDI_GREEN, 0, 0, "And again: " CLEANUP_MESSAGE "\n");
425 flush_sockets ();
426 cfperl_cleanup (make_core);
499 _exit (0); 427 _exit (0);
428 }
500} 429}
501 430
502int 431int
503forbid_play (void) 432forbid_play (void)
504{ 433{
560 * done on the same tick, but that will happen very infrequently 489 * done on the same tick, but that will happen very infrequently
561 * 490 *
562 * I also think this code makes it easier to see how often we really are 491 * I also think this code makes it easier to see how often we really are
563 * doing the various things. 492 * doing the various things.
564 */ 493 */
565
566extern unsigned long todtick;
567
568void 494void
569do_specials (void) 495do_specials (void)
570{ 496{
571 if (!(pticks % PTICKS_PER_CLOCK)) 497 if (!(pticks % TICKS_PER_HOUR))
572 tick_the_clock (); 498 adjust_daylight ();
573 499
574 if (!(pticks % 7)) 500 if (!(pticks % 7))
575 shstr::gc (); 501 shstr::gc ();
576 502
577 if (!(pticks % 2503)) 503 if (!(pticks % 2503))
604 attachable::check_mortals (); 530 attachable::check_mortals ();
605 531
606 ++pticks; 532 ++pticks;
607} 533}
608 534
535#if 0
536// used fro benchmarking (x86/amd64-specific)
537typedef unsigned long tval;
538typedef unsigned long long stamp64;
539
540extern inline tval
541stamp (void)
542{
543 tval tsc;
544 asm volatile ("rdtsc":"=a" (tsc)::"edx");
545
546 return tsc;
547}
548
549extern inline tval
550measure (tval t)
551{
552 tval tsc;
553 asm volatile ("rdtsc":"=a" (tsc)::"edx");
554
555 if (tsc > t)
556 return tsc - t;
557 else
558 return t - tsc;
559}
560
609int 561int
610main (int argc, char **argv) 562main (int argc, char **argv)
611{ 563{
564 rand_gen rg(0);
565 tval fastest = 0x7fffffff;
566 for (int loop = 10000; loop--; )
567 {
568 tval s = stamp ();
569 volatile int i = rg.get_int(25);
570 fastest = min (fastest, measure (s));
571 }
572
573 //printf ("fastest %d\n", fastest);
574 for (int i = 0; i < 1024*1024*3; ++i)
575 {
576 char c = rg.get_int (256);
577 write (2, &c, 1);
578 }
579}
580
581#else
582
583// normal main
584int
585main (int argc, char **argv)
586{
612 settings.argc = argc; 587 settings.argc = argc;
613 settings.argv = argv; 588 settings.argv = argv;
614 589
615 init (argc, argv); 590 init (argc, argv);
616 591
617 initPlugins (); 592 initPlugins ();
618 593
619 for (;;) 594 for (;;)
620 cfperl_main (); 595 cfperl_main ();
621} 596}
597#endif
622 598

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines