… | |
… | |
24 | |
24 | |
25 | #include <global.h> |
25 | #include <global.h> |
26 | #include <funcpoint.h> |
26 | #include <funcpoint.h> |
27 | |
27 | |
28 | #include <loader.h> |
28 | #include <loader.h> |
29 | #ifndef WIN32 /* ---win32 exclude header */ |
|
|
30 | # include <unistd.h> |
29 | #include <unistd.h> |
31 | #endif /* win32 */ |
|
|
32 | |
30 | |
33 | #include "path.h" |
31 | #include "path.h" |
34 | |
32 | |
35 | |
|
|
36 | extern int nrofallocobjects, nroffreeobjects; |
|
|
37 | |
|
|
38 | /* |
33 | /* |
39 | * Returns the mapstruct which has a name matching the given argument. |
34 | * Returns the maptile which has a name matching the given argument. |
40 | * return NULL if no match is found. |
35 | * return NULL if no match is found. |
41 | */ |
36 | */ |
42 | |
37 | |
43 | mapstruct * |
38 | maptile * |
44 | has_been_loaded (const char *name) |
39 | has_been_loaded (const char *name) |
45 | { |
40 | { |
46 | mapstruct *map; |
41 | maptile *map; |
47 | |
42 | |
48 | if (!name || !*name) |
43 | if (!name || !*name) |
49 | return 0; |
44 | return 0; |
50 | for (map = first_map; map; map = map->next) |
45 | for (map = first_map; map; map = map->next) |
51 | if (!strcmp (name, map->path)) |
46 | if (!strcmp (name, map->path)) |
… | |
… | |
159 | int |
154 | int |
160 | check_path (const char *name, int prepend_dir) |
155 | check_path (const char *name, int prepend_dir) |
161 | { |
156 | { |
162 | char buf[MAX_BUF]; |
157 | char buf[MAX_BUF]; |
163 | |
158 | |
164 | #ifndef WIN32 |
|
|
165 | char *endbuf; |
159 | char *endbuf; |
166 | struct stat statbuf; |
160 | struct stat statbuf; |
167 | int mode = 0; |
161 | int mode = 0; |
168 | #endif |
|
|
169 | |
162 | |
170 | if (prepend_dir) |
163 | if (prepend_dir) |
171 | strcpy (buf, create_pathname (name)); |
164 | strcpy (buf, create_pathname (name)); |
172 | else |
165 | else |
173 | strcpy (buf, name); |
166 | strcpy (buf, name); |
174 | #ifdef WIN32 /* ***win32: check this sucker in windows style. */ |
|
|
175 | return (_access (buf, 0)); |
|
|
176 | #else |
|
|
177 | |
167 | |
178 | /* old method (strchr(buf, '\0')) seemd very odd to me - |
168 | /* old method (strchr(buf, '\0')) seemd very odd to me - |
179 | * this method should be equivalant and is clearer. |
169 | * this method should be equivalant and is clearer. |
180 | * Can not use strcat because we need to cycle through |
170 | * Can not use strcat because we need to cycle through |
181 | * all the names. |
171 | * all the names. |
… | |
… | |
194 | if ((statbuf.st_mode & S_IWGRP && getegid () == statbuf.st_gid) || |
184 | if ((statbuf.st_mode & S_IWGRP && getegid () == statbuf.st_gid) || |
195 | (statbuf.st_mode & S_IWUSR && geteuid () == statbuf.st_uid) || (statbuf.st_mode & S_IWOTH)) |
185 | (statbuf.st_mode & S_IWUSR && geteuid () == statbuf.st_uid) || (statbuf.st_mode & S_IWOTH)) |
196 | mode |= 2; |
186 | mode |= 2; |
197 | |
187 | |
198 | return (mode); |
188 | return (mode); |
199 | #endif |
|
|
200 | } |
189 | } |
201 | |
190 | |
202 | /* |
191 | /* |
203 | * Prints out debug-information about a map. |
192 | * Prints out debug-information about a map. |
204 | * Dumping these at llevError doesn't seem right, but is |
193 | * Dumping these at llevError doesn't seem right, but is |
205 | * necessary to make sure the information is in fact logged. |
194 | * necessary to make sure the information is in fact logged. |
206 | */ |
195 | */ |
207 | |
196 | |
208 | void |
197 | void |
209 | dump_map (const mapstruct *m) |
198 | dump_map (const maptile *m) |
210 | { |
199 | { |
211 | LOG (llevError, "Map %s status: %d.\n", m->path, m->in_memory); |
200 | LOG (llevError, "Map %s status: %d.\n", m->path, m->in_memory); |
212 | LOG (llevError, "Size: %dx%d Start: %d,%d\n", MAP_WIDTH (m), MAP_HEIGHT (m), MAP_ENTER_X (m), MAP_ENTER_Y (m)); |
201 | LOG (llevError, "Size: %dx%d Start: %d,%d\n", MAP_WIDTH (m), MAP_HEIGHT (m), MAP_ENTER_X (m), MAP_ENTER_Y (m)); |
213 | |
202 | |
214 | if (m->msg != NULL) |
203 | if (m->msg != NULL) |
… | |
… | |
231 | */ |
220 | */ |
232 | |
221 | |
233 | void |
222 | void |
234 | dump_all_maps (void) |
223 | dump_all_maps (void) |
235 | { |
224 | { |
236 | mapstruct *m; |
225 | maptile *m; |
237 | |
226 | |
238 | for (m = first_map; m != NULL; m = m->next) |
227 | for (m = first_map; m != NULL; m = m->next) |
239 | { |
228 | { |
240 | dump_map (m); |
229 | dump_map (m); |
241 | } |
230 | } |
… | |
… | |
250 | * is needed. The case of not passing values is if we're just |
239 | * is needed. The case of not passing values is if we're just |
251 | * checking for the existence of something on those spaces, but |
240 | * checking for the existence of something on those spaces, but |
252 | * don't expect to insert/remove anything from those spaces. |
241 | * don't expect to insert/remove anything from those spaces. |
253 | */ |
242 | */ |
254 | int |
243 | int |
255 | get_map_flags (mapstruct *oldmap, mapstruct **newmap, sint16 x, sint16 y, sint16 * nx, sint16 * ny) |
244 | get_map_flags (maptile *oldmap, maptile **newmap, sint16 x, sint16 y, sint16 * nx, sint16 * ny) |
256 | { |
245 | { |
257 | sint16 newx, newy; |
246 | sint16 newx, newy; |
258 | int retval = 0; |
247 | int retval = 0; |
259 | mapstruct *mp; |
248 | maptile *mp; |
260 | |
249 | |
261 | if (out_of_map (oldmap, x, y)) |
250 | if (out_of_map (oldmap, x, y)) |
262 | return P_OUT_OF_MAP; |
251 | return P_OUT_OF_MAP; |
263 | newx = x; |
252 | newx = x; |
264 | newy = y; |
253 | newy = y; |
… | |
… | |
275 | retval |= mp->spaces[newx + mp->width * newy].flags; |
264 | retval |= mp->spaces[newx + mp->width * newy].flags; |
276 | |
265 | |
277 | return retval; |
266 | return retval; |
278 | } |
267 | } |
279 | |
268 | |
280 | |
|
|
281 | /* |
269 | /* |
282 | * Returns true if the given coordinate is blocked except by the |
270 | * Returns true if the given coordinate is blocked except by the |
283 | * object passed is not blocking. This is used with |
271 | * object passed is not blocking. This is used with |
284 | * multipart monsters - if we want to see if a 2x2 monster |
272 | * multipart monsters - if we want to see if a 2x2 monster |
285 | * can move 1 space to the left, we don't want its own area |
273 | * can move 1 space to the left, we don't want its own area |
… | |
… | |
288 | * monster. |
276 | * monster. |
289 | * m, x, y are the target map/coordinates - needed for map tiling. |
277 | * m, x, y are the target map/coordinates - needed for map tiling. |
290 | * the coordinates & map passed in should have been updated for tiling |
278 | * the coordinates & map passed in should have been updated for tiling |
291 | * by the caller. |
279 | * by the caller. |
292 | */ |
280 | */ |
293 | |
|
|
294 | int |
281 | int |
295 | blocked_link (object *ob, mapstruct *m, int sx, int sy) |
282 | blocked_link (object *ob, maptile *m, int sx, int sy) |
296 | { |
283 | { |
297 | object *tmp; |
284 | object *tmp; |
298 | int mflags, blocked; |
285 | int mflags, blocked; |
299 | |
286 | |
300 | /* Make sure the coordinates are valid - they should be, as caller should |
287 | /* Make sure the coordinates are valid - they should be, as caller should |
… | |
… | |
412 | * code, we need to have actual object to check its move_type |
399 | * code, we need to have actual object to check its move_type |
413 | * against the move_block values. |
400 | * against the move_block values. |
414 | */ |
401 | */ |
415 | |
402 | |
416 | int |
403 | int |
417 | ob_blocked (const object *ob, mapstruct *m, sint16 x, sint16 y) |
404 | ob_blocked (const object *ob, maptile *m, sint16 x, sint16 y) |
418 | { |
405 | { |
419 | archetype *tmp; |
406 | archetype *tmp; |
420 | int flag; |
407 | int flag; |
421 | mapstruct *m1; |
408 | maptile *m1; |
422 | sint16 sx, sy; |
409 | sint16 sx, sy; |
423 | |
410 | |
424 | if (ob == NULL) |
411 | if (ob == NULL) |
425 | { |
412 | { |
426 | flag = get_map_flags (m, &m1, x, y, &sx, &sy); |
413 | flag = get_map_flags (m, &m1, x, y, &sx, &sy); |
… | |
… | |
491 | * they are saved). We do have to look for the old maps that did save |
478 | * they are saved). We do have to look for the old maps that did save |
492 | * the more sections and not re-add sections for them. |
479 | * the more sections and not re-add sections for them. |
493 | */ |
480 | */ |
494 | |
481 | |
495 | static void |
482 | static void |
496 | link_multipart_objects (mapstruct *m) |
483 | link_multipart_objects (maptile *m) |
497 | { |
484 | { |
498 | int x, y; |
485 | int x, y; |
499 | object *tmp, *op, *last, *above; |
486 | object *tmp, *op, *last, *above; |
500 | archetype *at; |
487 | archetype *at; |
501 | |
488 | |
… | |
… | |
538 | * Loads (ands parses) the objects into a given map from the specified |
525 | * Loads (ands parses) the objects into a given map from the specified |
539 | * file pointer. |
526 | * file pointer. |
540 | * mapflags is the same as we get with load_original_map |
527 | * mapflags is the same as we get with load_original_map |
541 | */ |
528 | */ |
542 | void |
529 | void |
543 | load_objects (mapstruct *m, object_thawer & fp, int mapflags) |
530 | load_objects (maptile *m, object_thawer & fp, int mapflags) |
544 | { |
531 | { |
545 | int i, j; |
532 | int i, j; |
546 | int unique; |
533 | int unique; |
547 | object *op, *prev = NULL, *last_more = NULL, *otmp; |
534 | object *op, *prev = NULL, *last_more = NULL, *otmp; |
548 | |
535 | |
549 | op = get_object (); |
536 | op = object::create (); |
550 | op->map = m; /* To handle buttons correctly */ |
537 | op->map = m; /* To handle buttons correctly */ |
551 | |
538 | |
552 | while ((i = load_object (fp, op, mapflags))) |
539 | while ((i = load_object (fp, op, mapflags))) |
553 | { |
540 | { |
554 | /* if the archetype for the object is null, means that we |
541 | /* if the archetype for the object is null, means that we |
… | |
… | |
562 | } |
549 | } |
563 | |
550 | |
564 | |
551 | |
565 | switch (i) |
552 | switch (i) |
566 | { |
553 | { |
567 | case LL_NORMAL: |
554 | case LL_NORMAL: |
568 | /* if we are loading an overlay, put the floors on the bottom */ |
555 | /* if we are loading an overlay, put the floors on the bottom */ |
569 | if ((QUERY_FLAG (op, FLAG_IS_FLOOR) || QUERY_FLAG (op, FLAG_OVERLAY_FLOOR)) && mapflags & MAP_OVERLAY) |
556 | if ((QUERY_FLAG (op, FLAG_IS_FLOOR) || QUERY_FLAG (op, FLAG_OVERLAY_FLOOR)) && mapflags & MAP_OVERLAY) |
570 | insert_ob_in_map (op, m, op, INS_NO_MERGE | INS_NO_WALK_ON | INS_ABOVE_FLOOR_ONLY | INS_MAP_LOAD); |
557 | insert_ob_in_map (op, m, op, INS_NO_MERGE | INS_NO_WALK_ON | INS_ABOVE_FLOOR_ONLY | INS_MAP_LOAD); |
571 | else |
558 | else |
572 | insert_ob_in_map (op, m, op, INS_NO_MERGE | INS_NO_WALK_ON | INS_ON_TOP | INS_MAP_LOAD); |
559 | insert_ob_in_map (op, m, op, INS_NO_MERGE | INS_NO_WALK_ON | INS_ON_TOP | INS_MAP_LOAD); |
573 | |
560 | |
574 | if (op->inv) |
561 | if (op->inv) |
575 | sum_weight (op); |
562 | sum_weight (op); |
576 | |
563 | |
577 | prev = op, last_more = op; |
564 | prev = op, last_more = op; |
578 | break; |
565 | break; |
579 | |
566 | |
580 | case LL_MORE: |
567 | case LL_MORE: |
581 | insert_ob_in_map (op, m, op, INS_NO_MERGE | INS_NO_WALK_ON | INS_ABOVE_FLOOR_ONLY); |
568 | insert_ob_in_map (op, m, op, INS_NO_MERGE | INS_NO_WALK_ON | INS_ABOVE_FLOOR_ONLY); |
582 | op->head = prev, last_more->more = op, last_more = op; |
569 | op->head = prev, last_more->more = op, last_more = op; |
583 | break; |
570 | break; |
584 | } |
571 | } |
585 | |
572 | |
586 | if (mapflags & MAP_STYLE) |
573 | if (mapflags & MAP_STYLE) |
587 | remove_from_active_list (op); |
574 | remove_from_active_list (op); |
588 | |
575 | |
589 | op = get_object (); |
576 | op = object::create (); |
590 | op->map = m; |
577 | op->map = m; |
591 | } |
578 | } |
592 | |
579 | |
593 | for (i = 0; i < m->width; i++) |
580 | for (i = 0; i < m->width; i++) |
594 | { |
581 | { |
… | |
… | |
598 | /* check for unique items, or unique squares */ |
585 | /* check for unique items, or unique squares */ |
599 | for (otmp = get_map_ob (m, i, j); otmp; otmp = otmp->above) |
586 | for (otmp = get_map_ob (m, i, j); otmp; otmp = otmp->above) |
600 | { |
587 | { |
601 | if (QUERY_FLAG (otmp, FLAG_UNIQUE) || QUERY_FLAG (otmp, FLAG_OBJ_SAVE_ON_OVL)) |
588 | if (QUERY_FLAG (otmp, FLAG_UNIQUE) || QUERY_FLAG (otmp, FLAG_OBJ_SAVE_ON_OVL)) |
602 | unique = 1; |
589 | unique = 1; |
|
|
590 | |
603 | if (!(mapflags & (MAP_OVERLAY | MAP_PLAYER_UNIQUE) || unique)) |
591 | if (!(mapflags & (MAP_OVERLAY | MAP_PLAYER_UNIQUE) || unique)) |
604 | SET_FLAG (otmp, FLAG_OBJ_ORIGINAL); |
592 | SET_FLAG (otmp, FLAG_OBJ_ORIGINAL); |
605 | } |
593 | } |
606 | } |
594 | } |
607 | } |
595 | } |
608 | |
596 | |
609 | free_object (op); |
597 | op->destroy (); |
610 | link_multipart_objects (m); |
598 | link_multipart_objects (m); |
611 | } |
599 | } |
612 | |
600 | |
613 | /* This saves all the objects on the map in a non destructive fashion. |
601 | /* This saves all the objects on the map in a non destructive fashion. |
614 | * Modified by MSW 2001-07-01 to do in a single pass - reduces code, |
602 | * Modified by MSW 2001-07-01 to do in a single pass - reduces code, |
615 | * and we only save the head of multi part objects - this is needed |
603 | * and we only save the head of multi part objects - this is needed |
616 | * in order to do map tiling properly. |
604 | * in order to do map tiling properly. |
617 | */ |
605 | */ |
618 | void |
606 | void |
619 | save_objects (mapstruct *m, object_freezer & fp, object_freezer & fp2, int flag) |
607 | save_objects (maptile *m, object_freezer & fp, object_freezer & fp2, int flag) |
620 | { |
608 | { |
621 | int i, j = 0, unique = 0; |
609 | int i, j = 0, unique = 0; |
622 | object *op; |
610 | object *op; |
623 | |
611 | |
624 | /* first pass - save one-part objects */ |
612 | /* first pass - save one-part objects */ |
… | |
… | |
647 | |
635 | |
648 | } /* for this space */ |
636 | } /* for this space */ |
649 | } /* for this j */ |
637 | } /* for this j */ |
650 | } |
638 | } |
651 | |
639 | |
|
|
640 | maptile::maptile () |
|
|
641 | { |
|
|
642 | in_memory = MAP_SWAPPED; |
|
|
643 | /* The maps used to pick up default x and y values from the |
|
|
644 | * map archetype. Mimic that behaviour. |
|
|
645 | */ |
|
|
646 | MAP_WIDTH (this) = 16; |
|
|
647 | MAP_HEIGHT (this) = 16; |
|
|
648 | MAP_RESET_TIMEOUT (this) = 0; |
|
|
649 | MAP_TIMEOUT (this) = 300; |
|
|
650 | MAP_ENTER_X (this) = 0; |
|
|
651 | MAP_ENTER_Y (this) = 0; |
|
|
652 | /*set part to -1 indicating conversion to weather map not yet done */ |
|
|
653 | MAP_WORLDPARTX (this) = -1; |
|
|
654 | MAP_WORLDPARTY (this) = -1; |
|
|
655 | } |
|
|
656 | |
652 | /* |
657 | /* |
653 | * Allocates, initialises, and returns a pointer to a mapstruct. |
658 | * Allocates, initialises, and returns a pointer to a maptile. |
654 | * Modified to no longer take a path option which was not being |
659 | * Modified to no longer take a path option which was not being |
655 | * used anyways. MSW 2001-07-01 |
660 | * used anyways. MSW 2001-07-01 |
656 | */ |
661 | */ |
657 | |
662 | maptile * |
658 | mapstruct * |
|
|
659 | get_linked_map (void) |
663 | get_linked_map (void) |
660 | { |
664 | { |
661 | mapstruct *map = new mapstruct; |
665 | maptile *mp, *map = new maptile; |
662 | mapstruct *mp; |
|
|
663 | |
666 | |
664 | for (mp = first_map; mp != NULL && mp->next != NULL; mp = mp->next); |
667 | for (mp = first_map; mp && mp->next; mp = mp->next); |
|
|
668 | |
665 | if (mp == NULL) |
669 | if (mp == NULL) |
666 | first_map = map; |
670 | first_map = map; |
667 | else |
671 | else |
668 | mp->next = map; |
672 | mp->next = map; |
669 | |
673 | |
670 | map->in_memory = MAP_SWAPPED; |
|
|
671 | /* The maps used to pick up default x and y values from the |
|
|
672 | * map archetype. Mimic that behaviour. |
|
|
673 | */ |
|
|
674 | MAP_WIDTH (map) = 16; |
|
|
675 | MAP_HEIGHT (map) = 16; |
|
|
676 | MAP_RESET_TIMEOUT (map) = 0; |
|
|
677 | MAP_TIMEOUT (map) = 300; |
|
|
678 | MAP_ENTER_X (map) = 0; |
|
|
679 | MAP_ENTER_Y (map) = 0; |
|
|
680 | /*set part to -1 indicating conversion to weather map not yet done */ |
|
|
681 | MAP_WORLDPARTX (map) = -1; |
|
|
682 | MAP_WORLDPARTY (map) = -1; |
|
|
683 | return map; |
674 | return map; |
684 | } |
675 | } |
685 | |
676 | |
686 | /* |
677 | /* |
687 | * Allocates the arrays contained in a mapstruct. |
678 | * Allocates the arrays contained in a maptile. |
688 | * This basically allocates the dynamic array of spaces for the |
679 | * This basically allocates the dynamic array of spaces for the |
689 | * map. |
680 | * map. |
690 | */ |
681 | */ |
691 | |
|
|
692 | void |
682 | void |
693 | allocate_map (mapstruct *m) |
683 | maptile::allocate () |
694 | { |
684 | { |
695 | m->in_memory = MAP_IN_MEMORY; |
685 | in_memory = MAP_IN_MEMORY; |
|
|
686 | |
696 | /* Log this condition and free the storage. We could I suppose |
687 | /* Log this condition and free the storage. We could I suppose |
697 | * realloc, but if the caller is presuming the data will be intact, |
688 | * realloc, but if the caller is presuming the data will be intact, |
698 | * that is their poor assumption. |
689 | * that is their poor assumption. |
699 | */ |
690 | */ |
700 | if (m->spaces) |
691 | if (spaces) |
701 | { |
692 | { |
702 | LOG (llevError, "allocate_map called with already allocated map (%s)\n", m->path); |
693 | LOG (llevError, "allocate_map called with already allocated map (%s)\n", path); |
703 | free (m->spaces); |
694 | free (spaces); |
704 | } |
695 | } |
705 | |
696 | |
706 | m->spaces = (MapSpace *) calloc (1, MAP_WIDTH (m) * MAP_HEIGHT (m) * sizeof (MapSpace)); |
697 | spaces = (MapSpace *) |
|
|
698 | calloc (1, width * height * sizeof (MapSpace)); |
707 | |
699 | |
708 | if (m->spaces == NULL) |
700 | if (!spaces) |
709 | fatal (OUT_OF_MEMORY); |
701 | fatal (OUT_OF_MEMORY); |
710 | } |
702 | } |
711 | |
703 | |
712 | /* Create and returns a map of the specific size. Used |
704 | /* Create and returns a map of the specific size. Used |
713 | * in random map code and the editor. |
705 | * in random map code and the editor. |
714 | */ |
706 | */ |
715 | mapstruct * |
707 | maptile * |
716 | get_empty_map (int sizex, int sizey) |
708 | get_empty_map (int sizex, int sizey) |
717 | { |
709 | { |
718 | mapstruct *m = get_linked_map (); |
710 | maptile *m = get_linked_map (); |
719 | |
711 | |
720 | m->width = sizex; |
712 | m->width = sizex; |
721 | m->height = sizey; |
713 | m->height = sizey; |
722 | m->in_memory = MAP_SWAPPED; |
714 | m->in_memory = MAP_SWAPPED; |
723 | allocate_map (m); |
715 | m->allocate (); |
|
|
716 | |
724 | return m; |
717 | return m; |
725 | } |
718 | } |
726 | |
719 | |
727 | /* Takes a string from a map definition and outputs a pointer to the array of shopitems |
720 | /* Takes a string from a map definition and outputs a pointer to the array of shopitems |
728 | * corresponding to that string. Memory is allocated for this, it must be freed |
721 | * corresponding to that string. Memory is allocated for this, it must be freed |
… | |
… | |
736 | char *shop_string, *p, *q, *next_semicolon, *next_colon; |
729 | char *shop_string, *p, *q, *next_semicolon, *next_colon; |
737 | shopitems *items = NULL; |
730 | shopitems *items = NULL; |
738 | int i = 0, number_of_entries = 0; |
731 | int i = 0, number_of_entries = 0; |
739 | const typedata *current_type; |
732 | const typedata *current_type; |
740 | |
733 | |
741 | shop_string = strdup_local (input_string); |
734 | shop_string = strdup (input_string); |
742 | p = shop_string; |
735 | p = shop_string; |
743 | /* first we'll count the entries, we'll need that for allocating the array shortly */ |
736 | /* first we'll count the entries, we'll need that for allocating the array shortly */ |
744 | while (p) |
737 | while (p) |
745 | { |
738 | { |
746 | p = strchr (p, ';'); |
739 | p = strchr (p, ';'); |
… | |
… | |
805 | } |
798 | } |
806 | |
799 | |
807 | /* opposite of parse string, this puts the string that was originally fed in to |
800 | /* opposite of parse string, this puts the string that was originally fed in to |
808 | * the map (or something equivilent) into output_string. */ |
801 | * the map (or something equivilent) into output_string. */ |
809 | static void |
802 | static void |
810 | print_shop_string (mapstruct *m, char *output_string) |
803 | print_shop_string (maptile *m, char *output_string) |
811 | { |
804 | { |
812 | int i; |
805 | int i; |
813 | char tmp[MAX_BUF]; |
806 | char tmp[MAX_BUF]; |
814 | |
807 | |
815 | strcpy (output_string, ""); |
808 | strcpy (output_string, ""); |
… | |
… | |
849 | * MSW 2001-07-01 |
842 | * MSW 2001-07-01 |
850 | * return 0 on success, 1 on failure. |
843 | * return 0 on success, 1 on failure. |
851 | */ |
844 | */ |
852 | |
845 | |
853 | static int |
846 | static int |
854 | load_map_header (object_thawer & fp, mapstruct *m) |
847 | load_map_header (object_thawer & fp, maptile *m) |
855 | { |
848 | { |
856 | char buf[HUGE_BUF], msgbuf[HUGE_BUF], maplorebuf[HUGE_BUF], *key = NULL, *value, *end; |
849 | char buf[HUGE_BUF], msgbuf[HUGE_BUF], maplorebuf[HUGE_BUF], *key = NULL, *value, *end; |
857 | int msgpos = 0; |
850 | int msgpos = 0; |
858 | int maplorepos = 0; |
851 | int maplorepos = 0; |
859 | |
852 | |
860 | while (fgets (buf, HUGE_BUF - 1, fp) != NULL) |
853 | while (fgets (buf, HUGE_BUF, fp) != NULL) |
861 | { |
854 | { |
862 | buf[HUGE_BUF - 1] = 0; |
855 | buf[HUGE_BUF - 1] = 0; |
863 | key = buf; |
856 | key = buf; |
|
|
857 | |
864 | while (isspace (*key)) |
858 | while (isspace (*key)) |
865 | key++; |
859 | key++; |
|
|
860 | |
866 | if (*key == 0) |
861 | if (*key == 0) |
867 | continue; /* empty line */ |
862 | continue; /* empty line */ |
|
|
863 | |
868 | value = strchr (key, ' '); |
864 | value = strchr (key, ' '); |
|
|
865 | |
869 | if (!value) |
866 | if (!value) |
870 | { |
867 | { |
871 | end = strchr (key, '\n'); |
868 | if ((end = strchr (key, '\n'))) |
872 | if (end != NULL) |
|
|
873 | { |
|
|
874 | *end = 0; |
869 | *end = 0; |
875 | } |
|
|
876 | } |
870 | } |
877 | else |
871 | else |
878 | { |
872 | { |
879 | *value = 0; |
873 | *value = 0; |
880 | value++; |
874 | value++; |
881 | end = strchr (value, '\n'); |
875 | end = strchr (value, '\n'); |
|
|
876 | |
882 | while (isspace (*value)) |
877 | while (isspace (*value)) |
883 | { |
878 | { |
884 | value++; |
879 | value++; |
|
|
880 | |
885 | if (*value == '\0' || value == end) |
881 | if (*value == '\0' || value == end) |
886 | { |
882 | { |
887 | /* Nothing but spaces. */ |
883 | /* Nothing but spaces. */ |
888 | value = NULL; |
884 | value = NULL; |
889 | break; |
885 | break; |
… | |
… | |
912 | * that use the parameter. |
908 | * that use the parameter. |
913 | */ |
909 | */ |
914 | |
910 | |
915 | if (!strcmp (key, "msg")) |
911 | if (!strcmp (key, "msg")) |
916 | { |
912 | { |
917 | while (fgets (buf, HUGE_BUF - 1, fp) != NULL) |
913 | while (fgets (buf, HUGE_BUF, fp) != NULL) |
918 | { |
914 | { |
919 | if (!strcmp (buf, "endmsg\n")) |
915 | if (!strcmp (buf, "endmsg\n")) |
920 | break; |
916 | break; |
921 | else |
917 | else |
922 | { |
918 | { |
… | |
… | |
929 | * with nothing between). There is no reason in those cases to |
925 | * with nothing between). There is no reason in those cases to |
930 | * keep the empty message. Also, msgbuf contains garbage data |
926 | * keep the empty message. Also, msgbuf contains garbage data |
931 | * when msgpos is zero, so copying it results in crashes |
927 | * when msgpos is zero, so copying it results in crashes |
932 | */ |
928 | */ |
933 | if (msgpos != 0) |
929 | if (msgpos != 0) |
934 | m->msg = strdup_local (msgbuf); |
930 | m->msg = strdup (msgbuf); |
935 | } |
931 | } |
936 | else if (!strcmp (key, "maplore")) |
932 | else if (!strcmp (key, "maplore")) |
937 | { |
933 | { |
938 | while (fgets (buf, HUGE_BUF - 1, fp) != NULL) |
934 | while (fgets (buf, HUGE_BUF, fp) != NULL) |
939 | { |
935 | { |
940 | if (!strcmp (buf, "endmaplore\n")) |
936 | if (!strcmp (buf, "endmaplore\n")) |
941 | break; |
937 | break; |
942 | else |
938 | else |
943 | { |
939 | { |
… | |
… | |
945 | strcpy (maplorebuf + maplorepos, buf); |
941 | strcpy (maplorebuf + maplorepos, buf); |
946 | maplorepos += strlen (buf); |
942 | maplorepos += strlen (buf); |
947 | } |
943 | } |
948 | } |
944 | } |
949 | if (maplorepos != 0) |
945 | if (maplorepos != 0) |
950 | m->maplore = strdup_local (maplorebuf); |
946 | m->maplore = strdup (maplorebuf); |
951 | } |
947 | } |
952 | else if (!strcmp (key, "end")) |
948 | else if (!strcmp (key, "end")) |
953 | { |
949 | { |
954 | break; |
950 | break; |
955 | } |
951 | } |
… | |
… | |
964 | LOG (llevError, "loading map and got a non 'arch map' line(%s %s)?\n", key, value); |
960 | LOG (llevError, "loading map and got a non 'arch map' line(%s %s)?\n", key, value); |
965 | } |
961 | } |
966 | else if (!strcmp (key, "name")) |
962 | else if (!strcmp (key, "name")) |
967 | { |
963 | { |
968 | *end = 0; |
964 | *end = 0; |
969 | m->name = strdup_local (value); |
965 | m->name = strdup (value); |
970 | } |
966 | } |
971 | /* first strcmp value on these are old names supported |
967 | /* first strcmp value on these are old names supported |
972 | * for compatibility reasons. The new values (second) are |
968 | * for compatibility reasons. The new values (second) are |
973 | * what really should be used. |
969 | * what really should be used. |
974 | */ |
970 | */ |
975 | else if (!strcmp (key, "oid")) |
971 | else if (!strcmp (key, "oid")) |
976 | { |
|
|
977 | fp.get (m, atoi (value)); |
972 | fp.get (m, atoi (value)); |
978 | } |
|
|
979 | else if (!strcmp (key, "attach")) |
973 | else if (!strcmp (key, "attach")) |
980 | { |
|
|
981 | m->attach = value; |
974 | m->attach = value; |
982 | } |
|
|
983 | else if (!strcmp (key, "hp") || !strcmp (key, "enter_x")) |
975 | else if (!strcmp (key, "hp") || !strcmp (key, "enter_x")) |
984 | { |
|
|
985 | m->enter_x = atoi (value); |
976 | m->enter_x = atoi (value); |
986 | } |
|
|
987 | else if (!strcmp (key, "sp") || !strcmp (key, "enter_y")) |
977 | else if (!strcmp (key, "sp") || !strcmp (key, "enter_y")) |
988 | { |
|
|
989 | m->enter_y = atoi (value); |
978 | m->enter_y = atoi (value); |
990 | } |
|
|
991 | else if (!strcmp (key, "x") || !strcmp (key, "width")) |
979 | else if (!strcmp (key, "x") || !strcmp (key, "width")) |
992 | { |
|
|
993 | m->width = atoi (value); |
980 | m->width = atoi (value); |
994 | } |
|
|
995 | else if (!strcmp (key, "y") || !strcmp (key, "height")) |
981 | else if (!strcmp (key, "y") || !strcmp (key, "height")) |
996 | { |
|
|
997 | m->height = atoi (value); |
982 | m->height = atoi (value); |
998 | } |
|
|
999 | else if (!strcmp (key, "weight") || !strcmp (key, "reset_timeout")) |
983 | else if (!strcmp (key, "weight") || !strcmp (key, "reset_timeout")) |
1000 | { |
|
|
1001 | m->reset_timeout = atoi (value); |
984 | m->reset_timeout = atoi (value); |
1002 | } |
|
|
1003 | else if (!strcmp (key, "value") || !strcmp (key, "swap_time")) |
985 | else if (!strcmp (key, "value") || !strcmp (key, "swap_time")) |
1004 | { |
|
|
1005 | m->timeout = atoi (value); |
986 | m->timeout = atoi (value); |
1006 | } |
|
|
1007 | else if (!strcmp (key, "level") || !strcmp (key, "difficulty")) |
987 | else if (!strcmp (key, "level") || !strcmp (key, "difficulty")) |
1008 | { |
988 | m->difficulty = clamp (atoi (value), 1, settings.max_level); |
1009 | m->difficulty = atoi (value); |
|
|
1010 | } |
|
|
1011 | else if (!strcmp (key, "invisible") || !strcmp (key, "darkness")) |
989 | else if (!strcmp (key, "invisible") || !strcmp (key, "darkness")) |
1012 | { |
|
|
1013 | m->darkness = atoi (value); |
990 | m->darkness = atoi (value); |
1014 | } |
|
|
1015 | else if (!strcmp (key, "stand_still") || !strcmp (key, "fixed_resettime")) |
991 | else if (!strcmp (key, "stand_still") || !strcmp (key, "fixed_resettime")) |
1016 | { |
|
|
1017 | m->fixed_resettime = atoi (value); |
992 | m->fixed_resettime = atoi (value); |
1018 | } |
|
|
1019 | else if (!strcmp (key, "unique")) |
993 | else if (!strcmp (key, "unique")) |
1020 | { |
|
|
1021 | m->unique = atoi (value); |
994 | m->unique = atoi (value); |
1022 | } |
|
|
1023 | else if (!strcmp (key, "template")) |
995 | else if (!strcmp (key, "template")) |
1024 | { |
|
|
1025 | m->templatemap = atoi (value); |
996 | m->templatemap = atoi (value); |
1026 | } |
|
|
1027 | else if (!strcmp (key, "region")) |
997 | else if (!strcmp (key, "region")) |
1028 | { |
|
|
1029 | m->region = get_region_by_name (value); |
998 | m->region = get_region_by_name (value); |
1030 | } |
|
|
1031 | else if (!strcmp (key, "shopitems")) |
999 | else if (!strcmp (key, "shopitems")) |
1032 | { |
1000 | { |
1033 | *end = 0; |
1001 | *end = 0; |
1034 | m->shopitems = parse_shop_string (value); |
1002 | m->shopitems = parse_shop_string (value); |
1035 | } |
1003 | } |
1036 | else if (!strcmp (key, "shopgreed")) |
1004 | else if (!strcmp (key, "shopgreed")) |
1037 | { |
|
|
1038 | m->shopgreed = atof (value); |
1005 | m->shopgreed = atof (value); |
1039 | } |
|
|
1040 | else if (!strcmp (key, "shopmin")) |
1006 | else if (!strcmp (key, "shopmin")) |
1041 | { |
|
|
1042 | m->shopmin = atol (value); |
1007 | m->shopmin = atol (value); |
1043 | } |
|
|
1044 | else if (!strcmp (key, "shopmax")) |
1008 | else if (!strcmp (key, "shopmax")) |
1045 | { |
|
|
1046 | m->shopmax = atol (value); |
1009 | m->shopmax = atol (value); |
1047 | } |
|
|
1048 | else if (!strcmp (key, "shoprace")) |
1010 | else if (!strcmp (key, "shoprace")) |
1049 | { |
1011 | { |
1050 | *end = 0; |
1012 | *end = 0; |
1051 | m->shoprace = strdup_local (value); |
1013 | m->shoprace = strdup (value); |
1052 | } |
1014 | } |
1053 | else if (!strcmp (key, "outdoor")) |
1015 | else if (!strcmp (key, "outdoor")) |
1054 | { |
|
|
1055 | m->outdoor = atoi (value); |
1016 | m->outdoor = atoi (value); |
1056 | } |
|
|
1057 | else if (!strcmp (key, "temp")) |
1017 | else if (!strcmp (key, "temp")) |
1058 | { |
|
|
1059 | m->temp = atoi (value); |
1018 | m->temp = atoi (value); |
1060 | } |
|
|
1061 | else if (!strcmp (key, "pressure")) |
1019 | else if (!strcmp (key, "pressure")) |
1062 | { |
|
|
1063 | m->pressure = atoi (value); |
1020 | m->pressure = atoi (value); |
1064 | } |
|
|
1065 | else if (!strcmp (key, "humid")) |
1021 | else if (!strcmp (key, "humid")) |
1066 | { |
|
|
1067 | m->humid = atoi (value); |
1022 | m->humid = atoi (value); |
1068 | } |
|
|
1069 | else if (!strcmp (key, "windspeed")) |
1023 | else if (!strcmp (key, "windspeed")) |
1070 | { |
|
|
1071 | m->windspeed = atoi (value); |
1024 | m->windspeed = atoi (value); |
1072 | } |
|
|
1073 | else if (!strcmp (key, "winddir")) |
1025 | else if (!strcmp (key, "winddir")) |
1074 | { |
|
|
1075 | m->winddir = atoi (value); |
1026 | m->winddir = atoi (value); |
1076 | } |
|
|
1077 | else if (!strcmp (key, "sky")) |
1027 | else if (!strcmp (key, "sky")) |
1078 | { |
|
|
1079 | m->sky = atoi (value); |
1028 | m->sky = atoi (value); |
1080 | } |
|
|
1081 | else if (!strcmp (key, "nosmooth")) |
1029 | else if (!strcmp (key, "nosmooth")) |
1082 | { |
|
|
1083 | m->nosmooth = atoi (value); |
1030 | m->nosmooth = atoi (value); |
1084 | } |
|
|
1085 | else if (!strncmp (key, "tile_path_", 10)) |
1031 | else if (!strncmp (key, "tile_path_", 10)) |
1086 | { |
1032 | { |
1087 | int tile = atoi (key + 10); |
1033 | int tile = atoi (key + 10); |
1088 | |
1034 | |
1089 | if (tile < 1 || tile > 4) |
1035 | if (tile < 1 || tile > 4) |
… | |
… | |
1122 | } |
1068 | } |
1123 | |
1069 | |
1124 | if (editor) |
1070 | if (editor) |
1125 | { |
1071 | { |
1126 | /* Use the value as in the file. */ |
1072 | /* Use the value as in the file. */ |
1127 | m->tile_path[tile - 1] = strdup_local (value); |
1073 | m->tile_path[tile - 1] = strdup (value); |
1128 | } |
1074 | } |
1129 | else if (path != NULL) |
1075 | else if (path != NULL) |
1130 | { |
1076 | { |
1131 | /* Use the normalized value. */ |
1077 | /* Use the normalized value. */ |
1132 | m->tile_path[tile - 1] = strdup_local (path); |
1078 | m->tile_path[tile - 1] = strdup (path); |
1133 | } |
1079 | } |
1134 | } /* end if tile direction (in)valid */ |
1080 | } /* end if tile direction (in)valid */ |
1135 | } |
1081 | } |
1136 | else |
1082 | else |
1137 | { |
|
|
1138 | LOG (llevError, "Got unknown value in map header: %s %s\n", key, value); |
1083 | LOG (llevError, "Got unknown value in map header: %s %s\n", key, value); |
1139 | } |
|
|
1140 | } |
1084 | } |
|
|
1085 | |
1141 | if (!key || strcmp (key, "end")) |
1086 | if (!key || strcmp (key, "end")) |
1142 | { |
1087 | { |
1143 | LOG (llevError, "Got premature eof on map header!\n"); |
1088 | LOG (llevError, "Got premature eof on map header!\n"); |
1144 | return 1; |
1089 | return 1; |
1145 | } |
1090 | } |
|
|
1091 | |
1146 | return 0; |
1092 | return 0; |
1147 | } |
1093 | } |
1148 | |
1094 | |
1149 | /* |
1095 | /* |
1150 | * Opens the file "filename" and reads information about the map |
1096 | * Opens the file "filename" and reads information about the map |
1151 | * from the given file, and stores it in a newly allocated |
1097 | * from the given file, and stores it in a newly allocated |
1152 | * mapstruct. A pointer to this structure is returned, or NULL on failure. |
1098 | * maptile. A pointer to this structure is returned, or NULL on failure. |
1153 | * flags correspond to those in map.h. Main ones used are |
1099 | * flags correspond to those in map.h. Main ones used are |
1154 | * MAP_PLAYER_UNIQUE, in which case we don't do any name changes, and |
1100 | * MAP_PLAYER_UNIQUE, in which case we don't do any name changes, and |
1155 | * MAP_BLOCK, in which case we block on this load. This happens in all |
1101 | * MAP_BLOCK, in which case we block on this load. This happens in all |
1156 | * cases, no matter if this flag is set or not. |
1102 | * cases, no matter if this flag is set or not. |
1157 | * MAP_STYLE: style map - don't add active objects, don't add to server |
1103 | * MAP_STYLE: style map - don't add active objects, don't add to server |
1158 | * managed map list. |
1104 | * managed map list. |
1159 | */ |
1105 | */ |
1160 | |
1106 | |
1161 | mapstruct * |
1107 | maptile * |
1162 | load_original_map (const char *filename, int flags) |
1108 | load_original_map (const char *filename, int flags) |
1163 | { |
1109 | { |
1164 | mapstruct *m; |
1110 | maptile *m; |
1165 | char pathname[MAX_BUF]; |
1111 | char pathname[MAX_BUF]; |
1166 | |
1112 | |
1167 | if (flags & MAP_PLAYER_UNIQUE) |
1113 | if (flags & MAP_PLAYER_UNIQUE) |
1168 | strcpy (pathname, filename); |
1114 | strcpy (pathname, filename); |
1169 | else if (flags & MAP_OVERLAY) |
1115 | else if (flags & MAP_OVERLAY) |
… | |
… | |
1186 | LOG (llevError, "Error loading map header for %s, flags=%d\n", filename, flags); |
1132 | LOG (llevError, "Error loading map header for %s, flags=%d\n", filename, flags); |
1187 | delete_map (m); |
1133 | delete_map (m); |
1188 | return NULL; |
1134 | return NULL; |
1189 | } |
1135 | } |
1190 | |
1136 | |
1191 | allocate_map (m); |
1137 | m->allocate (); |
1192 | |
1138 | |
1193 | m->in_memory = MAP_LOADING; |
1139 | m->in_memory = MAP_LOADING; |
1194 | load_objects (m, thawer, flags & (MAP_BLOCK | MAP_STYLE)); |
1140 | load_objects (m, thawer, flags & (MAP_BLOCK | MAP_STYLE)); |
1195 | |
1141 | |
1196 | m->in_memory = MAP_IN_MEMORY; |
1142 | m->in_memory = MAP_IN_MEMORY; |
… | |
… | |
1205 | * Loads a map, which has been loaded earlier, from file. |
1151 | * Loads a map, which has been loaded earlier, from file. |
1206 | * Return the map object we load into (this can change from the passed |
1152 | * Return the map object we load into (this can change from the passed |
1207 | * option if we can't find the original map) |
1153 | * option if we can't find the original map) |
1208 | */ |
1154 | */ |
1209 | |
1155 | |
1210 | static mapstruct * |
1156 | static maptile * |
1211 | load_temporary_map (mapstruct *m) |
1157 | load_temporary_map (maptile *m) |
1212 | { |
1158 | { |
1213 | char buf[MAX_BUF]; |
1159 | char buf[MAX_BUF]; |
1214 | |
1160 | |
1215 | if (!m->tmpname) |
1161 | if (!m->tmpname) |
1216 | { |
1162 | { |
… | |
… | |
1242 | LOG (llevError, "Error loading map header for %s (%s)\n", m->path, m->tmpname); |
1188 | LOG (llevError, "Error loading map header for %s (%s)\n", m->path, m->tmpname); |
1243 | delete_map (m); |
1189 | delete_map (m); |
1244 | m = load_original_map (m->path, 0); |
1190 | m = load_original_map (m->path, 0); |
1245 | return NULL; |
1191 | return NULL; |
1246 | } |
1192 | } |
|
|
1193 | |
1247 | allocate_map (m); |
1194 | m->allocate (); |
1248 | |
1195 | |
1249 | m->in_memory = MAP_LOADING; |
1196 | m->in_memory = MAP_LOADING; |
1250 | load_objects (m, thawer, 0); |
1197 | load_objects (m, thawer, 0); |
1251 | |
1198 | |
1252 | m->in_memory = MAP_IN_MEMORY; |
1199 | m->in_memory = MAP_IN_MEMORY; |
… | |
… | |
1258 | * Loads a map, which has been loaded earlier, from file. |
1205 | * Loads a map, which has been loaded earlier, from file. |
1259 | * Return the map object we load into (this can change from the passed |
1206 | * Return the map object we load into (this can change from the passed |
1260 | * option if we can't find the original map) |
1207 | * option if we can't find the original map) |
1261 | */ |
1208 | */ |
1262 | |
1209 | |
1263 | mapstruct * |
1210 | maptile * |
1264 | load_overlay_map (const char *filename, mapstruct *m) |
1211 | load_overlay_map (const char *filename, maptile *m) |
1265 | { |
1212 | { |
1266 | char pathname[MAX_BUF]; |
1213 | char pathname[MAX_BUF]; |
1267 | |
1214 | |
1268 | strcpy (pathname, create_overlay_pathname (filename)); |
1215 | strcpy (pathname, create_overlay_pathname (filename)); |
1269 | |
1216 | |
… | |
… | |
1277 | LOG (llevError, "Error loading map header for overlay %s (%s)\n", m->path, pathname); |
1224 | LOG (llevError, "Error loading map header for overlay %s (%s)\n", m->path, pathname); |
1278 | delete_map (m); |
1225 | delete_map (m); |
1279 | m = load_original_map (m->path, 0); |
1226 | m = load_original_map (m->path, 0); |
1280 | return NULL; |
1227 | return NULL; |
1281 | } |
1228 | } |
1282 | /*allocate_map(m); */ |
1229 | /*m->allocate ();*/ |
1283 | |
1230 | |
1284 | m->in_memory = MAP_LOADING; |
1231 | m->in_memory = MAP_LOADING; |
1285 | load_objects (m, thawer, MAP_OVERLAY); |
1232 | load_objects (m, thawer, MAP_OVERLAY); |
1286 | |
1233 | |
1287 | m->in_memory = MAP_IN_MEMORY; |
1234 | m->in_memory = MAP_IN_MEMORY; |
… | |
… | |
1292 | * This is the start of unique map handling code |
1239 | * This is the start of unique map handling code |
1293 | *****************************************************************************/ |
1240 | *****************************************************************************/ |
1294 | |
1241 | |
1295 | /* This goes through map 'm' and removed any unique items on the map. */ |
1242 | /* This goes through map 'm' and removed any unique items on the map. */ |
1296 | static void |
1243 | static void |
1297 | delete_unique_items (mapstruct *m) |
1244 | delete_unique_items (maptile *m) |
1298 | { |
1245 | { |
1299 | int i, j, unique; |
1246 | int i, j, unique; |
1300 | object *op, *next; |
1247 | object *op, *next; |
1301 | |
1248 | |
1302 | for (i = 0; i < MAP_WIDTH (m); i++) |
1249 | for (i = 0; i < MAP_WIDTH (m); i++) |
1303 | for (j = 0; j < MAP_HEIGHT (m); j++) |
1250 | for (j = 0; j < MAP_HEIGHT (m); j++) |
1304 | { |
1251 | { |
1305 | unique = 0; |
1252 | unique = 0; |
|
|
1253 | |
1306 | for (op = get_map_ob (m, i, j); op; op = next) |
1254 | for (op = get_map_ob (m, i, j); op; op = next) |
1307 | { |
1255 | { |
1308 | next = op->above; |
1256 | next = op->above; |
|
|
1257 | |
1309 | if (QUERY_FLAG (op, FLAG_IS_FLOOR) && QUERY_FLAG (op, FLAG_UNIQUE)) |
1258 | if (QUERY_FLAG (op, FLAG_IS_FLOOR) && QUERY_FLAG (op, FLAG_UNIQUE)) |
1310 | unique = 1; |
1259 | unique = 1; |
|
|
1260 | |
1311 | if (op->head == NULL && (QUERY_FLAG (op, FLAG_UNIQUE) || unique)) |
1261 | if (op->head == NULL && (QUERY_FLAG (op, FLAG_UNIQUE) || unique)) |
1312 | { |
1262 | { |
1313 | clean_object (op); |
1263 | clean_object (op); |
|
|
1264 | |
1314 | if (QUERY_FLAG (op, FLAG_IS_LINKED)) |
1265 | if (QUERY_FLAG (op, FLAG_IS_LINKED)) |
1315 | remove_button_link (op); |
1266 | remove_button_link (op); |
1316 | remove_ob (op); |
1267 | |
1317 | free_object (op); |
1268 | op->destroy (); |
1318 | } |
1269 | } |
1319 | } |
1270 | } |
1320 | } |
1271 | } |
1321 | } |
1272 | } |
1322 | |
1273 | |
… | |
… | |
1324 | /* |
1275 | /* |
1325 | * Loads unique objects from file(s) into the map which is in memory |
1276 | * Loads unique objects from file(s) into the map which is in memory |
1326 | * m is the map to load unique items into. |
1277 | * m is the map to load unique items into. |
1327 | */ |
1278 | */ |
1328 | static void |
1279 | static void |
1329 | load_unique_objects (mapstruct *m) |
1280 | load_unique_objects (maptile *m) |
1330 | { |
1281 | { |
1331 | int count; |
1282 | int count; |
1332 | char firstname[MAX_BUF]; |
1283 | char firstname[MAX_BUF]; |
1333 | |
1284 | |
1334 | for (count = 0; count < 10; count++) |
1285 | for (count = 0; count < 10; count++) |
… | |
… | |
1357 | |
1308 | |
1358 | /* |
1309 | /* |
1359 | * Saves a map to file. If flag is set, it is saved into the same |
1310 | * Saves a map to file. If flag is set, it is saved into the same |
1360 | * file it was (originally) loaded from. Otherwise a temporary |
1311 | * file it was (originally) loaded from. Otherwise a temporary |
1361 | * filename will be genarated, and the file will be stored there. |
1312 | * filename will be genarated, and the file will be stored there. |
1362 | * The temporary filename will be stored in the mapstructure. |
1313 | * The temporary filename will be stored in the maptileure. |
1363 | * If the map is unique, we also save to the filename in the map |
1314 | * If the map is unique, we also save to the filename in the map |
1364 | * (this should have been updated when first loaded) |
1315 | * (this should have been updated when first loaded) |
1365 | */ |
1316 | */ |
1366 | |
1317 | |
1367 | int |
1318 | int |
1368 | new_save_map (mapstruct *m, int flag) |
1319 | new_save_map (maptile *m, int flag) |
1369 | { |
1320 | { |
1370 | char filename[MAX_BUF], buf[MAX_BUF], shop[MAX_BUF]; |
1321 | char filename[MAX_BUF], buf[MAX_BUF], shop[MAX_BUF]; |
1371 | int i; |
1322 | int i; |
1372 | |
1323 | |
1373 | if (flag && !*m->path) |
1324 | if (flag && !*m->path) |
… | |
… | |
1391 | make_path_to_file (filename); |
1342 | make_path_to_file (filename); |
1392 | } |
1343 | } |
1393 | else |
1344 | else |
1394 | { |
1345 | { |
1395 | if (!m->tmpname) |
1346 | if (!m->tmpname) |
1396 | m->tmpname = tempnam_local (settings.tmpdir, NULL); |
1347 | m->tmpname = tempnam (settings.tmpdir, NULL); |
1397 | |
1348 | |
1398 | strcpy (filename, m->tmpname); |
1349 | strcpy (filename, m->tmpname); |
1399 | } |
1350 | } |
1400 | |
1351 | |
1401 | LOG (llevDebug, "Saving map %s to %s\n", m->path, filename); |
1352 | LOG (llevDebug, "Saving map %s to %s\n", m->path, filename); |
… | |
… | |
1425 | print_shop_string (m, shop); |
1376 | print_shop_string (m, shop); |
1426 | fprintf (freezer, "shopitems %s\n", shop); |
1377 | fprintf (freezer, "shopitems %s\n", shop); |
1427 | } |
1378 | } |
1428 | if (m->shopgreed) |
1379 | if (m->shopgreed) |
1429 | fprintf (freezer, "shopgreed %f\n", m->shopgreed); |
1380 | fprintf (freezer, "shopgreed %f\n", m->shopgreed); |
1430 | #ifndef WIN32 |
|
|
1431 | if (m->shopmin) |
1381 | if (m->shopmin) |
1432 | fprintf (freezer, "shopmin %llu\n", m->shopmin); |
1382 | fprintf (freezer, "shopmin %llu\n", m->shopmin); |
1433 | if (m->shopmax) |
1383 | if (m->shopmax) |
1434 | fprintf (freezer, "shopmax %llu\n", m->shopmax); |
1384 | fprintf (freezer, "shopmax %llu\n", m->shopmax); |
1435 | #else |
|
|
1436 | if (m->shopmin) |
|
|
1437 | fprintf (freezer, "shopmin %I64u\n", m->shopmin); |
|
|
1438 | if (m->shopmax) |
|
|
1439 | fprintf (freezer, "shopmax %I64u\n", m->shopmax); |
|
|
1440 | #endif |
|
|
1441 | if (m->shoprace) |
1385 | if (m->shoprace) |
1442 | fprintf (freezer, "shoprace %s\n", m->shoprace); |
1386 | fprintf (freezer, "shoprace %s\n", m->shoprace); |
1443 | if (m->darkness) |
1387 | if (m->darkness) |
1444 | fprintf (freezer, "darkness %d\n", m->darkness); |
1388 | fprintf (freezer, "darkness %d\n", m->darkness); |
1445 | if (m->width) |
1389 | if (m->width) |
… | |
… | |
1524 | object *tmp, *next; |
1468 | object *tmp, *next; |
1525 | |
1469 | |
1526 | for (tmp = op->inv; tmp; tmp = next) |
1470 | for (tmp = op->inv; tmp; tmp = next) |
1527 | { |
1471 | { |
1528 | next = tmp->below; |
1472 | next = tmp->below; |
|
|
1473 | |
1529 | clean_object (tmp); |
1474 | clean_object (tmp); |
1530 | if (QUERY_FLAG (tmp, FLAG_IS_LINKED)) |
1475 | if (QUERY_FLAG (tmp, FLAG_IS_LINKED)) |
1531 | remove_button_link (tmp); |
1476 | remove_button_link (tmp); |
1532 | remove_ob (tmp); |
1477 | |
1533 | free_object (tmp); |
1478 | tmp->destroy (); |
1534 | } |
1479 | } |
1535 | } |
1480 | } |
1536 | |
1481 | |
1537 | /* |
1482 | /* |
1538 | * Remove and free all objects in the given map. |
1483 | * Remove and free all objects in the given map. |
1539 | */ |
1484 | */ |
1540 | |
1485 | |
1541 | void |
1486 | void |
1542 | free_all_objects (mapstruct *m) |
1487 | free_all_objects (maptile *m) |
1543 | { |
1488 | { |
1544 | int i, j; |
1489 | int i, j; |
1545 | object *op; |
1490 | object *op; |
1546 | |
1491 | |
1547 | for (i = 0; i < MAP_WIDTH (m); i++) |
1492 | for (i = 0; i < MAP_WIDTH (m); i++) |
… | |
… | |
1554 | if (op == previous_obj) |
1499 | if (op == previous_obj) |
1555 | { |
1500 | { |
1556 | LOG (llevDebug, "free_all_objects: Link error, bailing out.\n"); |
1501 | LOG (llevDebug, "free_all_objects: Link error, bailing out.\n"); |
1557 | break; |
1502 | break; |
1558 | } |
1503 | } |
|
|
1504 | |
1559 | previous_obj = op; |
1505 | previous_obj = op; |
|
|
1506 | |
1560 | if (op->head != NULL) |
1507 | if (op->head != NULL) |
1561 | op = op->head; |
1508 | op = op->head; |
1562 | |
1509 | |
1563 | /* If the map isn't in memory, free_object will remove and |
1510 | /* If the map isn't in memory, free_object will remove and |
1564 | * free objects in op's inventory. So let it do the job. |
1511 | * free objects in op's inventory. So let it do the job. |
1565 | */ |
1512 | */ |
1566 | if (m->in_memory == MAP_IN_MEMORY) |
1513 | if (m->in_memory == MAP_IN_MEMORY) |
1567 | clean_object (op); |
1514 | clean_object (op); |
1568 | remove_ob (op); |
1515 | |
1569 | free_object (op); |
1516 | op->destroy (); |
1570 | } |
1517 | } |
1571 | } |
1518 | } |
1572 | } |
1519 | } |
1573 | |
1520 | |
1574 | /* |
1521 | /* |
1575 | * Frees everything allocated by the given mapstructure. |
1522 | * Frees everything allocated by the given maptileure. |
1576 | * don't free tmpname - our caller is left to do that |
1523 | * don't free tmpname - our caller is left to do that |
1577 | */ |
1524 | */ |
1578 | |
1525 | |
1579 | void |
1526 | void |
1580 | free_map (mapstruct *m, int flag) |
1527 | free_map (maptile *m, int flag) |
1581 | { |
1528 | { |
1582 | int i; |
1529 | int i; |
1583 | |
1530 | |
1584 | if (!m->in_memory) |
1531 | if (!m->in_memory) |
1585 | { |
1532 | { |
1586 | LOG (llevError, "Trying to free freed map.\n"); |
1533 | LOG (llevError, "Trying to free freed map.\n"); |
1587 | return; |
1534 | return; |
1588 | } |
1535 | } |
|
|
1536 | |
|
|
1537 | // TODO: use new/delete |
|
|
1538 | #define FREE_AND_CLEAR(p) { free (p); p = NULL; } |
|
|
1539 | |
1589 | if (flag && m->spaces) |
1540 | if (flag && m->spaces) |
1590 | free_all_objects (m); |
1541 | free_all_objects (m); |
1591 | if (m->name) |
1542 | if (m->name) |
1592 | FREE_AND_CLEAR (m->name); |
1543 | FREE_AND_CLEAR (m->name); |
1593 | if (m->spaces) |
1544 | if (m->spaces) |
1594 | FREE_AND_CLEAR (m->spaces); |
1545 | FREE_AND_CLEAR (m->spaces); |
1595 | if (m->msg) |
1546 | if (m->msg) |
1596 | FREE_AND_CLEAR (m->msg); |
1547 | FREE_AND_CLEAR (m->msg); |
1597 | if (m->maplore) |
1548 | if (m->maplore) |
1598 | FREE_AND_CLEAR (m->maplore); |
1549 | FREE_AND_CLEAR (m->maplore); |
1599 | if (m->shopitems) |
1550 | |
1600 | delete[]m->shopitems; |
1551 | delete [] m->shopitems; |
1601 | m->shopitems = 0; |
1552 | m->shopitems = 0; |
|
|
1553 | |
1602 | if (m->shoprace) |
1554 | if (m->shoprace) |
1603 | FREE_AND_CLEAR (m->shoprace); |
1555 | FREE_AND_CLEAR (m->shoprace); |
|
|
1556 | |
1604 | if (m->buttons) |
1557 | if (m->buttons) |
1605 | free_objectlinkpt (m->buttons); |
1558 | free_objectlinkpt (m->buttons); |
|
|
1559 | |
1606 | m->buttons = NULL; |
1560 | m->buttons = NULL; |
|
|
1561 | |
1607 | for (i = 0; i < 4; i++) |
1562 | for (i = 0; i < 4; i++) |
1608 | { |
1563 | { |
1609 | if (m->tile_path[i]) |
1564 | if (m->tile_path[i]) |
1610 | FREE_AND_CLEAR (m->tile_path[i]); |
1565 | FREE_AND_CLEAR (m->tile_path[i]); |
1611 | m->tile_map[i] = NULL; |
1566 | m->tile_map[i] = NULL; |
1612 | } |
1567 | } |
|
|
1568 | |
1613 | m->in_memory = MAP_SWAPPED; |
1569 | m->in_memory = MAP_SWAPPED; |
|
|
1570 | |
|
|
1571 | #undef FREE_AND_CLEAR |
|
|
1572 | |
1614 | } |
1573 | } |
1615 | |
1574 | |
1616 | /* |
1575 | /* |
1617 | * function: vanish mapstruct |
1576 | * function: vanish maptile |
1618 | * m : pointer to mapstruct, if NULL no action |
1577 | * m : pointer to maptile, if NULL no action |
1619 | * this deletes all the data on the map (freeing pointers) |
1578 | * this deletes all the data on the map (freeing pointers) |
1620 | * and then removes this map from the global linked list of maps. |
1579 | * and then removes this map from the global linked list of maps. |
1621 | */ |
1580 | */ |
1622 | |
1581 | |
1623 | void |
1582 | void |
1624 | delete_map (mapstruct *m) |
1583 | delete_map (maptile *m) |
1625 | { |
1584 | { |
1626 | mapstruct *tmp, *last; |
1585 | maptile *tmp, *last; |
1627 | int i; |
1586 | int i; |
1628 | |
1587 | |
1629 | if (!m) |
1588 | if (!m) |
1630 | return; |
1589 | return; |
1631 | |
1590 | |
… | |
… | |
1678 | else |
1637 | else |
1679 | last->next = m->next; |
1638 | last->next = m->next; |
1680 | |
1639 | |
1681 | delete m; |
1640 | delete m; |
1682 | } |
1641 | } |
1683 | |
|
|
1684 | |
|
|
1685 | |
1642 | |
1686 | /* |
1643 | /* |
1687 | * Makes sure the given map is loaded and swapped in. |
1644 | * Makes sure the given map is loaded and swapped in. |
1688 | * name is path name of the map. |
1645 | * name is path name of the map. |
1689 | * flags meaning: |
1646 | * flags meaning: |
… | |
… | |
1692 | * 0x2 (MAP_PLAYER_UNIQUE) - this is a unique map for each player. |
1649 | * 0x2 (MAP_PLAYER_UNIQUE) - this is a unique map for each player. |
1693 | * dont do any more name translation on it. |
1650 | * dont do any more name translation on it. |
1694 | * |
1651 | * |
1695 | * Returns a pointer to the given map. |
1652 | * Returns a pointer to the given map. |
1696 | */ |
1653 | */ |
1697 | |
1654 | maptile * |
1698 | mapstruct * |
|
|
1699 | ready_map_name (const char *name, int flags) |
1655 | ready_map_name (const char *name, int flags) |
1700 | { |
1656 | { |
1701 | mapstruct *m; |
1657 | maptile *m; |
1702 | |
1658 | |
1703 | if (!name) |
1659 | if (!name) |
1704 | return (NULL); |
1660 | return (NULL); |
1705 | |
1661 | |
1706 | /* Have we been at this level before? */ |
1662 | /* Have we been at this level before? */ |
… | |
… | |
1808 | * have a difficulty set than using this function - human calculation |
1764 | * have a difficulty set than using this function - human calculation |
1809 | * is much better than this functions guesswork. |
1765 | * is much better than this functions guesswork. |
1810 | */ |
1766 | */ |
1811 | |
1767 | |
1812 | int |
1768 | int |
1813 | calculate_difficulty (mapstruct *m) |
1769 | calculate_difficulty (maptile *m) |
1814 | { |
1770 | { |
1815 | object *op; |
1771 | object *op; |
1816 | archetype *at; |
1772 | archetype *at; |
1817 | int x, y, i; |
1773 | int x, y, i; |
1818 | long monster_cnt = 0; |
1774 | long monster_cnt = 0; |
… | |
… | |
1860 | |
1816 | |
1861 | return 1; |
1817 | return 1; |
1862 | } |
1818 | } |
1863 | |
1819 | |
1864 | void |
1820 | void |
1865 | clean_tmp_map (mapstruct *m) |
1821 | clean_tmp_map (maptile *m) |
1866 | { |
1822 | { |
1867 | if (m->tmpname == NULL) |
1823 | if (m->tmpname == NULL) |
1868 | return; |
1824 | return; |
1869 | INVOKE_MAP (CLEAN, m); |
1825 | INVOKE_MAP (CLEAN, m); |
1870 | (void) unlink (m->tmpname); |
1826 | (void) unlink (m->tmpname); |
… | |
… | |
1895 | * to maps than los. |
1851 | * to maps than los. |
1896 | * postive values make it darker, negative make it brighter |
1852 | * postive values make it darker, negative make it brighter |
1897 | */ |
1853 | */ |
1898 | |
1854 | |
1899 | int |
1855 | int |
1900 | change_map_light (mapstruct *m, int change) |
1856 | change_map_light (maptile *m, int change) |
1901 | { |
1857 | { |
1902 | int new_level = m->darkness + change; |
1858 | int new_level = m->darkness + change; |
1903 | |
1859 | |
1904 | /* Nothing to do */ |
1860 | /* Nothing to do */ |
1905 | if (!change || (new_level <= 0 && m->darkness == 0) || (new_level >= MAX_DARKNESS && m->darkness >= MAX_DARKNESS)) |
1861 | if (!change || (new_level <= 0 && m->darkness == 0) || (new_level >= MAX_DARKNESS && m->darkness >= MAX_DARKNESS)) |
… | |
… | |
1936 | * on the map (what it looks like, whether it blocks magic, |
1892 | * on the map (what it looks like, whether it blocks magic, |
1937 | * has a living creatures, prevents people from passing |
1893 | * has a living creatures, prevents people from passing |
1938 | * through, etc) |
1894 | * through, etc) |
1939 | */ |
1895 | */ |
1940 | void |
1896 | void |
1941 | update_position (mapstruct *m, int x, int y) |
1897 | update_position (maptile *m, int x, int y) |
1942 | { |
1898 | { |
1943 | object *tmp, *last = NULL; |
1899 | object *tmp, *last = NULL; |
1944 | uint8 flags = 0, oldflags, light = 0, anywhere = 0; |
1900 | uint8 flags = 0, oldflags, light = 0, anywhere = 0; |
1945 | New_Face *top, *floor, *middle; |
1901 | New_Face *top, *floor, *middle; |
1946 | object *top_obj, *floor_obj, *middle_obj; |
1902 | object *top_obj, *floor_obj, *middle_obj; |
… | |
… | |
2139 | SET_MAP_LIGHT (m, x, y, light); |
2095 | SET_MAP_LIGHT (m, x, y, light); |
2140 | } |
2096 | } |
2141 | |
2097 | |
2142 | |
2098 | |
2143 | void |
2099 | void |
2144 | set_map_reset_time (mapstruct *map) |
2100 | set_map_reset_time (maptile *map) |
2145 | { |
2101 | { |
2146 | int timeout; |
2102 | int timeout; |
2147 | |
2103 | |
2148 | timeout = MAP_RESET_TIMEOUT (map); |
2104 | timeout = MAP_RESET_TIMEOUT (map); |
2149 | if (timeout <= 0) |
2105 | if (timeout <= 0) |
2150 | timeout = MAP_DEFAULTRESET; |
2106 | timeout = MAP_DEFAULTRESET; |
2151 | if (timeout >= MAP_MAXRESET) |
2107 | if (timeout >= MAP_MAXRESET) |
2152 | timeout = MAP_MAXRESET; |
2108 | timeout = MAP_MAXRESET; |
2153 | MAP_WHEN_RESET (map) = seconds () + timeout; |
2109 | MAP_WHEN_RESET (map) = time (0) + timeout; |
2154 | } |
2110 | } |
2155 | |
2111 | |
2156 | /* this updates the orig_map->tile_map[tile_num] value after loading |
2112 | /* this updates the orig_map->tile_map[tile_num] value after loading |
2157 | * the map. It also takes care of linking back the freshly loaded |
2113 | * the map. It also takes care of linking back the freshly loaded |
2158 | * maps tile_map values if it tiles back to this one. It returns |
2114 | * maps tile_map values if it tiles back to this one. It returns |
2159 | * the value of orig_map->tile_map[tile_num]. It really only does this |
2115 | * the value of orig_map->tile_map[tile_num]. It really only does this |
2160 | * so that it is easier for calling functions to verify success. |
2116 | * so that it is easier for calling functions to verify success. |
2161 | */ |
2117 | */ |
2162 | |
2118 | |
2163 | static mapstruct * |
2119 | static maptile * |
2164 | load_and_link_tiled_map (mapstruct *orig_map, int tile_num) |
2120 | load_and_link_tiled_map (maptile *orig_map, int tile_num) |
2165 | { |
2121 | { |
2166 | int dest_tile = (tile_num + 2) % 4; |
2122 | int dest_tile = (tile_num + 2) % 4; |
2167 | char *path = path_combine_and_normalize (orig_map->path, orig_map->tile_path[tile_num]); |
2123 | char *path = path_combine_and_normalize (orig_map->path, orig_map->tile_path[tile_num]); |
2168 | |
2124 | |
2169 | orig_map->tile_map[tile_num] = ready_map_name (path, 0); |
2125 | orig_map->tile_map[tile_num] = ready_map_name (path, 0); |
… | |
… | |
2184 | * tiled maps. |
2140 | * tiled maps. |
2185 | * |
2141 | * |
2186 | * |
2142 | * |
2187 | */ |
2143 | */ |
2188 | int |
2144 | int |
2189 | out_of_map (mapstruct *m, int x, int y) |
2145 | out_of_map (maptile *m, int x, int y) |
2190 | { |
2146 | { |
2191 | |
2147 | |
2192 | /* If we get passed a null map, this is obviously the |
2148 | /* If we get passed a null map, this is obviously the |
2193 | * case. This generally shouldn't happen, but if the |
2149 | * case. This generally shouldn't happen, but if the |
2194 | * map loads fail below, it could happen. |
2150 | * map loads fail below, it could happen. |
… | |
… | |
2249 | * the map as that the coordinates are really on, and |
2205 | * the map as that the coordinates are really on, and |
2250 | * updates x and y to be the localized coordinates. |
2206 | * updates x and y to be the localized coordinates. |
2251 | * Using this is more efficient of calling out_of_map |
2207 | * Using this is more efficient of calling out_of_map |
2252 | * and then figuring out what the real map is |
2208 | * and then figuring out what the real map is |
2253 | */ |
2209 | */ |
2254 | mapstruct * |
2210 | maptile * |
2255 | get_map_from_coord (mapstruct *m, sint16 * x, sint16 * y) |
2211 | get_map_from_coord (maptile *m, sint16 * x, sint16 * y) |
2256 | { |
2212 | { |
2257 | |
2213 | |
2258 | if (*x < 0) |
2214 | if (*x < 0) |
2259 | { |
2215 | { |
2260 | if (!m->tile_path[3]) |
2216 | if (!m->tile_path[3]) |
… | |
… | |
2306 | /** |
2262 | /** |
2307 | * Return whether map2 is adjacent to map1. If so, store the distance from |
2263 | * Return whether map2 is adjacent to map1. If so, store the distance from |
2308 | * map1 to map2 in dx/dy. |
2264 | * map1 to map2 in dx/dy. |
2309 | */ |
2265 | */ |
2310 | static int |
2266 | static int |
2311 | adjacent_map (const mapstruct *map1, const mapstruct *map2, int *dx, int *dy) |
2267 | adjacent_map (const maptile *map1, const maptile *map2, int *dx, int *dy) |
2312 | { |
2268 | { |
2313 | if (!map1 || !map2) |
2269 | if (!map1 || !map2) |
2314 | return 0; |
2270 | return 0; |
2315 | |
2271 | |
2316 | if (map1 == map2) |
2272 | if (map1 == map2) |
… | |
… | |
2474 | * for something in the future. Also, since no object is pasted, the best |
2430 | * for something in the future. Also, since no object is pasted, the best |
2475 | * field of the rv_vector is set to NULL. |
2431 | * field of the rv_vector is set to NULL. |
2476 | */ |
2432 | */ |
2477 | |
2433 | |
2478 | void |
2434 | void |
2479 | get_rangevector_from_mapcoord (const mapstruct *m, int x, int y, const object *op2, rv_vector * retval, int flags) |
2435 | get_rangevector_from_mapcoord (const maptile *m, int x, int y, const object *op2, rv_vector * retval, int flags) |
2480 | { |
2436 | { |
2481 | if (!adjacent_map (m, op2->map, &retval->distance_x, &retval->distance_y)) |
2437 | if (!adjacent_map (m, op2->map, &retval->distance_x, &retval->distance_y)) |
2482 | { |
2438 | { |
2483 | /* be conservative and fill in _some_ data */ |
2439 | /* be conservative and fill in _some_ data */ |
2484 | retval->distance = 100000; |
2440 | retval->distance = 100000; |