1 | /* |
1 | /* |
2 | * static char *rcsid_main_c = |
2 | * static char *rcsid_main_c = |
3 | * "$Id: main.C,v 1.2 2006/08/15 18:07:25 root Exp $"; |
3 | * "$Id: main.C,v 1.5 2006/08/24 17:29:30 root Exp $"; |
4 | */ |
4 | */ |
5 | |
5 | |
6 | /* |
6 | /* |
7 | CrossFire, A Multiplayer game for X-windows |
7 | CrossFire, A Multiplayer game for X-windows |
8 | |
8 | |
… | |
… | |
356 | if (oldmap->players <= 0) /* can be less than zero due to errors in tracking this */ |
356 | if (oldmap->players <= 0) /* can be less than zero due to errors in tracking this */ |
357 | set_map_timeout(oldmap); |
357 | set_map_timeout(oldmap); |
358 | } |
358 | } |
359 | } |
359 | } |
360 | swap_below_max (newmap->path); |
360 | swap_below_max (newmap->path); |
361 | |
|
|
362 | if( op->type == PLAYER) |
|
|
363 | MapNewmapCmd( op->contr); |
|
|
364 | } |
361 | } |
365 | |
362 | |
366 | void set_map_timeout(mapstruct *oldmap) |
363 | void set_map_timeout(mapstruct *oldmap) |
367 | { |
364 | { |
368 | #if MAP_MAXTIMEOUT |
365 | #if MAP_MAXTIMEOUT |
… | |
… | |
872 | } |
869 | } |
873 | |
870 | |
874 | /* |
871 | /* |
875 | * process_active_maps(): Works like process_events(), but it only |
872 | * process_active_maps(): Works like process_events(), but it only |
876 | * processes maps which a player is on. |
873 | * processes maps which a player is on. |
877 | * It will check that it isn't called too often, and abort |
|
|
878 | * if time since last call is less than MAX_TIME. |
|
|
879 | * |
874 | * |
880 | */ |
875 | */ |
881 | |
876 | |
|
|
877 | #if 0 // dead code, schmorp |
882 | void process_active_maps(void) { |
878 | void process_active_maps () |
883 | mapstruct *map; |
879 | { |
884 | |
|
|
885 | |
|
|
886 | /* |
|
|
887 | * If enough time has elapsed, do some work. |
|
|
888 | */ |
|
|
889 | if(enough_elapsed_time()) { |
|
|
890 | for(map=first_map;map!=NULL;map=map->next) { |
880 | for (mapstruct *map = first_map; map != NULL; map = map->next) |
891 | if(map->in_memory == MAP_IN_MEMORY) { |
881 | if (map->in_memory == MAP_IN_MEMORY) |
892 | if(players_on_map(map,TRUE)) |
882 | if (players_on_map (map, TRUE)) |
893 | process_events(map); |
883 | process_events (map); |
894 | } |
|
|
895 | } |
|
|
896 | } |
|
|
897 | } |
884 | } |
|
|
885 | #endif |
898 | |
886 | |
899 | /* process_players1 and process_players2 do all the player related stuff. |
887 | /* process_players1 and process_players2 do all the player related stuff. |
900 | * I moved it out of process events and process_map. This was to some |
888 | * I moved it out of process events and process_map. This was to some |
901 | * extent for debugging as well as to get a better idea of the time used |
889 | * extent for debugging as well as to get a better idea of the time used |
902 | * by the various functions. process_players1() does the processing before |
890 | * by the various functions. process_players1() does the processing before |
… | |
… | |
1316 | |
1304 | |
1317 | if (!(pticks % 12503)) |
1305 | if (!(pticks % 12503)) |
1318 | fix_luck(); |
1306 | fix_luck(); |
1319 | } |
1307 | } |
1320 | |
1308 | |
1321 | /* |
1309 | void server_tick () |
1322 | * sleep_delta checks how much time has elapsed since last tick. |
|
|
1323 | * If it is less than max_time, the remaining time is slept with select(). |
|
|
1324 | */ |
|
|
1325 | |
|
|
1326 | void |
|
|
1327 | sleep_delta(void) |
|
|
1328 | { |
1310 | { |
1329 | extern long max_time; |
1311 | nroferrors = 0; |
1330 | extern struct timeval last_time; |
|
|
1331 | extern long process_utime_long_count; |
|
|
1332 | extern void cfperl_sleep (double delta); |
|
|
1333 | |
1312 | |
1334 | struct timeval new_time; |
1313 | doeric_server(); |
1335 | GETTIMEOFDAY (&new_time); |
1314 | process_events(NULL); /* "do" something with objects with speed */ |
|
|
1315 | cftimer_process_timers();/* Process the crossfire Timers */ |
|
|
1316 | /* Lauwenmark : Here we handle the CLOCK global event */ |
|
|
1317 | execute_global_event(EVENT_CLOCK); |
|
|
1318 | flush_sockets(); |
|
|
1319 | check_active_maps(); /* Removes unused maps after a certain timeout */ |
|
|
1320 | do_specials(); /* Routines called from time to time. */ |
1336 | |
1321 | |
1337 | long sleep_sec = last_time.tv_sec - new_time.tv_sec; |
1322 | ++pticks; |
1338 | long sleep_usec = max_time - (new_time.tv_usec - last_time.tv_usec); |
|
|
1339 | |
|
|
1340 | /* This is very ugly, but probably the fastest for our use: */ |
|
|
1341 | while (sleep_usec < 0) { |
|
|
1342 | sleep_usec += 1000000; |
|
|
1343 | sleep_sec -= 1; |
|
|
1344 | } |
|
|
1345 | while (sleep_usec > 1000000) { |
|
|
1346 | sleep_usec -= 1000000; |
|
|
1347 | sleep_sec += 1; |
|
|
1348 | } |
|
|
1349 | |
|
|
1350 | log_time((new_time.tv_sec - last_time.tv_sec)*1000000 |
|
|
1351 | + new_time.tv_usec - last_time.tv_usec); |
|
|
1352 | |
|
|
1353 | /* |
|
|
1354 | * Set last_time to when we're expected to wake up: |
|
|
1355 | */ |
|
|
1356 | last_time.tv_usec += max_time; |
|
|
1357 | while (last_time.tv_usec > 1000000) { |
|
|
1358 | last_time.tv_usec -= 1000000; |
|
|
1359 | last_time.tv_sec++; |
|
|
1360 | } |
|
|
1361 | |
|
|
1362 | /* |
|
|
1363 | * Don't do too much catching up: |
|
|
1364 | * (Things can still get jerky on a slow/loaded computer) |
|
|
1365 | */ |
|
|
1366 | if (last_time.tv_sec * 1000000 + last_time.tv_usec < |
|
|
1367 | new_time.tv_sec * 1000000 + new_time.tv_usec) |
|
|
1368 | { |
|
|
1369 | last_time.tv_sec = new_time.tv_sec; |
|
|
1370 | last_time.tv_usec = new_time.tv_usec; |
|
|
1371 | } |
|
|
1372 | |
|
|
1373 | if (sleep_sec >= 0 && sleep_usec > 0) |
|
|
1374 | cfperl_sleep (sleep_sec + sleep_usec * 1e-6); |
|
|
1375 | else |
|
|
1376 | process_utime_long_count++; |
|
|
1377 | } |
1323 | } |
1378 | |
1324 | |
1379 | int main(int argc, char **argv) |
1325 | int main(int argc, char **argv) |
1380 | { |
1326 | { |
1381 | #ifdef WIN32 /* ---win32 this sets the win32 from 0d0a to 0a handling */ |
1327 | #ifdef WIN32 /* ---win32 this sets the win32 from 0d0a to 0a handling */ |
… | |
… | |
1389 | |
1335 | |
1390 | settings.argc=argc; |
1336 | settings.argc=argc; |
1391 | settings.argv=argv; |
1337 | settings.argv=argv; |
1392 | init(argc, argv); |
1338 | init(argc, argv); |
1393 | initPlugins(); /* GROS - Init the Plugins */ |
1339 | initPlugins(); /* GROS - Init the Plugins */ |
1394 | #ifdef WIN32 |
|
|
1395 | while ( bRunning ) |
|
|
1396 | { |
|
|
1397 | #else |
|
|
1398 | for(;;) { |
|
|
1399 | #endif |
|
|
1400 | nroferrors = 0; |
|
|
1401 | |
1340 | |
1402 | doeric_server(); |
1341 | cfperl_main (); |
1403 | process_events(NULL); /* "do" something with objects with speed */ |
|
|
1404 | cftimer_process_timers();/* Process the crossfire Timers */ |
|
|
1405 | /* Lauwenmark : Here we handle the CLOCK global event */ |
|
|
1406 | execute_global_event(EVENT_CLOCK); |
|
|
1407 | flush_sockets(); |
|
|
1408 | check_active_maps(); /* Removes unused maps after a certain timeout */ |
|
|
1409 | do_specials(); /* Routines called from time to time. */ |
|
|
1410 | |
1342 | |
1411 | sleep_delta(); /* Slepp proper amount of time before next tick */ |
|
|
1412 | } |
|
|
1413 | emergency_save( 0 ); |
1343 | emergency_save( 0 ); |
1414 | cleanup( ); |
1344 | cleanup( ); |
1415 | return 0; |
1345 | return 0; |
1416 | } |
1346 | } |