… | |
… | |
31 | # ifdef HAVE_CRYPT_H |
31 | # ifdef HAVE_CRYPT_H |
32 | # include <crypt.h> |
32 | # include <crypt.h> |
33 | # endif |
33 | # endif |
34 | #endif |
34 | #endif |
35 | |
35 | |
36 | #ifndef __CEXTRACT__ |
|
|
37 | # include <sproto.h> |
36 | #include <sproto.h> |
38 | #endif |
|
|
39 | |
|
|
40 | #ifdef HAVE_TIME_H |
|
|
41 | # include <time.h> |
37 | #include <time.h> |
42 | #endif |
|
|
43 | |
38 | |
44 | #include <../random_maps/random_map.h> |
39 | #include <../random_maps/random_map.h> |
45 | #include <../random_maps/rproto.h> |
40 | #include <../random_maps/rproto.h> |
46 | #include "path.h" |
41 | #include "path.h" |
47 | |
42 | |
… | |
… | |
472 | rp.origin_y = exit_ob->y; |
467 | rp.origin_y = exit_ob->y; |
473 | strcpy (rp.origin_map, pl->map->path); |
468 | strcpy (rp.origin_map, pl->map->path); |
474 | |
469 | |
475 | /* If we have a final_map, use it as a base name to give some clue |
470 | /* If we have a final_map, use it as a base name to give some clue |
476 | * as where the player is. Otherwise, use the origin map. |
471 | * as where the player is. Otherwise, use the origin map. |
477 | * Take the last component (after the last slash) to give |
|
|
478 | * shorter names without bogus slashes. |
|
|
479 | */ |
472 | */ |
480 | if (rp.final_map[0]) |
473 | sprintf (newmap_name, "/random%s+%04d", |
481 | { |
474 | *rp.final_map ? rp.final_map : rp.origin_map, |
482 | cp = strrchr (rp.final_map, '/'); |
475 | reference_number++); |
483 | if (!cp) |
|
|
484 | cp = rp.final_map; |
|
|
485 | } |
|
|
486 | else |
|
|
487 | { |
|
|
488 | char buf[HUGE_BUF]; |
|
|
489 | |
|
|
490 | cp = strrchr (rp.origin_map, '/'); |
|
|
491 | if (!cp) |
|
|
492 | cp = rp.origin_map; |
|
|
493 | /* Need to strip of any trailing digits, if it has them */ |
|
|
494 | strcpy (buf, cp); |
|
|
495 | while (isdigit (buf[strlen (buf) - 1])) |
|
|
496 | buf[strlen (buf) - 1] = 0; |
|
|
497 | cp = buf; |
|
|
498 | } |
|
|
499 | |
|
|
500 | sprintf (newmap_name, "/random/%s%04d", cp + 1, reference_number++); |
|
|
501 | |
476 | |
502 | /* now to generate the actual map. */ |
477 | /* now to generate the actual map. */ |
503 | new_map = generate_random_map (newmap_name, &rp); |
478 | new_map = generate_random_map (newmap_name, &rp); |
504 | |
479 | |
505 | /* Update the exit_ob so it now points directly at the newly created |
480 | /* Update the exit_ob so it now points directly at the newly created |
… | |
… | |
950 | |
925 | |
951 | enter_map (op, newmap, op->x, op->y); |
926 | enter_map (op, newmap, op->x, op->y); |
952 | } |
927 | } |
953 | } |
928 | } |
954 | |
929 | |
955 | /* |
|
|
956 | * process_active_maps(): Works like process_events(), but it only |
|
|
957 | * processes maps which a player is on. |
|
|
958 | * |
|
|
959 | */ |
|
|
960 | |
|
|
961 | #if 0 // dead code, schmorp |
|
|
962 | void |
|
|
963 | process_active_maps () |
|
|
964 | { |
|
|
965 | for (maptile *map = first_map; map != NULL; map = map->next) |
|
|
966 | if (map->in_memory == MAP_IN_MEMORY) |
|
|
967 | if (players_on_map (map, TRUE)) |
|
|
968 | process_events (map); |
|
|
969 | } |
|
|
970 | #endif |
|
|
971 | |
|
|
972 | /* process_players1 and process_players2 do all the player related stuff. |
930 | /* process_players1 and process_players2 do all the player related stuff. |
973 | * I moved it out of process events and process_map. This was to some |
931 | * I moved it out of process events and process_map. This was to some |
974 | * extent for debugging as well as to get a better idea of the time used |
932 | * extent for debugging as well as to get a better idea of the time used |
975 | * by the various functions. process_players1() does the processing before |
933 | * by the various functions. process_players1() does the processing before |
976 | * objects have been updated, process_players2() does the processing that |
934 | * objects have been updated, process_players2() does the processing that |
977 | * is needed after the players have been updated. |
935 | * is needed after the players have been updated. |
978 | */ |
936 | */ |
979 | |
937 | static void |
980 | void |
|
|
981 | process_players1 (maptile *map) |
938 | process_players1 () |
982 | { |
939 | { |
983 | int flag; |
940 | int flag; |
984 | player *pl, *plnext; |
|
|
985 | |
|
|
986 | /* Basically, we keep looping until all the players have done their actions. */ |
941 | /* Basically, we keep looping until all the players have done their actions. */ |
987 | for (flag = 1; flag != 0;) |
942 | for (flag = 1; flag != 0;) |
988 | { |
943 | { |
989 | flag = 0; |
944 | flag = 0; |
990 | for (pl = first_player; pl != NULL; pl = plnext) |
945 | for (player *plnext, *pl = first_player; pl; pl = plnext) |
991 | { |
946 | { |
992 | plnext = pl->next; /* In case a player exits the game in handle_player() */ |
947 | plnext = pl->next; /* In case a player exits the game in handle_player() */ |
993 | |
948 | |
994 | if (pl->ob == NULL) |
949 | if (!pl->ob) |
995 | continue; |
|
|
996 | |
|
|
997 | if (map != NULL && pl->ob->map != map) |
|
|
998 | continue; |
950 | continue; |
999 | |
951 | |
1000 | if (pl->ob->speed_left > 0) |
952 | if (pl->ob->speed_left > 0) |
1001 | { |
953 | { |
1002 | if (handle_newcs_player (pl->ob)) |
954 | if (handle_newcs_player (pl->ob)) |
… | |
… | |
1016 | /* check for ST_PLAYING state so that we don't try to save off when |
968 | /* check for ST_PLAYING state so that we don't try to save off when |
1017 | * the player is logging in. |
969 | * the player is logging in. |
1018 | */ |
970 | */ |
1019 | if ((pl->last_save_tick + AUTOSAVE) < (uint32) pticks && pl->state == ST_PLAYING) |
971 | if ((pl->last_save_tick + AUTOSAVE) < (uint32) pticks && pl->state == ST_PLAYING) |
1020 | { |
972 | { |
1021 | /* Don't save the player on unholy ground. Instead, increase the |
|
|
1022 | * tick time so it will be about 10 seconds before we try and save |
|
|
1023 | * again. |
|
|
1024 | */ |
|
|
1025 | // if (get_map_flags(pl->ob->map, NULL, pl->ob->x, pl->ob->y, NULL, NULL) & P_NO_CLERIC) { |
|
|
1026 | // pl->last_save_tick += 100; |
|
|
1027 | // } else { |
|
|
1028 | save_player (pl->ob, 1); |
973 | save_player (pl->ob, 1); |
1029 | pl->last_save_tick = pticks; |
974 | pl->last_save_tick = pticks; |
1030 | // } |
|
|
1031 | } |
975 | } |
1032 | #endif |
976 | #endif |
1033 | } /* end of for loop for all the players */ |
977 | } /* end of for loop for all the players */ |
1034 | } /* for flag */ |
978 | } /* for flag */ |
|
|
979 | |
1035 | for (pl = first_player; pl != NULL; pl = pl->next) |
980 | for (player *pl = first_player; pl; pl = pl->next) |
1036 | { |
981 | { |
1037 | if (map != NULL && (pl->ob == NULL || pl->ob->map != map)) |
|
|
1038 | continue; |
|
|
1039 | if (settings.casting_time == TRUE) |
982 | if (settings.casting_time) |
1040 | { |
983 | { |
1041 | if (pl->ob->casting_time > 0) |
984 | if (pl->ob->casting_time > 0) |
1042 | { |
985 | { |
1043 | pl->ob->casting_time--; |
986 | pl->ob->casting_time--; |
1044 | pl->ob->start_holding = 1; |
987 | pl->ob->start_holding = 1; |
1045 | } |
988 | } |
|
|
989 | |
1046 | /* set spell_state so we can update the range in stats field */ |
990 | /* set spell_state so we can update the range in stats field */ |
1047 | if ((pl->ob->casting_time == 0) && (pl->ob->start_holding == 1)) |
991 | if ((pl->ob->casting_time == 0) && (pl->ob->start_holding == 1)) |
1048 | { |
|
|
1049 | pl->ob->start_holding = 0; |
992 | pl->ob->start_holding = 0; |
1050 | } |
993 | } |
1051 | } |
994 | |
1052 | do_some_living (pl->ob); |
995 | do_some_living (pl->ob); |
1053 | /* draw(pl->ob); *//* updated in socket code */ |
|
|
1054 | } |
996 | } |
1055 | } |
997 | } |
1056 | |
998 | |
1057 | void |
999 | static void |
1058 | process_players2 (maptile *map) |
1000 | process_players2 () |
1059 | { |
1001 | { |
1060 | player *pl; |
|
|
1061 | |
|
|
1062 | /* Then check if any players should use weapon-speed instead of speed */ |
1002 | /* Then check if any players should use weapon-speed instead of speed */ |
1063 | for (pl = first_player; pl != NULL; pl = pl->next) |
1003 | for (player *pl = first_player; pl; pl = pl->next) |
1064 | { |
1004 | { |
1065 | if (map != NULL) |
|
|
1066 | { |
|
|
1067 | if (pl->ob == NULL || QUERY_FLAG (pl->ob, FLAG_REMOVED)) |
|
|
1068 | continue; |
|
|
1069 | else if (pl->loading != NULL) /* Player is blocked */ |
|
|
1070 | pl->ob->speed_left -= pl->ob->speed; |
|
|
1071 | if (pl->ob->map != map) |
|
|
1072 | continue; |
|
|
1073 | } |
|
|
1074 | |
|
|
1075 | /* The code that did weapon_sp handling here was out of place - |
1005 | /* The code that did weapon_sp handling here was out of place - |
1076 | * this isn't called until after the player has finished there |
1006 | * this isn't called until after the player has finished there |
1077 | * actions, and is thus out of place. All we do here is bounds |
1007 | * actions, and is thus out of place. All we do here is bounds |
1078 | * checking. |
1008 | * checking. |
1079 | */ |
1009 | */ |
… | |
… | |
1085 | /* This needs to be here - if the player is running, we need to |
1015 | /* This needs to be here - if the player is running, we need to |
1086 | * clear this each tick, but new commands are not being received |
1016 | * clear this each tick, but new commands are not being received |
1087 | * so execute_newserver_command() is never called |
1017 | * so execute_newserver_command() is never called |
1088 | */ |
1018 | */ |
1089 | pl->has_hit = 0; |
1019 | pl->has_hit = 0; |
1090 | |
|
|
1091 | } |
1020 | } |
1092 | else if (pl->ob->speed_left > pl->ob->speed) |
1021 | else if (pl->ob->speed_left > pl->ob->speed) |
1093 | pl->ob->speed_left = pl->ob->speed; |
1022 | pl->ob->speed_left = pl->ob->speed; |
1094 | } |
1023 | } |
1095 | } |
1024 | } |
1096 | |
1025 | |
1097 | void |
1026 | void |
1098 | process_events (maptile *map) |
1027 | process_events () |
1099 | { |
1028 | { |
1100 | object *op; |
1029 | object *op; |
1101 | |
1030 | |
1102 | static object *marker; |
1031 | static object *marker; |
1103 | |
1032 | |
1104 | if (!marker) |
1033 | if (!marker) |
1105 | marker = object::create (); |
1034 | marker = object::create (); |
1106 | |
1035 | |
1107 | process_players1 (map); |
1036 | process_players1 (); |
1108 | |
1037 | |
1109 | marker->active_next = active_objects; |
1038 | marker->active_next = active_objects; |
1110 | |
1039 | |
1111 | if (marker->active_next) |
1040 | if (marker->active_next) |
1112 | marker->active_next->active_prev = marker; |
1041 | marker->active_next->active_prev = marker; |
… | |
… | |
1163 | continue; |
1092 | continue; |
1164 | } |
1093 | } |
1165 | |
1094 | |
1166 | if (!op->speed) |
1095 | if (!op->speed) |
1167 | { |
1096 | { |
1168 | LOG (llevError, "BUG: process_events(): Object %s has no speed, " "but is on active list\n", &op->arch->name); |
1097 | LOG (llevError, "BUG: process_events(): Object %s has no speed, " |
|
|
1098 | "but is on active list\n", &op->arch->name); |
1169 | update_ob_speed (op); |
1099 | update_ob_speed (op); |
1170 | continue; |
1100 | continue; |
1171 | } |
1101 | } |
1172 | |
1102 | |
1173 | if (op->map == NULL && op->env == NULL && op->name && op->type != MAP && map == NULL) |
1103 | if (op->map == NULL && op->env == NULL && op->name && op->type != MAP) |
1174 | { |
1104 | { |
1175 | LOG (llevError, "BUG: process_events(): Object without map or " "inventory is on active list: %s (%d)\n", &op->name, op->count); |
1105 | LOG (llevError, "BUG: process_events(): Object without map or " |
|
|
1106 | "inventory is on active list: %s (%d)\n", &op->name, op->count); |
1176 | op->speed = 0; |
1107 | op->speed = 0; |
1177 | update_ob_speed (op); |
1108 | update_ob_speed (op); |
1178 | continue; |
1109 | continue; |
1179 | } |
1110 | } |
1180 | |
1111 | |
1181 | if (map != NULL && op->map != map) |
|
|
1182 | continue; |
|
|
1183 | |
|
|
1184 | /* Animate the object. Bug of feature that andim_speed |
1112 | /* Animate the object. Bug or feature that anim_speed |
1185 | * is based on ticks, and not the creatures speed? |
1113 | * is based on ticks, and not the creatures speed? |
1186 | */ |
1114 | */ |
1187 | if (op->anim_speed && op->last_anim >= op->anim_speed) |
1115 | if (op->anim_speed && op->last_anim >= op->anim_speed) |
1188 | { |
1116 | { |
1189 | if ((op->type == PLAYER) || (op->type == MONSTER)) |
1117 | if ((op->type == PLAYER)) |
1190 | animate_object (op, op->facing); |
1118 | animate_object (op, op->facing); |
1191 | else |
1119 | else |
1192 | animate_object (op, op->direction); |
1120 | animate_object (op, op->direction); |
1193 | |
1121 | |
1194 | op->last_anim = 1; |
1122 | op->last_anim = 1; |
… | |
… | |
1230 | if (marker->active_prev != NULL) |
1158 | if (marker->active_prev != NULL) |
1231 | marker->active_prev->active_next = NULL; |
1159 | marker->active_prev->active_next = NULL; |
1232 | else |
1160 | else |
1233 | active_objects = NULL; |
1161 | active_objects = NULL; |
1234 | |
1162 | |
1235 | process_players2 (map); |
1163 | process_players2 (); |
1236 | } |
1164 | } |
1237 | |
1165 | |
1238 | void |
1166 | void |
1239 | clean_tmp_files (void) |
1167 | clean_tmp_files (void) |
1240 | { |
1168 | { |
… | |
… | |
1244 | |
1172 | |
1245 | /* We save the maps - it may not be intuitive why, but if there are unique |
1173 | /* We save the maps - it may not be intuitive why, but if there are unique |
1246 | * items, we need to save the map so they get saved off. Perhaps we should |
1174 | * items, we need to save the map so they get saved off. Perhaps we should |
1247 | * just make a special function that only saves the unique items. |
1175 | * just make a special function that only saves the unique items. |
1248 | */ |
1176 | */ |
1249 | for (m = first_map; m != NULL; m = next) |
1177 | for (m = first_map; m; m = next) |
1250 | { |
1178 | { |
1251 | next = m->next; |
1179 | next = m->next; |
|
|
1180 | |
1252 | if (m->in_memory == MAP_IN_MEMORY) |
1181 | if (m->in_memory == MAP_IN_MEMORY) |
1253 | { |
1182 | { |
1254 | /* If we want to reuse the temp maps, swap it out (note that will also |
1183 | /* If we want to reuse the temp maps, swap it out (note that will also |
1255 | * update the log file. Otherwise, save the map (mostly for unique item |
1184 | * update the log file. |
1256 | * stuff). Note that the clean_tmp_map is called after the end of |
|
|
1257 | * the for loop but is in the #else bracket. IF we are recycling the maps, |
|
|
1258 | * we certainly don't want the temp maps removed. |
|
|
1259 | */ |
1185 | */ |
1260 | |
1186 | |
1261 | /* XXX The above comment is dead wrong */ |
|
|
1262 | if (settings.recycle_tmp_maps == TRUE) |
|
|
1263 | swap_map (m); |
1187 | swap_map (m); |
1264 | else |
|
|
1265 | { |
|
|
1266 | new_save_map (m, 0); /* note we save here into a overlay map */ |
|
|
1267 | clean_tmp_map (m); |
|
|
1268 | } |
1188 | } |
1269 | } |
|
|
1270 | } |
1189 | } |
|
|
1190 | |
1271 | write_todclock (); /* lets just write the clock here */ |
1191 | write_todclock (); /* lets just write the clock here */ |
1272 | } |
1192 | } |
1273 | |
1193 | |
1274 | /* clean up everything before exiting */ |
1194 | /* clean up everything before exiting */ |
1275 | void |
1195 | void |
1276 | cleanup (void) |
1196 | cleanup (bool make_core) |
1277 | { |
1197 | { |
1278 | LOG (llevDebug, "Cleanup called.\n"); |
1198 | LOG (llevDebug, "Cleanup called.\n"); |
1279 | |
1199 | |
|
|
1200 | if (init_done) |
|
|
1201 | { |
1280 | for (player *pl = first_player; pl != NULL; pl = pl->next) |
1202 | for (player *pl = first_player; pl != NULL; pl = pl->next) |
1281 | save_player (pl->ob, 0); |
1203 | save_player (pl->ob, 0); |
1282 | |
1204 | |
1283 | for (player *pl = first_player; pl != NULL; pl = pl->next) |
1205 | for (player *pl = first_player; pl != NULL; pl = pl->next) |
1284 | if (!QUERY_FLAG (pl->ob, FLAG_REMOVED)) |
1206 | if (!QUERY_FLAG (pl->ob, FLAG_REMOVED)) |
1285 | leave_map (pl->ob); |
1207 | leave_map (pl->ob); |
1286 | |
1208 | |
1287 | clean_tmp_files (); |
1209 | clean_tmp_files (); |
1288 | write_book_archive (); |
1210 | write_book_archive (); |
1289 | |
1211 | |
1290 | INVOKE_GLOBAL (CLEANUP); |
1212 | INVOKE_GLOBAL (CLEANUP); |
|
|
1213 | } |
1291 | |
1214 | |
|
|
1215 | if (make_core) |
|
|
1216 | abort (); |
|
|
1217 | else |
1292 | _exit (0); |
1218 | _exit (0); |
1293 | } |
1219 | } |
1294 | |
1220 | |
1295 | void |
1221 | void |
1296 | leave (player *pl, int draw_exit) |
1222 | leave (player *pl, int draw_exit) |
1297 | { |
1223 | { |
1298 | if (pl != NULL) |
1224 | if (pl) |
1299 | { |
1225 | { |
1300 | /* We do this so that the socket handling routine can do the final |
1226 | /* We do this so that the socket handling routine can do the final |
1301 | * cleanup. We also leave that loop to actually handle the freeing |
1227 | * cleanup. We also leave that loop to actually handle the freeing |
1302 | * of the data. |
1228 | * of the data. |
1303 | */ |
1229 | */ |
|
|
1230 | if (pl->socket) |
|
|
1231 | pl->socket->destroy (); |
|
|
1232 | |
1304 | if (pl->ob->type != DEAD_OBJECT) |
1233 | if (pl->ob->type != DEAD_OBJECT) |
1305 | { |
1234 | { |
1306 | pl->socket.status = Ns_Dead; |
|
|
1307 | |
|
|
1308 | /* If a hidden dm dropped connection do not create |
1235 | /* If a hidden dm dropped connection do not create |
1309 | * inconsistencies by showing that they have left the game |
1236 | * inconsistencies by showing that they have left the game |
1310 | */ |
1237 | */ |
1311 | if (!(QUERY_FLAG (pl->ob, FLAG_WIZ) && pl->ob->contr->hidden) |
1238 | if (!(QUERY_FLAG (pl->ob, FLAG_WIZ) && pl->ob->contr->hidden) |
1312 | && draw_exit && (pl->state != ST_GET_NAME && pl->state != ST_GET_PASSWORD && pl->state != ST_CONFIRM_PASSWORD)) |
1239 | && draw_exit && (pl->state != ST_GET_NAME && pl->state != ST_GET_PASSWORD && pl->state != ST_CONFIRM_PASSWORD)) |
1313 | { |
1240 | { |
1314 | if (pl->ob->map) |
1241 | if (pl->ob->map) |
1315 | { |
1242 | { |
1316 | INVOKE_PLAYER (LOGOUT, pl); |
1243 | INVOKE_PLAYER (LOGOUT, pl); |
1317 | LOG (llevInfo, "LOGOUT: Player named %s from ip %s\n", &pl->ob->name, pl->socket.host); |
1244 | LOG (llevInfo, "LOGOUT: Player named %s from ip %s\n", &pl->ob->name, pl->socket->host); |
1318 | } |
1245 | } |
1319 | |
1246 | |
1320 | char buf[MAX_BUF]; |
1247 | char buf[MAX_BUF]; |
1321 | |
1248 | |
1322 | sprintf (buf, "%s left the game.", &pl->ob->name); |
1249 | sprintf (buf, "%s left the game.", &pl->ob->name); |
… | |
… | |
1398 | extern unsigned long todtick; |
1325 | extern unsigned long todtick; |
1399 | |
1326 | |
1400 | void |
1327 | void |
1401 | do_specials (void) |
1328 | do_specials (void) |
1402 | { |
1329 | { |
1403 | |
|
|
1404 | #ifdef WATCHDOG |
|
|
1405 | if (!(pticks % 503)) |
|
|
1406 | watchdog (); |
|
|
1407 | #endif |
|
|
1408 | |
|
|
1409 | if (!(pticks % PTICKS_PER_CLOCK)) |
1330 | if (!(pticks % PTICKS_PER_CLOCK)) |
1410 | tick_the_clock (); |
1331 | tick_the_clock (); |
1411 | |
1332 | |
1412 | if (!(pticks % 7)) |
1333 | if (!(pticks % 7)) |
1413 | shstr::gc (); |
1334 | shstr::gc (); |
… | |
… | |
1434 | void |
1355 | void |
1435 | server_tick () |
1356 | server_tick () |
1436 | { |
1357 | { |
1437 | nroferrors = 0; |
1358 | nroferrors = 0; |
1438 | |
1359 | |
|
|
1360 | // first do the user visible stuff |
1439 | doeric_server (); |
1361 | doeric_server (); |
1440 | INVOKE_GLOBAL (CLOCK); |
1362 | INVOKE_GLOBAL (CLOCK); |
1441 | process_events (NULL); /* "do" something with objects with speed */ |
1363 | process_events (); /* "do" something with objects with speed */ |
1442 | flush_sockets (); |
1364 | flush_sockets (); |
|
|
1365 | |
|
|
1366 | // then do some bookkeeping, should not really be here |
1443 | check_active_maps (); /* Removes unused maps after a certain timeout */ |
1367 | check_active_maps (); /* Removes unused maps after a certain timeout */ |
1444 | do_specials (); /* Routines called from time to time. */ |
1368 | do_specials (); /* Routines called from time to time. */ |
1445 | object::free_mortals (); |
1369 | object::free_mortals (); |
1446 | |
1370 | |
1447 | ++pticks; |
1371 | ++pticks; |