1 | /* |
1 | /* |
2 | * static char *rcsid_main_c = |
2 | * static char *rcsid_main_c = |
3 | * "$Id: main.C,v 1.3 2006/08/17 20:23:31 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 | |
… | |
… | |
869 | } |
869 | } |
870 | |
870 | |
871 | /* |
871 | /* |
872 | * process_active_maps(): Works like process_events(), but it only |
872 | * process_active_maps(): Works like process_events(), but it only |
873 | * processes maps which a player is on. |
873 | * processes maps which a player is on. |
874 | * It will check that it isn't called too often, and abort |
|
|
875 | * if time since last call is less than MAX_TIME. |
|
|
876 | * |
874 | * |
877 | */ |
875 | */ |
878 | |
876 | |
|
|
877 | #if 0 // dead code, schmorp |
879 | void process_active_maps(void) { |
878 | void process_active_maps () |
880 | mapstruct *map; |
879 | { |
881 | |
|
|
882 | |
|
|
883 | /* |
|
|
884 | * If enough time has elapsed, do some work. |
|
|
885 | */ |
|
|
886 | if(enough_elapsed_time()) { |
|
|
887 | for(map=first_map;map!=NULL;map=map->next) { |
880 | for (mapstruct *map = first_map; map != NULL; map = map->next) |
888 | if(map->in_memory == MAP_IN_MEMORY) { |
881 | if (map->in_memory == MAP_IN_MEMORY) |
889 | if(players_on_map(map,TRUE)) |
882 | if (players_on_map (map, TRUE)) |
890 | process_events(map); |
883 | process_events (map); |
891 | } |
|
|
892 | } |
|
|
893 | } |
|
|
894 | } |
884 | } |
|
|
885 | #endif |
895 | |
886 | |
896 | /* process_players1 and process_players2 do all the player related stuff. |
887 | /* process_players1 and process_players2 do all the player related stuff. |
897 | * 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 |
898 | * 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 |
899 | * by the various functions. process_players1() does the processing before |
890 | * by the various functions. process_players1() does the processing before |
… | |
… | |
1313 | |
1304 | |
1314 | if (!(pticks % 12503)) |
1305 | if (!(pticks % 12503)) |
1315 | fix_luck(); |
1306 | fix_luck(); |
1316 | } |
1307 | } |
1317 | |
1308 | |
1318 | /* |
1309 | void server_tick () |
1319 | * sleep_delta checks how much time has elapsed since last tick. |
|
|
1320 | * If it is less than max_time, the remaining time is slept with select(). |
|
|
1321 | */ |
|
|
1322 | |
|
|
1323 | void |
|
|
1324 | sleep_delta(void) |
|
|
1325 | { |
1310 | { |
1326 | extern long max_time; |
1311 | nroferrors = 0; |
1327 | extern struct timeval last_time; |
|
|
1328 | extern long process_utime_long_count; |
|
|
1329 | extern void cfperl_sleep (double delta); |
|
|
1330 | |
1312 | |
1331 | struct timeval new_time; |
1313 | doeric_server(); |
1332 | 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. */ |
1333 | |
1321 | |
1334 | long sleep_sec = last_time.tv_sec - new_time.tv_sec; |
1322 | ++pticks; |
1335 | long sleep_usec = max_time - (new_time.tv_usec - last_time.tv_usec); |
|
|
1336 | |
|
|
1337 | /* This is very ugly, but probably the fastest for our use: */ |
|
|
1338 | while (sleep_usec < 0) { |
|
|
1339 | sleep_usec += 1000000; |
|
|
1340 | sleep_sec -= 1; |
|
|
1341 | } |
|
|
1342 | while (sleep_usec > 1000000) { |
|
|
1343 | sleep_usec -= 1000000; |
|
|
1344 | sleep_sec += 1; |
|
|
1345 | } |
|
|
1346 | |
|
|
1347 | log_time((new_time.tv_sec - last_time.tv_sec)*1000000 |
|
|
1348 | + new_time.tv_usec - last_time.tv_usec); |
|
|
1349 | |
|
|
1350 | /* |
|
|
1351 | * Set last_time to when we're expected to wake up: |
|
|
1352 | */ |
|
|
1353 | last_time.tv_usec += max_time; |
|
|
1354 | while (last_time.tv_usec > 1000000) { |
|
|
1355 | last_time.tv_usec -= 1000000; |
|
|
1356 | last_time.tv_sec++; |
|
|
1357 | } |
|
|
1358 | |
|
|
1359 | /* |
|
|
1360 | * Don't do too much catching up: |
|
|
1361 | * (Things can still get jerky on a slow/loaded computer) |
|
|
1362 | */ |
|
|
1363 | if (last_time.tv_sec * 1000000 + last_time.tv_usec < |
|
|
1364 | new_time.tv_sec * 1000000 + new_time.tv_usec) |
|
|
1365 | { |
|
|
1366 | last_time.tv_sec = new_time.tv_sec; |
|
|
1367 | last_time.tv_usec = new_time.tv_usec; |
|
|
1368 | } |
|
|
1369 | |
|
|
1370 | if (sleep_sec >= 0 && sleep_usec > 0) |
|
|
1371 | cfperl_sleep (sleep_sec + sleep_usec * 1e-6); |
|
|
1372 | else |
|
|
1373 | process_utime_long_count++; |
|
|
1374 | } |
1323 | } |
1375 | |
1324 | |
1376 | int main(int argc, char **argv) |
1325 | int main(int argc, char **argv) |
1377 | { |
1326 | { |
1378 | #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 */ |
… | |
… | |
1386 | |
1335 | |
1387 | settings.argc=argc; |
1336 | settings.argc=argc; |
1388 | settings.argv=argv; |
1337 | settings.argv=argv; |
1389 | init(argc, argv); |
1338 | init(argc, argv); |
1390 | initPlugins(); /* GROS - Init the Plugins */ |
1339 | initPlugins(); /* GROS - Init the Plugins */ |
1391 | #ifdef WIN32 |
|
|
1392 | while ( bRunning ) |
|
|
1393 | { |
|
|
1394 | #else |
|
|
1395 | for(;;) { |
|
|
1396 | #endif |
|
|
1397 | nroferrors = 0; |
|
|
1398 | |
1340 | |
1399 | doeric_server(); |
1341 | cfperl_main (); |
1400 | process_events(NULL); /* "do" something with objects with speed */ |
|
|
1401 | cftimer_process_timers();/* Process the crossfire Timers */ |
|
|
1402 | /* Lauwenmark : Here we handle the CLOCK global event */ |
|
|
1403 | execute_global_event(EVENT_CLOCK); |
|
|
1404 | flush_sockets(); |
|
|
1405 | check_active_maps(); /* Removes unused maps after a certain timeout */ |
|
|
1406 | do_specials(); /* Routines called from time to time. */ |
|
|
1407 | |
1342 | |
1408 | sleep_delta(); /* Slepp proper amount of time before next tick */ |
|
|
1409 | } |
|
|
1410 | emergency_save( 0 ); |
1343 | emergency_save( 0 ); |
1411 | cleanup( ); |
1344 | cleanup( ); |
1412 | return 0; |
1345 | return 0; |
1413 | } |
1346 | } |