1 | /* |
1 | /* |
2 | * static char *rcsid_map_c = |
2 | * static char *rcsid_map_c = |
3 | * "$Id: map.c,v 1.4 2006/02/22 18:53:48 elmex Exp $"; |
3 | * "$Id: map.c,v 1.12 2006/05/17 18:32:50 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 | |
… | |
… | |
38 | #include "path.h" |
38 | #include "path.h" |
39 | |
39 | |
40 | |
40 | |
41 | extern int nrofallocobjects,nroffreeobjects; |
41 | extern int nrofallocobjects,nroffreeobjects; |
42 | |
42 | |
|
|
43 | void (*load_original_map_callback)(mapstruct *map); |
|
|
44 | void (*load_temporary_map_callback)(mapstruct *map); |
|
|
45 | void (*clean_temporary_map_callback)(mapstruct *map); |
43 | |
46 | |
44 | /* |
47 | /* |
45 | * Returns the mapstruct which has a name matching the given argument. |
48 | * Returns the mapstruct which has a name matching the given argument. |
46 | * return NULL if no match is found. |
49 | * return NULL if no match is found. |
47 | */ |
50 | */ |
… | |
… | |
587 | * and we only save the head of multi part objects - this is needed |
590 | * and we only save the head of multi part objects - this is needed |
588 | * in order to do map tiling properly. |
591 | * in order to do map tiling properly. |
589 | */ |
592 | */ |
590 | void save_objects (mapstruct *m, FILE *fp, FILE *fp2, int flag) { |
593 | void save_objects (mapstruct *m, FILE *fp, FILE *fp2, int flag) { |
591 | int i, j = 0,unique=0; |
594 | int i, j = 0,unique=0; |
592 | object *op, *otmp; |
595 | object *op; |
593 | /* first pass - save one-part objects */ |
596 | /* first pass - save one-part objects */ |
594 | for(i = 0; i < MAP_WIDTH(m); i++) |
597 | for(i = 0; i < MAP_WIDTH(m); i++) |
595 | for (j = 0; j < MAP_HEIGHT(m); j++) { |
598 | for (j = 0; j < MAP_HEIGHT(m); j++) { |
596 | unique=0; |
599 | unique=0; |
597 | for(op = get_map_ob (m, i, j); op; op = otmp) { |
600 | for(op = get_map_ob (m, i, j); op; op = op->above) { |
598 | otmp = op->above; |
|
|
599 | |
|
|
600 | if (QUERY_FLAG(op,FLAG_IS_FLOOR) && QUERY_FLAG(op, FLAG_UNIQUE)) |
601 | if (QUERY_FLAG(op,FLAG_IS_FLOOR) && QUERY_FLAG(op, FLAG_UNIQUE)) |
601 | unique=1; |
602 | unique=1; |
602 | |
603 | |
603 | if(op->type == PLAYER) { |
604 | if(op->type == PLAYER) { |
604 | LOG(llevDebug, "Player on map that is being saved\n"); |
605 | LOG(llevDebug, "Player on map that is being saved\n"); |
… | |
… | |
676 | |
677 | |
677 | if(m->spaces==NULL) |
678 | if(m->spaces==NULL) |
678 | fatal(OUT_OF_MEMORY); |
679 | fatal(OUT_OF_MEMORY); |
679 | } |
680 | } |
680 | |
681 | |
681 | /* Creatures and returns a map of the specific size. Used |
682 | /* Create and returns a map of the specific size. Used |
682 | * in random map code and the editor. |
683 | * in random map code and the editor. |
683 | */ |
684 | */ |
684 | mapstruct *get_empty_map(int sizex, int sizey) { |
685 | mapstruct *get_empty_map(int sizex, int sizey) { |
685 | mapstruct *m = get_linked_map(); |
686 | mapstruct *m = get_linked_map(); |
686 | m->width = sizex; |
687 | m->width = sizex; |
… | |
… | |
702 | int i=0, number_of_entries=0; |
703 | int i=0, number_of_entries=0; |
703 | const typedata *current_type; |
704 | const typedata *current_type; |
704 | |
705 | |
705 | shop_string=strdup_local(input_string); |
706 | shop_string=strdup_local(input_string); |
706 | p=shop_string; |
707 | p=shop_string; |
707 | LOG(llevDebug, "parsing %s\n", input_string); |
|
|
708 | /* first we'll count the entries, we'll need that for allocating the array shortly */ |
708 | /* first we'll count the entries, we'll need that for allocating the array shortly */ |
709 | while (p) { |
709 | while (p) { |
710 | p=strchr(p, ';'); |
710 | p=strchr(p, ';'); |
711 | number_of_entries++; |
711 | number_of_entries++; |
712 | if (p) p++; |
712 | if (p) p++; |
… | |
… | |
1049 | close_and_delete(fp, comp); |
1049 | close_and_delete(fp, comp); |
1050 | m->in_memory=MAP_IN_MEMORY; |
1050 | m->in_memory=MAP_IN_MEMORY; |
1051 | if (!MAP_DIFFICULTY(m)) |
1051 | if (!MAP_DIFFICULTY(m)) |
1052 | MAP_DIFFICULTY(m)=calculate_difficulty(m); |
1052 | MAP_DIFFICULTY(m)=calculate_difficulty(m); |
1053 | set_map_reset_time(m); |
1053 | set_map_reset_time(m); |
|
|
1054 | if (load_original_map_callback) |
|
|
1055 | load_original_map_callback(m); |
1054 | return (m); |
1056 | return (m); |
1055 | } |
1057 | } |
1056 | |
1058 | |
1057 | /* |
1059 | /* |
1058 | * Loads a map, which has been loaded earlier, from file. |
1060 | * Loads a map, which has been loaded earlier, from file. |
… | |
… | |
1098 | |
1100 | |
1099 | m->in_memory=MAP_LOADING; |
1101 | m->in_memory=MAP_LOADING; |
1100 | load_objects (m, fp, 0); |
1102 | load_objects (m, fp, 0); |
1101 | close_and_delete(fp, comp); |
1103 | close_and_delete(fp, comp); |
1102 | m->in_memory=MAP_IN_MEMORY; |
1104 | m->in_memory=MAP_IN_MEMORY; |
|
|
1105 | if (load_temporary_map_callback) |
|
|
1106 | load_temporary_map_callback(m); |
1103 | return m; |
1107 | return m; |
1104 | } |
1108 | } |
1105 | |
1109 | |
1106 | /* |
1110 | /* |
1107 | * Loads a map, which has been loaded earlier, from file. |
1111 | * Loads a map, which has been loaded earlier, from file. |
… | |
… | |
1143 | *****************************************************************************/ |
1147 | *****************************************************************************/ |
1144 | |
1148 | |
1145 | /* This goes through map 'm' and removed any unique items on the map. */ |
1149 | /* This goes through map 'm' and removed any unique items on the map. */ |
1146 | static void delete_unique_items(mapstruct *m) |
1150 | static void delete_unique_items(mapstruct *m) |
1147 | { |
1151 | { |
1148 | int i,j,unique=0; |
1152 | int i,j,unique; |
1149 | object *op, *next; |
1153 | object *op, *next; |
1150 | |
1154 | |
1151 | for(i=0; i<MAP_WIDTH(m); i++) |
1155 | for(i=0; i<MAP_WIDTH(m); i++) |
1152 | for(j=0; j<MAP_HEIGHT(m); j++) { |
1156 | for(j=0; j<MAP_HEIGHT(m); j++) { |
1153 | unique=0; |
1157 | unique=0; |
… | |
… | |
1243 | m->tmpname = tempnam_local(settings.tmpdir,NULL); |
1247 | m->tmpname = tempnam_local(settings.tmpdir,NULL); |
1244 | strcpy(filename, m->tmpname); |
1248 | strcpy(filename, m->tmpname); |
1245 | } |
1249 | } |
1246 | LOG(llevDebug,"Saving map %s\n",m->path); |
1250 | LOG(llevDebug,"Saving map %s\n",m->path); |
1247 | m->in_memory = MAP_SAVING; |
1251 | m->in_memory = MAP_SAVING; |
|
|
1252 | |
|
|
1253 | unlink (filename); // do not overwrite backups if done via hardlinks |
1248 | |
1254 | |
1249 | /* Compress if it isn't a temporary save. Do compress if unique */ |
1255 | /* Compress if it isn't a temporary save. Do compress if unique */ |
1250 | if (m->compressed && (m->unique || m->template || flag)) { |
1256 | if (m->compressed && (m->unique || m->template || flag)) { |
1251 | char buf[MAX_BUF]; |
1257 | char buf[MAX_BUF]; |
1252 | strcpy(buf, uncomp[m->compressed][2]); |
1258 | strcpy(buf, uncomp[m->compressed][2]); |
… | |
… | |
1657 | } |
1663 | } |
1658 | |
1664 | |
1659 | void clean_tmp_map(mapstruct *m) { |
1665 | void clean_tmp_map(mapstruct *m) { |
1660 | if(m->tmpname == NULL) |
1666 | if(m->tmpname == NULL) |
1661 | return; |
1667 | return; |
|
|
1668 | if (clean_temporary_map_callback) |
|
|
1669 | clean_temporary_map_callback (m); |
1662 | (void) unlink(m->tmpname); |
1670 | (void) unlink(m->tmpname); |
1663 | } |
1671 | } |
1664 | |
1672 | |
1665 | void free_all_maps(void) |
1673 | void free_all_maps(void) |
1666 | { |
1674 | { |
… | |
… | |
1694 | return 0; |
1702 | return 0; |
1695 | } |
1703 | } |
1696 | |
1704 | |
1697 | /* inform all players on the map */ |
1705 | /* inform all players on the map */ |
1698 | if (change>0) |
1706 | if (change>0) |
1699 | new_info_map(NDI_BLACK, m,"It becomes darker."); |
1707 | new_info_map(NDI_BLACK|NDI_UNIQUE, m,"It becomes darker."); |
1700 | else |
1708 | else |
1701 | new_info_map(NDI_BLACK, m,"It becomes brighter."); |
1709 | new_info_map(NDI_BLACK|NDI_UNIQUE, m,"It becomes brighter."); |
1702 | |
1710 | |
1703 | /* Do extra checking. since m->darkness is a unsigned value, |
1711 | /* Do extra checking. since m->darkness is a unsigned value, |
1704 | * we need to be extra careful about negative values. |
1712 | * we need to be extra careful about negative values. |
1705 | * In general, the checks below are only needed if change |
1713 | * In general, the checks below are only needed if change |
1706 | * is not +/-1 |
1714 | * is not +/-1 |
… | |
… | |
2037 | */ |
2045 | */ |
2038 | |
2046 | |
2039 | return m; |
2047 | return m; |
2040 | } |
2048 | } |
2041 | |
2049 | |
2042 | // return wether map2 is adjacent to map1 and store their distance |
|
|
2043 | // in dx/dy if yes. |
|
|
2044 | static int adjacent_map (mapstruct *map1, mapstruct *map2, int *dx, int *dy) |
|
|
2045 | { |
|
|
2046 | if (!map1 || !map2) |
|
|
2047 | return 0; |
|
|
2048 | |
|
|
2049 | else if (map1 == map2) |
|
|
2050 | *dx = *dy = 0; |
|
|
2051 | |
|
|
2052 | else if (map1->tile_map[0] == map2) // up |
|
|
2053 | (*dx = 0), (*dy = -MAP_HEIGHT (map2)); |
|
|
2054 | else if (map1->tile_map[1] == map2) // right |
|
|
2055 | (*dx = MAP_WIDTH (map2)), (*dy = 0); |
|
|
2056 | else if (map1->tile_map[2] == map2) // down |
|
|
2057 | (*dx = 0), (*dy = MAP_HEIGHT (map2)); |
|
|
2058 | else if (map1->tile_map[3] == map2) // left |
|
|
2059 | (*dx = -MAP_WIDTH (map2)), (*dy = 0); |
|
|
2060 | |
|
|
2061 | else if (map1->tile_map[0] && map1->tile_map[0]->tile_map[1] == map2) // up right |
|
|
2062 | (*dx = MAP_WIDTH (map2)), (*dy = -MAP_HEIGHT (map1->tile_map[0])); |
|
|
2063 | else if (map1->tile_map[0] && map1->tile_map[0]->tile_map[3] == map2) // up left |
|
|
2064 | (*dx = -MAP_WIDTH (map2)), (*dy = -MAP_HEIGHT (map1->tile_map[0])); |
|
|
2065 | else if (map1->tile_map[1] && map1->tile_map[1]->tile_map[0] == map2) // right up |
|
|
2066 | (*dx = MAP_HEIGHT (map1->tile_map[1])), (*dy = -MAP_WIDTH (map2)); |
|
|
2067 | else if (map1->tile_map[1] && map1->tile_map[1]->tile_map[2] == map2) // right down |
|
|
2068 | (*dx = MAP_HEIGHT (map1->tile_map[1])), (*dy = MAP_WIDTH (map2)); |
|
|
2069 | else if (map1->tile_map[2] && map1->tile_map[2]->tile_map[1] == map2) // down right |
|
|
2070 | (*dx = MAP_WIDTH (map2)), (*dy = MAP_HEIGHT (map1->tile_map[2])); |
|
|
2071 | else if (map1->tile_map[2] && map1->tile_map[2]->tile_map[3] == map2) // down left |
|
|
2072 | (*dx = -MAP_WIDTH (map2)), (*dy = MAP_HEIGHT (map1->tile_map[2])); |
|
|
2073 | else if (map1->tile_map[3] && map1->tile_map[3]->tile_map[0] == map2) // left up |
|
|
2074 | (*dx = -MAP_HEIGHT (map1->tile_map[3])), (*dy = -MAP_WIDTH (map2)); |
|
|
2075 | else if (map1->tile_map[3] && map1->tile_map[3]->tile_map[2] == map2) // left down |
|
|
2076 | (*dx = MAP_HEIGHT (map1->tile_map[3])), (*dy = MAP_WIDTH (map2)); |
|
|
2077 | |
|
|
2078 | else // not "adjacent" enough |
|
|
2079 | return 0; |
|
|
2080 | |
|
|
2081 | return 1; |
|
|
2082 | } |
|
|
2083 | |
|
|
2084 | /** |
2050 | /** |
2085 | * Return whether map2 is adjacent to map1. If so, store the distance from |
2051 | * Return whether map2 is adjacent to map1. If so, store the distance from |
2086 | * map1 to map2 in dx/dy. |
2052 | * map1 to map2 in dx/dy. |
2087 | */ |
2053 | */ |
2088 | static int adjacent_map(const mapstruct *map1, const mapstruct *map2, int *dx, int *dy) { |
2054 | static int adjacent_map(const mapstruct *map1, const mapstruct *map2, int *dx, int *dy) { |