1 | /* |
1 | /* |
2 | * static char *rcsid_map_c = |
2 | * static char *rcsid_map_c = |
3 | * "$Id: map.C,v 1.10 2006/08/28 14:05:23 root Exp $"; |
3 | * "$Id: map.C,v 1.16 2006/08/30 16:30:37 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 | |
… | |
… | |
173 | * Can not use strcat because we need to cycle through |
173 | * Can not use strcat because we need to cycle through |
174 | * all the names. |
174 | * all the names. |
175 | */ |
175 | */ |
176 | endbuf = buf + strlen(buf); |
176 | endbuf = buf + strlen(buf); |
177 | |
177 | |
178 | for (i = 0; i < NROF_COMPRESS_METHODS; i++) { |
|
|
179 | if (uncomp[i][0]) |
|
|
180 | strcpy(endbuf, uncomp[i][0]); |
|
|
181 | else |
|
|
182 | *endbuf = '\0'; |
|
|
183 | if (!stat (buf, &statbuf)) |
178 | if (stat (buf, &statbuf)) |
184 | break; |
|
|
185 | } |
|
|
186 | if (i == NROF_COMPRESS_METHODS) |
|
|
187 | return (-1); |
179 | return -1; |
188 | if (!S_ISREG (statbuf.st_mode)) |
180 | if (!S_ISREG (statbuf.st_mode)) |
189 | return (-1); |
181 | return (-1); |
190 | |
182 | |
191 | if (((statbuf.st_mode & S_IRGRP) && getegid() == statbuf.st_gid) || |
183 | if (((statbuf.st_mode & S_IRGRP) && getegid() == statbuf.st_gid) || |
192 | ((statbuf.st_mode & S_IRUSR) && geteuid() == statbuf.st_uid) || |
184 | ((statbuf.st_mode & S_IRUSR) && geteuid() == statbuf.st_uid) || |
… | |
… | |
793 | * currently, there are few enough fields this is not a big deal. |
785 | * currently, there are few enough fields this is not a big deal. |
794 | * MSW 2001-07-01 |
786 | * MSW 2001-07-01 |
795 | * return 0 on success, 1 on failure. |
787 | * return 0 on success, 1 on failure. |
796 | */ |
788 | */ |
797 | |
789 | |
798 | static int load_map_header(FILE *fp, mapstruct *m) |
790 | static int load_map_header(object_thawer &fp, mapstruct *m) |
799 | { |
791 | { |
800 | char buf[HUGE_BUF], msgbuf[HUGE_BUF], maplorebuf[HUGE_BUF], *key=NULL, *value, *end; |
792 | char buf[HUGE_BUF], msgbuf[HUGE_BUF], maplorebuf[HUGE_BUF], *key=NULL, *value, *end; |
801 | int msgpos=0; |
793 | int msgpos=0; |
802 | int maplorepos=0; |
794 | int maplorepos=0; |
803 | |
795 | |
… | |
… | |
893 | } |
885 | } |
894 | /* first strcmp value on these are old names supported |
886 | /* first strcmp value on these are old names supported |
895 | * for compatibility reasons. The new values (second) are |
887 | * for compatibility reasons. The new values (second) are |
896 | * what really should be used. |
888 | * what really should be used. |
897 | */ |
889 | */ |
|
|
890 | else if (!strcmp(key,"oid")) { |
|
|
891 | fp.get (m, atoi(value)); |
|
|
892 | } else if (!strcmp(key, "attach")) { |
|
|
893 | m->attach = add_string (value); |
898 | else if (!strcmp(key,"hp") || !strcmp(key, "enter_x")) { |
894 | } else if (!strcmp(key,"hp") || !strcmp(key, "enter_x")) { |
899 | m->enter_x = atoi(value); |
895 | m->enter_x = atoi(value); |
900 | } else if (!strcmp(key,"sp") || !strcmp(key, "enter_y")) { |
896 | } else if (!strcmp(key,"sp") || !strcmp(key, "enter_y")) { |
901 | m->enter_y = atoi(value); |
897 | m->enter_y = atoi(value); |
902 | } else if (!strcmp(key,"x") || !strcmp(key, "width")) { |
898 | } else if (!strcmp(key,"x") || !strcmp(key, "width")) { |
903 | m->width = atoi(value); |
899 | m->width = atoi(value); |
… | |
… | |
945 | m->winddir = atoi(value); |
941 | m->winddir = atoi(value); |
946 | } else if (!strcmp(key, "sky")) { |
942 | } else if (!strcmp(key, "sky")) { |
947 | m->sky = atoi(value); |
943 | m->sky = atoi(value); |
948 | } else if (!strcmp(key, "nosmooth")) { |
944 | } else if (!strcmp(key, "nosmooth")) { |
949 | m->nosmooth = atoi(value); |
945 | m->nosmooth = atoi(value); |
950 | } else if (!strcmp(key, "safe_map")) { |
|
|
951 | m->safe_map = atoi(value); |
|
|
952 | } |
946 | } |
953 | else if (!strncmp(key,"tile_path_", 10)) { |
947 | else if (!strncmp(key,"tile_path_", 10)) { |
954 | int tile=atoi(key+10); |
948 | int tile=atoi(key+10); |
955 | |
949 | |
956 | if (tile<1 || tile>4) { |
950 | if (tile<1 || tile>4) { |
… | |
… | |
1013 | * MAP_STYLE: style map - don't add active objects, don't add to server |
1007 | * MAP_STYLE: style map - don't add active objects, don't add to server |
1014 | * managed map list. |
1008 | * managed map list. |
1015 | */ |
1009 | */ |
1016 | |
1010 | |
1017 | mapstruct *load_original_map(const char *filename, int flags) { |
1011 | mapstruct *load_original_map(const char *filename, int flags) { |
1018 | FILE *fp; |
|
|
1019 | mapstruct *m; |
1012 | mapstruct *m; |
1020 | int comp; |
|
|
1021 | char pathname[MAX_BUF]; |
1013 | char pathname[MAX_BUF]; |
1022 | |
1014 | |
1023 | LOG(llevDebug, "load_original_map: %s (%x)\n", filename,flags); |
1015 | LOG(llevDebug, "load_original_map: %s (%x)\n", filename,flags); |
1024 | if (flags & MAP_PLAYER_UNIQUE) |
1016 | if (flags & MAP_PLAYER_UNIQUE) |
1025 | strcpy(pathname, filename); |
1017 | strcpy(pathname, filename); |
1026 | else if (flags & MAP_OVERLAY) |
1018 | else if (flags & MAP_OVERLAY) |
1027 | strcpy(pathname, create_overlay_pathname(filename)); |
1019 | strcpy(pathname, create_overlay_pathname(filename)); |
1028 | else |
1020 | else |
1029 | strcpy(pathname, create_pathname(filename)); |
1021 | strcpy(pathname, create_pathname(filename)); |
1030 | |
1022 | |
1031 | if((fp=open_and_uncompress(pathname, 0, &comp))==NULL) { |
|
|
1032 | LOG(llevError, "Can't open %s: %s\n", pathname, strerror_local(errno)); |
|
|
1033 | return (NULL); |
|
|
1034 | } |
|
|
1035 | |
|
|
1036 | object_thawer thawer (fp, filename); |
1023 | object_thawer thawer (pathname); |
|
|
1024 | |
|
|
1025 | if (!thawer) |
|
|
1026 | return 0; |
1037 | |
1027 | |
1038 | m = get_linked_map(); |
1028 | m = get_linked_map(); |
1039 | |
1029 | |
1040 | strcpy (m->path, filename); |
1030 | strcpy (m->path, filename); |
1041 | if (load_map_header(fp, m)) { |
1031 | if (load_map_header(thawer, m)) { |
1042 | LOG(llevError,"Error loading map header for %s, flags=%d\n", |
1032 | LOG(llevError,"Error loading map header for %s, flags=%d\n", |
1043 | filename, flags); |
1033 | filename, flags); |
1044 | delete_map(m); |
1034 | delete_map(m); |
1045 | return NULL; |
1035 | return NULL; |
1046 | } |
1036 | } |
1047 | |
1037 | |
1048 | allocate_map(m); |
1038 | allocate_map(m); |
1049 | |
1039 | |
1050 | m->in_memory=MAP_LOADING; |
1040 | m->in_memory=MAP_LOADING; |
1051 | load_objects (m, thawer, flags & (MAP_BLOCK|MAP_STYLE)); |
1041 | load_objects (m, thawer, flags & (MAP_BLOCK|MAP_STYLE)); |
1052 | close_and_delete(fp, comp); |
1042 | |
1053 | m->in_memory=MAP_IN_MEMORY; |
1043 | m->in_memory=MAP_IN_MEMORY; |
1054 | if (!MAP_DIFFICULTY(m)) |
1044 | if (!MAP_DIFFICULTY(m)) |
1055 | MAP_DIFFICULTY(m)=calculate_difficulty(m); |
1045 | MAP_DIFFICULTY(m)=calculate_difficulty(m); |
1056 | set_map_reset_time(m); |
1046 | set_map_reset_time(m); |
1057 | INVOKE_MAP (INSTANTIATE, m); |
1047 | m->instantiate (); |
1058 | return (m); |
1048 | return (m); |
1059 | } |
1049 | } |
1060 | |
1050 | |
1061 | /* |
1051 | /* |
1062 | * Loads a map, which has been loaded earlier, from file. |
1052 | * Loads a map, which has been loaded earlier, from file. |
1063 | * Return the map object we load into (this can change from the passed |
1053 | * Return the map object we load into (this can change from the passed |
1064 | * option if we can't find the original map) |
1054 | * option if we can't find the original map) |
1065 | */ |
1055 | */ |
1066 | |
1056 | |
1067 | static mapstruct *load_temporary_map(mapstruct *m) { |
1057 | static mapstruct *load_temporary_map(mapstruct *m) { |
1068 | FILE *fp; |
|
|
1069 | int comp; |
1058 | int comp; |
1070 | char buf[MAX_BUF]; |
1059 | char buf[MAX_BUF]; |
1071 | |
1060 | |
1072 | if (!m->tmpname) { |
1061 | if (!m->tmpname) { |
1073 | LOG(llevError, "No temporary filename for map %s\n", m->path); |
1062 | LOG(llevError, "No temporary filename for map %s\n", m->path); |
… | |
… | |
1077 | if(m==NULL) return NULL; |
1066 | if(m==NULL) return NULL; |
1078 | fix_auto_apply(m); /* Chests which open as default */ |
1067 | fix_auto_apply(m); /* Chests which open as default */ |
1079 | return m; |
1068 | return m; |
1080 | } |
1069 | } |
1081 | |
1070 | |
1082 | if((fp=open_and_uncompress(m->tmpname,0, &comp))==NULL) { |
1071 | object_thawer thawer (m->tmpname); |
1083 | LOG(llevError, "Cannot open %s: %s\n",m->tmpname, strerror_local(errno)); |
1072 | |
|
|
1073 | if (!thawer) |
|
|
1074 | { |
1084 | strcpy(buf, m->path); |
1075 | strcpy (buf, m->path); |
1085 | delete_map(m); |
1076 | delete_map (m); |
1086 | m = load_original_map(buf, 0); |
1077 | m = load_original_map (buf, 0); |
1087 | if(m==NULL) return NULL; |
1078 | if (!m) return NULL; |
1088 | fix_auto_apply(m); /* Chests which open as default */ |
1079 | fix_auto_apply (m); /* Chests which open as default */ |
1089 | return m; |
1080 | return m; |
1090 | } |
1081 | } |
1091 | |
|
|
1092 | object_thawer thawer (fp, m->tmpname); |
|
|
1093 | |
1082 | |
1094 | if (load_map_header(thawer, m)) { |
1083 | if (load_map_header(thawer, m)) { |
1095 | LOG(llevError,"Error loading map header for %s (%s)\n", |
1084 | LOG(llevError,"Error loading map header for %s (%s)\n", |
1096 | m->path, m->tmpname); |
1085 | m->path, m->tmpname); |
1097 | delete_map(m); |
1086 | delete_map(m); |
… | |
… | |
1100 | } |
1089 | } |
1101 | allocate_map(m); |
1090 | allocate_map(m); |
1102 | |
1091 | |
1103 | m->in_memory=MAP_LOADING; |
1092 | m->in_memory=MAP_LOADING; |
1104 | load_objects (m, thawer, 0); |
1093 | load_objects (m, thawer, 0); |
1105 | close_and_delete(fp, comp); |
1094 | |
1106 | m->in_memory=MAP_IN_MEMORY; |
1095 | m->in_memory=MAP_IN_MEMORY; |
1107 | INVOKE_MAP (SWAPIN, m); |
1096 | INVOKE_MAP (SWAPIN, m); |
1108 | return m; |
1097 | return m; |
1109 | } |
1098 | } |
1110 | |
1099 | |
… | |
… | |
1113 | * Return the map object we load into (this can change from the passed |
1102 | * Return the map object we load into (this can change from the passed |
1114 | * option if we can't find the original map) |
1103 | * option if we can't find the original map) |
1115 | */ |
1104 | */ |
1116 | |
1105 | |
1117 | mapstruct *load_overlay_map(const char *filename, mapstruct *m) { |
1106 | mapstruct *load_overlay_map(const char *filename, mapstruct *m) { |
1118 | FILE *fp; |
|
|
1119 | int comp; |
|
|
1120 | char pathname[MAX_BUF]; |
1107 | char pathname[MAX_BUF]; |
1121 | |
1108 | |
1122 | strcpy(pathname, create_overlay_pathname(filename)); |
1109 | strcpy(pathname, create_overlay_pathname(filename)); |
1123 | |
1110 | |
1124 | if((fp=open_and_uncompress(pathname, 0, &comp))==NULL) { |
|
|
1125 | /* LOG(llevDebug,"Can't open overlay %s\n", pathname);*/ |
|
|
1126 | return m; |
|
|
1127 | } |
|
|
1128 | |
|
|
1129 | object_thawer thawer (fp, pathname); |
1111 | object_thawer thawer (pathname); |
|
|
1112 | |
|
|
1113 | if (!thawer) |
|
|
1114 | return m; |
1130 | |
1115 | |
1131 | if (load_map_header(thawer, m)) { |
1116 | if (load_map_header(thawer, m)) { |
1132 | LOG(llevError,"Error loading map header for overlay %s (%s)\n", |
1117 | LOG(llevError,"Error loading map header for overlay %s (%s)\n", |
1133 | m->path, pathname); |
1118 | m->path, pathname); |
1134 | delete_map(m); |
1119 | delete_map(m); |
… | |
… | |
1137 | } |
1122 | } |
1138 | /*allocate_map(m);*/ |
1123 | /*allocate_map(m);*/ |
1139 | |
1124 | |
1140 | m->in_memory=MAP_LOADING; |
1125 | m->in_memory=MAP_LOADING; |
1141 | load_objects (m, thawer, MAP_OVERLAY); |
1126 | load_objects (m, thawer, MAP_OVERLAY); |
1142 | close_and_delete(fp, comp); |
1127 | |
1143 | m->in_memory=MAP_IN_MEMORY; |
1128 | m->in_memory=MAP_IN_MEMORY; |
1144 | return m; |
1129 | return m; |
1145 | } |
1130 | } |
1146 | |
1131 | |
1147 | /****************************************************************************** |
1132 | /****************************************************************************** |
… | |
… | |
1176 | /* |
1161 | /* |
1177 | * Loads unique objects from file(s) into the map which is in memory |
1162 | * Loads unique objects from file(s) into the map which is in memory |
1178 | * m is the map to load unique items into. |
1163 | * m is the map to load unique items into. |
1179 | */ |
1164 | */ |
1180 | static void load_unique_objects(mapstruct *m) { |
1165 | static void load_unique_objects(mapstruct *m) { |
1181 | FILE *fp; |
|
|
1182 | int comp,count; |
1166 | int count; |
1183 | char firstname[MAX_BUF]; |
1167 | char firstname[MAX_BUF]; |
1184 | |
1168 | |
1185 | for (count=0; count<10; count++) { |
1169 | for (count=0; count<10; count++) { |
1186 | sprintf(firstname, "%s.v%02d", create_items_path(m->path), count); |
1170 | sprintf(firstname, "%s.v%02d", create_items_path(m->path), count); |
1187 | if (!access(firstname, R_OK)) break; |
1171 | if (!access(firstname, R_OK)) break; |
1188 | } |
1172 | } |
1189 | /* If we get here, we did not find any map */ |
1173 | /* If we get here, we did not find any map */ |
1190 | if (count==10) return; |
1174 | if (count==10) return; |
1191 | |
1175 | |
1192 | if ((fp=open_and_uncompress(firstname, 0, &comp))==NULL) { |
|
|
1193 | /* There is no expectation that every map will have unique items, but this |
|
|
1194 | * is debug output, so leave it in. |
|
|
1195 | */ |
|
|
1196 | LOG(llevDebug, "Can't open unique items file for %s\n", create_items_path(m->path)); |
|
|
1197 | return; |
|
|
1198 | } |
|
|
1199 | |
|
|
1200 | object_thawer thawer (fp, firstname); |
1176 | object_thawer thawer (firstname); |
|
|
1177 | |
|
|
1178 | if (!thawer) |
|
|
1179 | return; |
1201 | |
1180 | |
1202 | m->in_memory=MAP_LOADING; |
1181 | m->in_memory=MAP_LOADING; |
1203 | if (m->tmpname == NULL) /* if we have loaded unique items from */ |
1182 | if (m->tmpname == NULL) /* if we have loaded unique items from */ |
1204 | delete_unique_items(m); /* original map before, don't duplicate them */ |
1183 | delete_unique_items(m); /* original map before, don't duplicate them */ |
1205 | load_object(thawer, NULL, LO_NOREAD,0); |
1184 | load_object(thawer, NULL, LO_NOREAD,0); |
1206 | load_objects (m, thawer, 0); |
1185 | load_objects (m, thawer, 0); |
1207 | close_and_delete(fp, comp); |
1186 | |
1208 | m->in_memory=MAP_IN_MEMORY; |
1187 | m->in_memory=MAP_IN_MEMORY; |
1209 | } |
1188 | } |
1210 | |
1189 | |
1211 | |
1190 | |
1212 | /* |
1191 | /* |
… | |
… | |
1325 | fprintf (fp, "winddir %d\n", m->winddir); |
1304 | fprintf (fp, "winddir %d\n", m->winddir); |
1326 | if (m->sky) |
1305 | if (m->sky) |
1327 | fprintf (fp, "sky %d\n", m->sky); |
1306 | fprintf (fp, "sky %d\n", m->sky); |
1328 | if (m->nosmooth) |
1307 | if (m->nosmooth) |
1329 | fprintf (fp, "nosmooth %d\n", m->nosmooth); |
1308 | fprintf (fp, "nosmooth %d\n", m->nosmooth); |
1330 | if (m->safe_map) |
|
|
1331 | fprintf (fp, "safe_map %d\n", m->safe_map); |
|
|
1332 | |
1309 | |
1333 | /* Save any tiling information, except on overlays */ |
1310 | /* Save any tiling information, except on overlays */ |
1334 | if (flag != 2) |
1311 | if (flag != 2) |
1335 | for (i = 0; i < 4; i++) |
1312 | for (i = 0; i < 4; i++) |
1336 | if (m->tile_path[i]) |
1313 | if (m->tile_path[i]) |
1337 | fprintf (fp, "tile_path_%d %s\n", i + 1, m->tile_path[i]); |
1314 | fprintf (fp, "tile_path_%d %s\n", i + 1, m->tile_path[i]); |
1338 | |
1315 | |
|
|
1316 | fp.put (m); |
1339 | fprintf (fp, "end\n"); |
1317 | fprintf (fp, "end\n"); |
1340 | |
1318 | |
1341 | /* In the game save unique items in the different file, but |
1319 | /* In the game save unique items in the different file, but |
1342 | * in the editor save them to the normal map file. |
1320 | * in the editor save them to the normal map file. |
1343 | * If unique map, save files in the proper destination (set by |
1321 | * If unique map, save files in the proper destination (set by |
… | |
… | |
1466 | mapstruct *tmp, *last; |
1444 | mapstruct *tmp, *last; |
1467 | int i; |
1445 | int i; |
1468 | |
1446 | |
1469 | if (!m) |
1447 | if (!m) |
1470 | return; |
1448 | return; |
|
|
1449 | |
|
|
1450 | m->clear (); |
|
|
1451 | |
1471 | if (m->in_memory == MAP_IN_MEMORY) { |
1452 | if (m->in_memory == MAP_IN_MEMORY) { |
1472 | /* change to MAP_SAVING, even though we are not, |
1453 | /* change to MAP_SAVING, even though we are not, |
1473 | * so that remove_ob doesn't do as much work. |
1454 | * so that remove_ob doesn't do as much work. |
1474 | */ |
1455 | */ |
1475 | m->in_memory = MAP_SAVING; |
1456 | m->in_memory = MAP_SAVING; |
… | |
… | |
1577 | if (! (flags & (MAP_FLUSH|MAP_PLAYER_UNIQUE|MAP_OVERLAY))) { |
1558 | if (! (flags & (MAP_FLUSH|MAP_PLAYER_UNIQUE|MAP_OVERLAY))) { |
1578 | m=load_overlay_map(name, m); |
1559 | m=load_overlay_map(name, m); |
1579 | if (m==NULL) |
1560 | if (m==NULL) |
1580 | return NULL; |
1561 | return NULL; |
1581 | } |
1562 | } |
|
|
1563 | |
|
|
1564 | if (flags & MAP_PLAYER_UNIQUE) |
|
|
1565 | INVOKE_MAP (SWAPIN, m); |
1582 | |
1566 | |
1583 | } else { |
1567 | } else { |
1584 | /* If in this loop, we found a temporary map, so load it up. */ |
1568 | /* If in this loop, we found a temporary map, so load it up. */ |
1585 | |
1569 | |
1586 | m=load_temporary_map (m); |
1570 | m=load_temporary_map (m); |