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.13 2006/08/29 07:34:00 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 | |
… | |
… | |
280 | LOG(llevInfo,"enter_map: Could not find free spot for player - will dump on top of object (%s: %d, %d)\n", |
280 | LOG(llevInfo,"enter_map: Could not find free spot for player - will dump on top of object (%s: %d, %d)\n", |
281 | newmap->path, x , y); |
281 | newmap->path, x , y); |
282 | } |
282 | } |
283 | } /* end if looking for free spot */ |
283 | } /* end if looking for free spot */ |
284 | |
284 | |
|
|
285 | if (op->map!=NULL) |
|
|
286 | { |
|
|
287 | INVOKE_PLAYER (MAP_CHANGE, op->contr, ARG_MAP (op->map), ARG_MAP (newmap)); |
|
|
288 | INVOKE_MAP (LEAVE, op->map, ARG_PLAYER (op->contr)); |
|
|
289 | } |
285 | |
290 | |
286 | /* If it is a player login, he has yet to be inserted anyplace. |
291 | /* If it is a player login, he has yet to be inserted anyplace. |
287 | * otherwise, we need to deal with removing the playe here. |
292 | * otherwise, we need to deal with removing the player here. |
288 | */ |
293 | */ |
289 | if(!QUERY_FLAG(op, FLAG_REMOVED)) |
294 | if(!QUERY_FLAG(op, FLAG_REMOVED)) |
290 | remove_ob(op); |
295 | remove_ob(op); |
291 | if (op->map!=NULL) |
296 | |
292 | { |
|
|
293 | /* Lauwenmark : Here we handle the MAPLEAVE global event */ |
|
|
294 | execute_global_event(EVENT_MAPLEAVE, op); |
|
|
295 | } |
|
|
296 | /* remove_ob clears these so they must be reset after the remove_ob call */ |
297 | /* remove_ob clears these so they must be reset after the remove_ob call */ |
297 | op->x = x; |
298 | op->x = x; |
298 | op->y = y; |
299 | op->y = y; |
299 | op->map = newmap; |
300 | op->map = newmap; |
300 | insert_ob_in_map(op,op->map,NULL,INS_NO_WALK_ON); |
301 | insert_ob_in_map(op,op->map,NULL,INS_NO_WALK_ON); |
301 | |
302 | |
302 | /* Lauwenmark : Here we handle the MAPENTER global event */ |
303 | INVOKE_MAP (ENTER, op->map, ARG_PLAYER (op->contr)); |
303 | execute_global_event(EVENT_MAPENTER, op); |
|
|
304 | |
304 | |
305 | if (!op->contr->hidden) |
305 | if (!op->contr->hidden) |
306 | newmap->players++; |
306 | newmap->players++; |
307 | |
307 | |
308 | newmap->timeout=0; |
308 | newmap->timeout=0; |
… | |
… | |
716 | /* It may be nice to support other creatures moving across |
716 | /* It may be nice to support other creatures moving across |
717 | * exits, but right now a lot of the code looks at op->contr, |
717 | * exits, but right now a lot of the code looks at op->contr, |
718 | * so thta is an RFE. |
718 | * so thta is an RFE. |
719 | */ |
719 | */ |
720 | if (op->type != PLAYER) return; |
720 | if (op->type != PLAYER) return; |
721 | |
|
|
722 | /* Need to remove player from transport */ |
|
|
723 | if (op->contr->transport) apply_transport(op, op->contr->transport, AP_UNAPPLY); |
|
|
724 | |
721 | |
725 | /* First, lets figure out what map the player is going to go to */ |
722 | /* First, lets figure out what map the player is going to go to */ |
726 | if (exit_ob){ |
723 | if (exit_ob){ |
727 | |
724 | |
728 | /* check to see if we make a template map */ |
725 | /* check to see if we make a template map */ |
… | |
… | |
869 | } |
866 | } |
870 | |
867 | |
871 | /* |
868 | /* |
872 | * process_active_maps(): Works like process_events(), but it only |
869 | * process_active_maps(): Works like process_events(), but it only |
873 | * processes maps which a player is on. |
870 | * 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 | * |
871 | * |
877 | */ |
872 | */ |
878 | |
873 | |
|
|
874 | #if 0 // dead code, schmorp |
879 | void process_active_maps(void) { |
875 | void process_active_maps () |
880 | mapstruct *map; |
876 | { |
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) { |
877 | for (mapstruct *map = first_map; map != NULL; map = map->next) |
888 | if(map->in_memory == MAP_IN_MEMORY) { |
878 | if (map->in_memory == MAP_IN_MEMORY) |
889 | if(players_on_map(map,TRUE)) |
879 | if (players_on_map (map, TRUE)) |
890 | process_events(map); |
880 | process_events (map); |
891 | } |
|
|
892 | } |
|
|
893 | } |
|
|
894 | } |
881 | } |
|
|
882 | #endif |
895 | |
883 | |
896 | /* process_players1 and process_players2 do all the player related stuff. |
884 | /* 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 |
885 | * 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 |
886 | * 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 |
887 | * by the various functions. process_players1() does the processing before |
… | |
… | |
1196 | * cleanup. We also leave that loop to actually handle the freeing |
1184 | * cleanup. We also leave that loop to actually handle the freeing |
1197 | * of the data. |
1185 | * of the data. |
1198 | */ |
1186 | */ |
1199 | if (pl->ob->type != DEAD_OBJECT) |
1187 | if (pl->ob->type != DEAD_OBJECT) |
1200 | { |
1188 | { |
1201 | execute_global_event (EVENT_LOGOUT, pl, pl->socket.host); |
|
|
1202 | LOG (llevInfo,"LOGOUT: Player named %s from ip %s\n", pl->ob->name, pl->socket.host); |
|
|
1203 | |
|
|
1204 | pl->socket.status = Ns_Dead; |
1189 | pl->socket.status = Ns_Dead; |
1205 | |
1190 | |
1206 | /* If a hidden dm dropped connection do not create |
1191 | /* If a hidden dm dropped connection do not create |
1207 | * inconsistencies by showing that they have left the game |
1192 | * inconsistencies by showing that they have left the game |
1208 | */ |
1193 | */ |
1209 | if (!(QUERY_FLAG(pl->ob,FLAG_WIZ) && pl->ob->contr->hidden) |
1194 | if (!(QUERY_FLAG(pl->ob,FLAG_WIZ) && pl->ob->contr->hidden) |
1210 | && draw_exit |
1195 | && draw_exit |
1211 | && (pl->state != ST_GET_NAME && pl->state!=ST_GET_PASSWORD && pl->state != ST_CONFIRM_PASSWORD)) |
1196 | && (pl->state != ST_GET_NAME && pl->state!=ST_GET_PASSWORD && pl->state != ST_CONFIRM_PASSWORD)) |
1212 | { |
1197 | { |
|
|
1198 | if (pl->ob->map) |
|
|
1199 | { |
|
|
1200 | INVOKE_PLAYER (LOGOUT, pl); |
|
|
1201 | LOG (llevInfo,"LOGOUT: Player named %s from ip %s\n", pl->ob->name, pl->socket.host); |
|
|
1202 | } |
|
|
1203 | |
1213 | char buf[MAX_BUF]; |
1204 | char buf[MAX_BUF]; |
1214 | sprintf (buf, "%s left the game.", pl->ob->name); |
1205 | sprintf (buf, "%s left the game.", pl->ob->name); |
1215 | new_draw_info(NDI_UNIQUE | NDI_ALL | NDI_DK_ORANGE, 5, NULL, buf); |
1206 | new_draw_info(NDI_UNIQUE | NDI_ALL | NDI_DK_ORANGE, 5, NULL, buf); |
1216 | } |
1207 | } |
1217 | |
1208 | |
… | |
… | |
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 | INVOKE_GLOBAL (CLOCK); |
|
|
1315 | process_events(NULL); /* "do" something with objects with speed */ |
|
|
1316 | flush_sockets(); |
|
|
1317 | check_active_maps(); /* Removes unused maps after a certain timeout */ |
|
|
1318 | do_specials(); /* Routines called from time to time. */ |
1333 | |
1319 | |
1334 | long sleep_sec = last_time.tv_sec - new_time.tv_sec; |
1320 | ++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 | } |
1321 | } |
1375 | |
1322 | |
1376 | int main(int argc, char **argv) |
1323 | int main(int argc, char **argv) |
1377 | { |
1324 | { |
1378 | #ifdef WIN32 /* ---win32 this sets the win32 from 0d0a to 0a handling */ |
|
|
1379 | _fmode = _O_BINARY ; |
|
|
1380 | bRunning = 1; |
|
|
1381 | #endif |
|
|
1382 | |
|
|
1383 | #ifdef DEBUG_MALLOC_LEVEL |
|
|
1384 | malloc_debug(DEBUG_MALLOC_LEVEL); |
|
|
1385 | #endif |
|
|
1386 | |
|
|
1387 | settings.argc=argc; |
1325 | settings.argc = argc; |
1388 | settings.argv=argv; |
1326 | settings.argv = argv; |
|
|
1327 | |
1389 | init(argc, argv); |
1328 | init (argc, argv); |
1390 | initPlugins(); /* GROS - Init the Plugins */ |
1329 | |
1391 | #ifdef WIN32 |
1330 | cfperl_init (); |
1392 | while ( bRunning ) |
1331 | initPlugins (); |
1393 | { |
1332 | |
1394 | #else |
|
|
1395 | for(;;) { |
1333 | for (;;) |
1396 | #endif |
1334 | cfperl_main (); |
1397 | nroferrors = 0; |
|
|
1398 | |
1335 | |
1399 | doeric_server(); |
1336 | // unreached |
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 | |
|
|
1408 | sleep_delta(); /* Slepp proper amount of time before next tick */ |
|
|
1409 | } |
|
|
1410 | emergency_save( 0 ); |
1337 | emergency_save (0); |
1411 | cleanup( ); |
1338 | cleanup (); |
|
|
1339 | |
1412 | return 0; |
1340 | return 0; |
1413 | } |
1341 | } |