ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/common/map.C
Revision: 1.61
Committed: Sun Dec 31 17:17:22 2006 UTC (17 years, 4 months ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.60: +4 -4 lines
Log Message:
many minor changes everywhere, random maps crash sometimes but design is in place

File Contents

# Content
1 /*
2 CrossFire, A Multiplayer game for X-windows
3
4 Copyright (C) 2001-2003 Mark Wedel & Crossfire Development Team
5 Copyright (C) 1992 Frank Tore Johansen
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20
21 The authors can be reached via e-mail at <crossfire@schmorp.de>
22 */
23
24 #include <global.h>
25 #include <funcpoint.h>
26
27 #include <loader.h>
28 #include <unistd.h>
29
30 #include "path.h"
31
32 /*
33 * This makes a path absolute outside the world of Crossfire.
34 * In other words, it prepends LIBDIR/MAPDIR/ to the given path
35 * and returns the pointer to a static array containing the result.
36 * it really should be called create_mapname
37 */
38 const char *
39 create_pathname (const char *name)
40 {
41 static char buf[MAX_BUF];
42
43 /* Why? having extra / doesn't confuse unix anyplace? Dependancies
44 * someplace else in the code? msw 2-17-97
45 */
46 if (*name == '/')
47 sprintf (buf, "%s/%s%s", settings.datadir, settings.mapdir, name);
48 else
49 sprintf (buf, "%s/%s/%s", settings.datadir, settings.mapdir, name);
50 return (buf);
51 }
52
53 /*
54 * same as create_pathname, but for the overlay maps.
55 */
56 const char *
57 create_overlay_pathname (const char *name)
58 {
59 static char buf[MAX_BUF];
60
61 /* Why? having extra / doesn't confuse unix anyplace? Dependancies
62 * someplace else in the code? msw 2-17-97
63 */
64 if (*name == '/')
65 sprintf (buf, "%s/%s%s", settings.localdir, settings.mapdir, name);
66 else
67 sprintf (buf, "%s/%s/%s", settings.localdir, settings.mapdir, name);
68 return (buf);
69 }
70
71 /*
72 * same as create_pathname, but for the template maps.
73 */
74 const char *
75 create_template_pathname (const char *name)
76 {
77 static char buf[MAX_BUF];
78
79 /* Why? having extra / doesn't confuse unix anyplace? Dependancies
80 * someplace else in the code? msw 2-17-97
81 */
82 if (*name == '/')
83 sprintf (buf, "%s/%s%s", settings.localdir, settings.templatedir, name);
84 else
85 sprintf (buf, "%s/%s/%s", settings.localdir, settings.templatedir, name);
86 return (buf);
87 }
88
89 /*
90 * This makes absolute path to the itemfile where unique objects
91 * will be saved. Converts '/' to '@'. I think it's essier maintain
92 * files than full directory structure, but if this is problem it can
93 * be changed.
94 */
95 static const char *
96 create_items_path (const char *s)
97 {
98 static char buf[MAX_BUF];
99 char *t;
100
101 if (*s == '/')
102 s++;
103
104 sprintf (buf, "%s/%s/", settings.localdir, settings.uniquedir);
105
106 for (t = buf + strlen (buf); *s; s++, t++)
107 if (*s == '/')
108 *t = '@';
109 else
110 *t = *s;
111 *t = 0;
112 return (buf);
113 }
114
115 /*
116 * This function checks if a file with the given path exists.
117 * -1 is returned if it fails, otherwise the mode of the file
118 * is returned.
119 * It tries out all the compression suffixes listed in the uncomp[] array.
120 *
121 * If prepend_dir is set, then we call create_pathname (which prepends
122 * libdir & mapdir). Otherwise, we assume the name given is fully
123 * complete.
124 * Only the editor actually cares about the writablity of this -
125 * the rest of the code only cares that the file is readable.
126 * when the editor goes away, the call to stat should probably be
127 * replaced by an access instead (similar to the windows one, but
128 * that seems to be missing the prepend_dir processing
129 */
130 int
131 check_path (const char *name, int prepend_dir)
132 {
133 char buf[MAX_BUF];
134
135 char *endbuf;
136 struct stat statbuf;
137 int mode = 0;
138
139 if (prepend_dir)
140 strcpy (buf, create_pathname (name));
141 else
142 strcpy (buf, name);
143
144 /* old method (strchr(buf, '\0')) seemd very odd to me -
145 * this method should be equivalant and is clearer.
146 * Can not use strcat because we need to cycle through
147 * all the names.
148 */
149 endbuf = buf + strlen (buf);
150
151 if (stat (buf, &statbuf))
152 return -1;
153 if (!S_ISREG (statbuf.st_mode))
154 return (-1);
155
156 if (((statbuf.st_mode & S_IRGRP) && getegid () == statbuf.st_gid) ||
157 ((statbuf.st_mode & S_IRUSR) && geteuid () == statbuf.st_uid) || (statbuf.st_mode & S_IROTH))
158 mode |= 4;
159
160 if ((statbuf.st_mode & S_IWGRP && getegid () == statbuf.st_gid) ||
161 (statbuf.st_mode & S_IWUSR && geteuid () == statbuf.st_uid) || (statbuf.st_mode & S_IWOTH))
162 mode |= 2;
163
164 return (mode);
165 }
166
167 /* This rolls up wall, blocks_magic, blocks_view, etc, all into
168 * one function that just returns a P_.. value (see map.h)
169 * it will also do map translation for tiled maps, returning
170 * new values into newmap, nx, and ny. Any and all of those
171 * values can be null, in which case if a new map is needed (returned
172 * by a P_NEW_MAP value, another call to get_map_from_coord
173 * is needed. The case of not passing values is if we're just
174 * checking for the existence of something on those spaces, but
175 * don't expect to insert/remove anything from those spaces.
176 */
177 int
178 get_map_flags (maptile *oldmap, maptile **newmap, sint16 x, sint16 y, sint16 *nx, sint16 *ny)
179 {
180 sint16 newx = x;
181 sint16 newy = y;
182
183 maptile *mp = get_map_from_coord (oldmap, &newx, &newy);
184
185 if (!mp)
186 return P_OUT_OF_MAP;
187
188 if (newmap) *newmap = mp;
189 if (nx) *nx = newx;
190 if (ny) *ny = newy;
191
192 return mp->at (newx, newy).flags () | (mp != oldmap ? P_NEW_MAP : 0);
193 }
194
195 /*
196 * Returns true if the given coordinate is blocked except by the
197 * object passed is not blocking. This is used with
198 * multipart monsters - if we want to see if a 2x2 monster
199 * can move 1 space to the left, we don't want its own area
200 * to block it from moving there.
201 * Returns TRUE if the space is blocked by something other than the
202 * monster.
203 * m, x, y are the target map/coordinates - needed for map tiling.
204 * the coordinates & map passed in should have been updated for tiling
205 * by the caller.
206 */
207 int
208 blocked_link (object *ob, maptile *m, int sx, int sy)
209 {
210 object *tmp;
211 int mflags, blocked;
212
213 /* Make sure the coordinates are valid - they should be, as caller should
214 * have already checked this.
215 */
216 if (OUT_OF_REAL_MAP (m, sx, sy))
217 {
218 LOG (llevError, "blocked_link: Passed map, x, y coordinates outside of map\n");
219 return 1;
220 }
221
222 /* Save some cycles - instead of calling get_map_flags(), just get the value
223 * directly.
224 */
225 mflags = m->at (sx, sy).flags ();
226
227 blocked = GET_MAP_MOVE_BLOCK (m, sx, sy);
228
229 /* If space is currently not blocked by anything, no need to
230 * go further. Not true for players - all sorts of special
231 * things we need to do for players.
232 */
233 if (ob->type != PLAYER && !(mflags & P_IS_ALIVE) && (blocked == 0))
234 return 0;
235
236 /* if there isn't anytyhing alive on this space, and this space isn't
237 * otherwise blocked, we can return now. Only if there is a living
238 * creature do we need to investigate if it is part of this creature
239 * or another. Likewise, only if something is blocking us do we
240 * need to investigate if there is a special circumstance that would
241 * let the player through (inventory checkers for example)
242 */
243 if (!(mflags & P_IS_ALIVE) && !OB_TYPE_MOVE_BLOCK (ob, blocked))
244 return 0;
245
246 if (ob->head != NULL)
247 ob = ob->head;
248
249 /* We basically go through the stack of objects, and if there is
250 * some other object that has NO_PASS or FLAG_ALIVE set, return
251 * true. If we get through the entire stack, that must mean
252 * ob is blocking it, so return 0.
253 */
254 for (tmp = GET_MAP_OB (m, sx, sy); tmp; tmp = tmp->above)
255 {
256
257 /* This must be before the checks below. Code for inventory checkers. */
258 if (tmp->type == CHECK_INV && OB_MOVE_BLOCK (ob, tmp))
259 {
260 /* If last_sp is set, the player/monster needs an object,
261 * so we check for it. If they don't have it, they can't
262 * pass through this space.
263 */
264 if (tmp->last_sp)
265 {
266 if (check_inv_recursive (ob, tmp) == NULL)
267 return 1;
268 else
269 continue;
270 }
271 else
272 {
273 /* In this case, the player must not have the object -
274 * if they do, they can't pass through.
275 */
276 if (check_inv_recursive (ob, tmp) != NULL) /* player has object */
277 return 1;
278 else
279 continue;
280 }
281 } /* if check_inv */
282 else
283 {
284 /* Broke apart a big nasty if into several here to make
285 * this more readable. first check - if the space blocks
286 * movement, can't move here.
287 * second - if a monster, can't move there, unles it is a
288 * hidden dm
289 */
290 if (OB_MOVE_BLOCK (ob, tmp))
291 return 1;
292 if (QUERY_FLAG (tmp, FLAG_ALIVE) && tmp->head != ob && tmp != ob &&
293 tmp->type != DOOR && !(QUERY_FLAG (tmp, FLAG_WIZ) && tmp->contr->hidden))
294 return 1;
295 }
296
297 }
298 return 0;
299 }
300
301
302 /*
303 * Returns true if the given object can't fit in the given spot.
304 * This is meant for multi space objects - for single space objecs,
305 * just calling get_map_blocked and checking that against movement type
306 * of object. This function goes through all the parts of the
307 * multipart object and makes sure they can be inserted.
308 *
309 * While this doesn't call out of map, the get_map_flags does.
310 *
311 * This function has been used to deprecate arch_out_of_map -
312 * this function also does that check, and since in most cases,
313 * a call to one would follow the other, doesn't make a lot of sense to
314 * have two seperate functions for this.
315 *
316 * This returns nonzero if this arch can not go on the space provided,
317 * 0 otherwise. the return value will contain the P_.. value
318 * so the caller can know why this object can't go on the map.
319 * Note that callers should not expect P_NEW_MAP to be set
320 * in return codes - since the object is multispace - if
321 * we did return values, what do you return if half the object
322 * is one map, half on another.
323 *
324 * Note this used to be arch_blocked, but with new movement
325 * code, we need to have actual object to check its move_type
326 * against the move_block values.
327 */
328 int
329 ob_blocked (const object *ob, maptile *m, sint16 x, sint16 y)
330 {
331 archetype *tmp;
332 int flag;
333 maptile *m1;
334 sint16 sx, sy;
335
336 if (!ob)
337 {
338 flag = get_map_flags (m, &m1, x, y, &sx, &sy);
339 if (flag & P_OUT_OF_MAP)
340 return P_OUT_OF_MAP;
341
342 /* don't have object, so don't know what types would block */
343 return m1->at (sx, sy).move_block;
344 }
345
346 for (tmp = ob->arch; tmp; tmp = tmp->more)
347 {
348 flag = get_map_flags (m, &m1, x + tmp->clone.x, y + tmp->clone.y, &sx, &sy);
349
350 if (flag & P_OUT_OF_MAP)
351 return P_OUT_OF_MAP;
352 if (flag & P_IS_ALIVE)
353 return P_IS_ALIVE;
354
355 mapspace &ms = m1->at (sx, sy);
356
357 /* find_first_free_spot() calls this function. However, often
358 * ob doesn't have any move type (when used to place exits)
359 * so the AND operation in OB_TYPE_MOVE_BLOCK doesn't work.
360 */
361
362 if (ob->move_type == 0 && ms.move_block != MOVE_ALL)
363 continue;
364
365 /* Note it is intentional that we check ob - the movement type of the
366 * head of the object should correspond for the entire object.
367 */
368 if (OB_TYPE_MOVE_BLOCK (ob, ms.move_block))
369 return P_NO_PASS;
370 }
371
372 return 0;
373 }
374
375 /* When the map is loaded, load_object does not actually insert objects
376 * into inventory, but just links them. What this does is go through
377 * and insert them properly.
378 * The object 'container' is the object that contains the inventory.
379 * This is needed so that we can update the containers weight.
380 */
381 void
382 fix_container (object *container)
383 {
384 object *tmp = container->inv, *next;
385
386 container->inv = 0;
387 while (tmp)
388 {
389 next = tmp->below;
390 if (tmp->inv)
391 fix_container (tmp);
392
393 insert_ob_in_ob (tmp, container);
394 tmp = next;
395 }
396
397 /* sum_weight will go through and calculate what all the containers are
398 * carrying.
399 */
400 sum_weight (container);
401 }
402
403 /* link_multipart_objects go through all the objects on the map looking
404 * for objects whose arch says they are multipart yet according to the
405 * info we have, they only have the head (as would be expected when
406 * they are saved). We do have to look for the old maps that did save
407 * the more sections and not re-add sections for them.
408 */
409 void
410 maptile::link_multipart_objects ()
411 {
412 if (!spaces)
413 return;
414
415 for (mapspace *ms = spaces + size (); ms-- > spaces; )
416 for (object *tmp = ms->bot; tmp; )
417 {
418 object *above = tmp->above;
419
420 /* already multipart - don't do anything more */
421 if (!tmp->head && !tmp->more)
422 {
423 /* If there is nothing more to this object, this for loop
424 * won't do anything.
425 */
426 archetype *at;
427 object *last, *op;
428 for (at = tmp->arch->more, last = tmp;
429 at;
430 at = at->more, last = op)
431 {
432 op = arch_to_object (at);
433
434 /* update x,y coordinates */
435 op->x += tmp->x;
436 op->y += tmp->y;
437 op->head = tmp;
438 op->map = this;
439 last->more = op;
440 op->name = tmp->name;
441 op->title = tmp->title;
442
443 /* we could link all the parts onto tmp, and then just
444 * call insert_ob_in_map once, but the effect is the same,
445 * as insert_ob_in_map will call itself with each part, and
446 * the coding is simpler to just to it here with each part.
447 */
448 insert_ob_in_map (op, op->map, tmp, INS_NO_MERGE | INS_ABOVE_FLOOR_ONLY | INS_NO_WALK_ON);
449 }
450 }
451
452 tmp = above;
453 }
454 }
455
456 /*
457 * Loads (ands parses) the objects into a given map from the specified
458 * file pointer.
459 * mapflags is the same as we get with load_original_map
460 */
461 bool
462 maptile::load_objects (object_thawer &thawer)
463 {
464 int unique;
465 object *op, *prev = NULL, *last_more = NULL, *otmp;
466
467 op = object::create ();
468 op->map = this; /* To handle buttons correctly */
469
470 while (int i = load_object (thawer, op, 0))
471 {
472 /* if the archetype for the object is null, means that we
473 * got an invalid object. Don't do anything with it - the game
474 * or editor will not be able to do anything with it either.
475 */
476 if (op->arch == NULL)
477 {
478 LOG (llevDebug, "Discarding object without arch: %s\n", op->name ? (const char *) op->name : "(null)");
479 continue;
480 }
481
482 switch (i)
483 {
484 case LL_NORMAL:
485 insert_ob_in_map (op, this, op, INS_NO_MERGE | INS_NO_WALK_ON | INS_ON_TOP | INS_MAP_LOAD);
486
487 if (op->inv)
488 sum_weight (op);
489
490 prev = op, last_more = op;
491 break;
492
493 case LL_MORE:
494 insert_ob_in_map (op, this, op, INS_NO_MERGE | INS_NO_WALK_ON | INS_ABOVE_FLOOR_ONLY);
495 op->head = prev, last_more->more = op, last_more = op;
496 break;
497 }
498
499 op = object::create ();
500 op->map = this;
501 }
502
503 op->destroy ();
504
505 #if 0
506 for (i = 0; i < width; i++)
507 for (j = 0; j < height; j++)
508 {
509 unique = 0;
510 /* check for unique items, or unique squares */
511 for (otmp = GET_MAP_OB (m, i, j); otmp; otmp = otmp->above)
512 {
513 if (QUERY_FLAG (otmp, FLAG_UNIQUE) || QUERY_FLAG (otmp, FLAG_OBJ_SAVE_ON_OVL))
514 unique = 1;
515
516 if (!(mapflags & (MAP_OVERLAY | MAP_PLAYER_UNIQUE) || unique))
517 SET_FLAG (otmp, FLAG_OBJ_ORIGINAL);
518 }
519 }
520 #endif
521
522 return true;
523 }
524
525 void
526 maptile::activate ()
527 {
528 if (!spaces)
529 return;
530
531 for (mapspace *ms = spaces + size (); ms-- > spaces; )
532 for (object *op = ms->bot; op; op = op->above)
533 op->activate_recursive ();
534 }
535
536 void
537 maptile::deactivate ()
538 {
539 if (!spaces)
540 return;
541
542 for (mapspace *ms = spaces + size (); ms-- > spaces; )
543 for (object *op = ms->bot; op; op = op->above)
544 op->deactivate_recursive ();
545 }
546
547 bool
548 maptile::save_objects (object_freezer &freezer, int flags)
549 {
550 if (flags & IO_HEADER)
551 save_header (freezer);
552
553 if (!spaces)
554 return false;
555
556 for (int i = 0; i < size (); ++i)
557 {
558 int unique = 0;
559 for (object *op = spaces [i].bot; op; op = op->above)
560 {
561 if (op->flag [FLAG_UNIQUE] && op->flag [FLAG_IS_FLOOR])
562 unique = 1;
563
564 if (!op->can_map_save ())
565 continue;
566
567 if (unique || op->flag [FLAG_UNIQUE])
568 {
569 if (flags & IO_UNIQUES)
570 save_object (freezer, op, 1);
571 }
572 else if (flags & IO_OBJECTS)
573 save_object (freezer, op, 1);
574 }
575 }
576
577 return true;
578 }
579
580 bool
581 maptile::load_objects (const char *path, bool skip_header)
582 {
583 object_thawer thawer (path);
584
585 if (!thawer)
586 return false;
587
588 if (skip_header)
589 for (;;)
590 {
591 keyword kw = thawer.get_kv ();
592
593 if (kw == KW_end)
594 break;
595
596 thawer.skip_kv (kw);
597 }
598
599 return load_objects (thawer);
600 }
601
602 bool
603 maptile::save_objects (const char *path, int flags)
604 {
605 object_freezer freezer;
606
607 if (!save_objects (freezer, flags))
608 return false;
609
610 return freezer.save (path);
611 }
612
613 maptile::maptile ()
614 {
615 in_memory = MAP_SWAPPED;
616
617 /* The maps used to pick up default x and y values from the
618 * map archetype. Mimic that behaviour.
619 */
620 width = 16;
621 height = 16;
622 reset_timeout = 0;
623 timeout = 300;
624 enter_x = 0;
625 enter_y = 0;
626 }
627
628 maptile::maptile (int w, int h)
629 {
630 in_memory = MAP_SWAPPED;
631
632 width = w;
633 height = h;
634 reset_timeout = 0;
635 timeout = 300;
636 enter_x = 0;
637 enter_y = 0;
638
639 alloc ();
640 }
641
642 /*
643 * Allocates the arrays contained in a maptile.
644 * This basically allocates the dynamic array of spaces for the
645 * map.
646 */
647 void
648 maptile::alloc ()
649 {
650 if (spaces)
651 return;
652
653 spaces = salloc0<mapspace> (size ());
654 }
655
656 /* Takes a string from a map definition and outputs a pointer to the array of shopitems
657 * corresponding to that string. Memory is allocated for this, it must be freed
658 * at a later date.
659 * Called by parse_map_headers below.
660 */
661 static shopitems *
662 parse_shop_string (const char *input_string)
663 {
664 char *shop_string, *p, *q, *next_semicolon, *next_colon;
665 shopitems *items = NULL;
666 int i = 0, number_of_entries = 0;
667 const typedata *current_type;
668
669 shop_string = strdup (input_string);
670 p = shop_string;
671 /* first we'll count the entries, we'll need that for allocating the array shortly */
672 while (p)
673 {
674 p = strchr (p, ';');
675 number_of_entries++;
676 if (p)
677 p++;
678 }
679
680 p = shop_string;
681 strip_endline (p);
682 items = new shopitems[number_of_entries + 1];
683 for (i = 0; i < number_of_entries; i++)
684 {
685 if (!p)
686 {
687 LOG (llevError, "parse_shop_string: I seem to have run out of string, that shouldn't happen.\n");
688 break;
689 }
690
691 next_semicolon = strchr (p, ';');
692 next_colon = strchr (p, ':');
693 /* if there is a stregth specified, figure out what it is, we'll need it soon. */
694 if (next_colon && (!next_semicolon || next_colon < next_semicolon))
695 items[i].strength = atoi (strchr (p, ':') + 1);
696
697 if (isdigit (*p) || *p == '*')
698 {
699 items[i].typenum = atoi (p); /* atoi returns 0 when we have an asterisk */
700 current_type = get_typedata (items[i].typenum);
701 if (current_type)
702 {
703 items[i].name = current_type->name;
704 items[i].name_pl = current_type->name_pl;
705 }
706 }
707 else
708 { /*we have a named type, let's figure out what it is */
709 q = strpbrk (p, ";:");
710 if (q)
711 *q = '\0';
712
713 current_type = get_typedata_by_name (p);
714 if (current_type)
715 {
716 items[i].name = current_type->name;
717 items[i].typenum = current_type->number;
718 items[i].name_pl = current_type->name_pl;
719 }
720 else
721 { /* oh uh, something's wrong, let's free up this one, and try
722 * the next entry while we're at it, better print a warning
723 */
724 LOG (llevError, "invalid type %s defined in shopitems in string %s\n", p, input_string);
725 }
726 }
727
728 items[i].index = number_of_entries;
729 if (next_semicolon)
730 p = ++next_semicolon;
731 else
732 p = NULL;
733 }
734
735 free (shop_string);
736 return items;
737 }
738
739 /* opposite of parse string, this puts the string that was originally fed in to
740 * the map (or something equivilent) into output_string. */
741 static void
742 print_shop_string (maptile *m, char *output_string)
743 {
744 int i;
745 char tmp[MAX_BUF];
746
747 strcpy (output_string, "");
748 for (i = 0; i < m->shopitems[0].index; i++)
749 {
750 if (m->shopitems[i].typenum)
751 {
752 if (m->shopitems[i].strength)
753 sprintf (tmp, "%s:%d;", m->shopitems[i].name, m->shopitems[i].strength);
754 else
755 sprintf (tmp, "%s;", m->shopitems[i].name);
756 }
757 else
758 {
759 if (m->shopitems[i].strength)
760 sprintf (tmp, "*:%d;", m->shopitems[i].strength);
761 else
762 sprintf (tmp, "*");
763 }
764
765 strcat (output_string, tmp);
766 }
767 }
768
769 /* This loads the header information of the map. The header
770 * contains things like difficulty, size, timeout, etc.
771 * this used to be stored in the map object, but with the
772 * addition of tiling, fields beyond that easily named in an
773 * object structure were needed, so it just made sense to
774 * put all the stuff in the map object so that names actually make
775 * sense.
776 * This could be done in lex (like the object loader), but I think
777 * currently, there are few enough fields this is not a big deal.
778 * MSW 2001-07-01
779 */
780 bool
781 maptile::load_header (object_thawer &thawer)
782 {
783 char buf[HUGE_BUF], msgbuf[HUGE_BUF], maplorebuf[HUGE_BUF], *key = NULL, *value, *end;
784 int msgpos = 0;
785 int maplorepos = 0;
786
787 for (;;)
788 {
789 keyword kw = thawer.get_kv ();
790
791 switch (kw)
792 {
793 case KW_EOF:
794 LOG (llevError, "%s: end of file while reading map header, aborting header load.\n", &path);
795 return false;
796
797 case KW_end:
798 return true;
799
800 case KW_msg:
801 thawer.get_ml (KW_endmsg, msg);
802 break;
803
804 case KW_lore: // CF+ extension
805 thawer.get_ml (KW_endlore, maplore);
806 break;
807
808 case KW_maplore:
809 thawer.get_ml (KW_endmaplore, maplore);
810 break;
811
812 case KW_arch:
813 if (strcmp (thawer.get_str (), "map"))
814 LOG (llevError, "%s: loading map and got a non 'arch map' line (arch %s), skipping.\n", &path, thawer.get_str ());
815 break;
816
817 case KW_oid:
818 thawer.get (this, thawer.get_sint32 ());
819 break;
820
821 case KW_file_format_version: break; // nop
822
823 case KW_name: thawer.get (name); break;
824 case KW_attach: thawer.get (attach); break;
825 case KW_reset_time: thawer.get (reset_time); break;
826 case KW_shopgreed: thawer.get (shopgreed); break;
827 case KW_shopmin: thawer.get (shopmin); break;
828 case KW_shopmax: thawer.get (shopmax); break;
829 case KW_shoprace: thawer.get (shoprace); break;
830 case KW_outdoor: thawer.get (outdoor); break;
831 case KW_temp: thawer.get (temp); break;
832 case KW_pressure: thawer.get (pressure); break;
833 case KW_humid: thawer.get (humid); break;
834 case KW_windspeed: thawer.get (windspeed); break;
835 case KW_winddir: thawer.get (winddir); break;
836 case KW_sky: thawer.get (sky); break;
837
838 case KW_per_player: thawer.get (per_player); break;
839 case KW_per_party: thawer.get (per_party); break;
840
841 case KW_region: get_region_by_name (thawer.get_str ()); break;
842 case KW_shopitems: shopitems = parse_shop_string (thawer.get_str ()); break;
843
844 // old names new names
845 case KW_hp: case KW_enter_x: thawer.get (enter_x); break;
846 case KW_sp: case KW_enter_y: thawer.get (enter_y); break;
847 case KW_x: case KW_width: thawer.get (width); break;
848 case KW_y: case KW_height: thawer.get (height); break;
849 case KW_weight: case KW_reset_timeout: thawer.get (reset_timeout); break;
850 case KW_value: case KW_swap_time: thawer.get (timeout); break;
851 case KW_level: case KW_difficulty: thawer.get (difficulty); difficulty = clamp (difficulty, 1, settings.max_level); break;
852 case KW_invisible: case KW_darkness: thawer.get (darkness); break;
853 case KW_stand_still: case KW_fixed_resettime: thawer.get (fixed_resettime); break;
854
855 case KW_tile_path_1: thawer.get (tile_path [0]); break;
856 case KW_tile_path_2: thawer.get (tile_path [1]); break;
857 case KW_tile_path_3: thawer.get (tile_path [2]); break;
858 case KW_tile_path_4: thawer.get (tile_path [3]); break;
859
860 default:
861 LOG (llevError, "%s: skipping unknown key in map header: %s\n", &path, keyword_str [kw]);
862 break;
863 }
864 }
865
866 abort ();
867 }
868
869 bool
870 maptile::load_header (const char *path)
871 {
872 object_thawer thawer (path);
873
874 if (!thawer)
875 return false;
876
877 return load_header (thawer);
878 }
879
880 /******************************************************************************
881 * This is the start of unique map handling code
882 *****************************************************************************/
883
884 /* This goes through the maptile and removed any unique items on the map. */
885 void
886 maptile::clear_unique_items ()
887 {
888 for (int i = 0; i < size (); ++i)
889 {
890 int unique = 0;
891 for (object *op = spaces [i].bot; op; )
892 {
893 object *above = op->above;
894
895 if (QUERY_FLAG (op, FLAG_IS_FLOOR) && QUERY_FLAG (op, FLAG_UNIQUE))
896 unique = 1;
897
898 if (op->head == NULL && (QUERY_FLAG (op, FLAG_UNIQUE) || unique))
899 {
900 op->destroy_inv (false);
901 op->destroy ();
902 }
903
904 op = above;
905 }
906 }
907 }
908
909 bool
910 maptile::save_header (object_freezer &freezer)
911 {
912 #define MAP_OUT(k) freezer.put (KW_ ## k, k)
913 #define MAP_OUT2(k,v) freezer.put (KW_ ## k, v)
914
915 MAP_OUT2 (arch, "map");
916
917 if (name) MAP_OUT (name);
918 MAP_OUT (swap_time);
919 MAP_OUT (reset_time);
920 MAP_OUT (reset_timeout);
921 MAP_OUT (fixed_resettime);
922 MAP_OUT (difficulty);
923
924 if (region) MAP_OUT2 (region, region->name);
925
926 if (shopitems)
927 {
928 char shop[MAX_BUF];
929 print_shop_string (this, shop);
930 MAP_OUT2 (shopitems, shop);
931 }
932
933 MAP_OUT (shopgreed);
934 MAP_OUT (shopmin);
935 MAP_OUT (shopmax);
936 if (shoprace) MAP_OUT (shoprace);
937 MAP_OUT (darkness);
938 MAP_OUT (width);
939 MAP_OUT (height);
940 MAP_OUT (enter_x);
941 MAP_OUT (enter_y);
942
943 if (msg) freezer.put (KW_msg , KW_endmsg , msg);
944 if (maplore) freezer.put (KW_maplore, KW_endmaplore, maplore);
945
946 MAP_OUT (outdoor);
947 MAP_OUT (temp);
948 MAP_OUT (pressure);
949 MAP_OUT (humid);
950 MAP_OUT (windspeed);
951 MAP_OUT (winddir);
952 MAP_OUT (sky);
953
954 MAP_OUT (per_player);
955 MAP_OUT (per_party);
956
957 if (tile_path [0]) MAP_OUT2 (tile_path_1, tile_path [0]);
958 if (tile_path [1]) MAP_OUT2 (tile_path_2, tile_path [1]);
959 if (tile_path [2]) MAP_OUT2 (tile_path_3, tile_path [2]);
960 if (tile_path [3]) MAP_OUT2 (tile_path_4, tile_path [3]);
961
962 freezer.put (KW_end);
963
964 return true;
965 }
966
967 bool
968 maptile::save_header (const char *path)
969 {
970 object_freezer freezer;
971
972 if (!save_header (freezer))
973 return false;
974
975 return freezer.save (path);
976 }
977
978 /*
979 * Remove and free all objects in the given map.
980 */
981 void
982 maptile::clear ()
983 {
984 if (!spaces)
985 return;
986
987 for (mapspace *ms = spaces + size (); ms-- > spaces; )
988 while (object *op = ms->bot)
989 {
990 if (op->head)
991 op = op->head;
992
993 op->destroy_inv (false);
994 op->destroy ();
995 }
996
997 sfree (spaces, size ()), spaces = 0;
998
999 if (buttons)
1000 free_objectlinkpt (buttons), buttons = 0;
1001 }
1002
1003 void
1004 maptile::clear_header ()
1005 {
1006 name = 0;
1007 msg = 0;
1008 maplore = 0;
1009 shoprace = 0;
1010 delete [] shopitems, shopitems = 0;
1011
1012 for (int i = 0; i < 4; i++)
1013 tile_path [i] = 0;
1014 }
1015
1016 maptile::~maptile ()
1017 {
1018 assert (destroyed ());
1019 }
1020
1021 void
1022 maptile::clear_links_to (maptile *m)
1023 {
1024 /* We need to look through all the maps and see if any maps
1025 * are pointing at this one for tiling information. Since
1026 * tiling can be asymetric, we just can not look to see which
1027 * maps this map tiles with and clears those.
1028 */
1029 for (int i = 0; i < 4; i++)
1030 if (tile_map[i] == m)
1031 tile_map[i] = 0;
1032 }
1033
1034 void
1035 maptile::do_destroy ()
1036 {
1037 attachable::do_destroy ();
1038
1039 clear ();
1040 }
1041
1042 /*
1043 * Updates every button on the map (by calling update_button() for them).
1044 */
1045 void
1046 maptile::update_buttons ()
1047 {
1048 for (oblinkpt *obp = buttons; obp; obp = obp->next)
1049 for (objectlink *ol = obp->link; ol; ol = ol->next)
1050 {
1051 if (!ol->ob)
1052 {
1053 LOG (llevError, "Internal error in update_button (%s (%dx%d), connected %ld).\n",
1054 ol->ob ? (const char *) ol->ob->name : "null", ol->ob ? ol->ob->x : -1, ol->ob ? ol->ob->y : -1, obp->value);
1055 continue;
1056 }
1057
1058 if (ol->ob->type == BUTTON || ol->ob->type == PEDESTAL)
1059 {
1060 update_button (ol->ob);
1061 break;
1062 }
1063 }
1064 }
1065
1066 /*
1067 * This routine is supposed to find out the difficulty of the map.
1068 * difficulty does not have a lot to do with character level,
1069 * but does have a lot to do with treasure on the map.
1070 *
1071 * Difficulty can now be set by the map creature. If the value stored
1072 * in the map is zero, then use this routine. Maps should really
1073 * have a difficulty set than using this function - human calculation
1074 * is much better than this functions guesswork.
1075 */
1076 int
1077 maptile::estimate_difficulty () const
1078 {
1079 long monster_cnt = 0;
1080 double avgexp = 0;
1081 sint64 total_exp = 0;
1082
1083 for (mapspace *ms = spaces + size (); ms-- > spaces; )
1084 for (object *op = ms->bot; op; op = op->above)
1085 {
1086 if (QUERY_FLAG (op, FLAG_MONSTER))
1087 {
1088 total_exp += op->stats.exp;
1089 monster_cnt++;
1090 }
1091
1092 if (QUERY_FLAG (op, FLAG_GENERATOR))
1093 {
1094 total_exp += op->stats.exp;
1095
1096 if (archetype *at = type_to_archetype (GENERATE_TYPE (op)))
1097 total_exp += at->clone.stats.exp * 8;
1098
1099 monster_cnt++;
1100 }
1101 }
1102
1103 avgexp = (double) total_exp / monster_cnt;
1104
1105 for (int i = 1; i <= settings.max_level; i++)
1106 if ((level_exp (i, 1) - level_exp (i - 1, 1)) > (100 * avgexp))
1107 return i;
1108
1109 return 1;
1110 }
1111
1112 /* change_map_light() - used to change map light level (darkness)
1113 * up or down. Returns true if successful. It should now be
1114 * possible to change a value by more than 1.
1115 * Move this from los.c to map.c since this is more related
1116 * to maps than los.
1117 * postive values make it darker, negative make it brighter
1118 */
1119 int
1120 maptile::change_map_light (int change)
1121 {
1122 int new_level = darkness + change;
1123
1124 /* Nothing to do */
1125 if (!change || (new_level <= 0 && darkness == 0) || (new_level >= MAX_DARKNESS && darkness >= MAX_DARKNESS))
1126 return 0;
1127
1128 /* inform all players on the map */
1129 if (change > 0)
1130 new_info_map (NDI_BLACK | NDI_UNIQUE, this, "It becomes darker.");
1131 else
1132 new_info_map (NDI_BLACK | NDI_UNIQUE, this, "It becomes brighter.");
1133
1134 /* Do extra checking. since darkness is a unsigned value,
1135 * we need to be extra careful about negative values.
1136 * In general, the checks below are only needed if change
1137 * is not +/-1
1138 */
1139 if (new_level < 0)
1140 darkness = 0;
1141 else if (new_level >= MAX_DARKNESS)
1142 darkness = MAX_DARKNESS;
1143 else
1144 darkness = new_level;
1145
1146 /* All clients need to get re-updated for the change */
1147 update_all_map_los (this);
1148 return 1;
1149 }
1150
1151 /*
1152 * This function updates various attributes about a specific space
1153 * on the map (what it looks like, whether it blocks magic,
1154 * has a living creatures, prevents people from passing
1155 * through, etc)
1156 */
1157 void
1158 mapspace::update_ ()
1159 {
1160 object *tmp, *last = 0;
1161 uint8 flags = 0, light = 0, anywhere = 0;
1162 New_Face *top, *floor, *middle;
1163 object *top_obj, *floor_obj, *middle_obj;
1164 MoveType move_block = 0, move_slow = 0, move_on = 0, move_off = 0, move_allow = 0;
1165
1166 middle = blank_face;
1167 top = blank_face;
1168 floor = blank_face;
1169
1170 middle_obj = 0;
1171 top_obj = 0;
1172 floor_obj = 0;
1173
1174 for (tmp = bot; tmp; last = tmp, tmp = tmp->above)
1175 {
1176 /* This could be made additive I guess (two lights better than
1177 * one). But if so, it shouldn't be a simple additive - 2
1178 * light bulbs do not illuminate twice as far as once since
1179 * it is a dissapation factor that is cubed.
1180 */
1181 if (tmp->glow_radius > light)
1182 light = tmp->glow_radius;
1183
1184 /* This call is needed in order to update objects the player
1185 * is standing in that have animations (ie, grass, fire, etc).
1186 * However, it also causes the look window to be re-drawn
1187 * 3 times each time the player moves, because many of the
1188 * functions the move_player calls eventualy call this.
1189 *
1190 * Always put the player down for drawing.
1191 */
1192 if (!tmp->invisible)
1193 {
1194 if ((tmp->type == PLAYER || QUERY_FLAG (tmp, FLAG_MONSTER)))
1195 {
1196 top = tmp->face;
1197 top_obj = tmp;
1198 }
1199 else if (QUERY_FLAG (tmp, FLAG_IS_FLOOR))
1200 {
1201 /* If we got a floor, that means middle and top were below it,
1202 * so should not be visible, so we clear them.
1203 */
1204 middle = blank_face;
1205 top = blank_face;
1206 floor = tmp->face;
1207 floor_obj = tmp;
1208 }
1209 /* Flag anywhere have high priority */
1210 else if (QUERY_FLAG (tmp, FLAG_SEE_ANYWHERE))
1211 {
1212 middle = tmp->face;
1213
1214 middle_obj = tmp;
1215 anywhere = 1;
1216 }
1217 /* Find the highest visible face around. If equal
1218 * visibilities, we still want the one nearer to the
1219 * top
1220 */
1221 else if (middle == blank_face || (tmp->face->visibility > middle->visibility && !anywhere))
1222 {
1223 middle = tmp->face;
1224 middle_obj = tmp;
1225 }
1226 }
1227
1228 if (tmp == tmp->above)
1229 {
1230 LOG (llevError, "Error in structure of map\n");
1231 exit (-1);
1232 }
1233
1234 move_slow |= tmp->move_slow;
1235 move_block |= tmp->move_block;
1236 move_on |= tmp->move_on;
1237 move_off |= tmp->move_off;
1238 move_allow |= tmp->move_allow;
1239
1240 if (QUERY_FLAG (tmp, FLAG_BLOCKSVIEW)) flags |= P_BLOCKSVIEW;
1241 if (QUERY_FLAG (tmp, FLAG_NO_MAGIC)) flags |= P_NO_MAGIC;
1242 if (tmp->type == PLAYER) flags |= P_PLAYER;
1243 if (tmp->type == SAFE_GROUND) flags |= P_SAFE;
1244 if (QUERY_FLAG (tmp, FLAG_ALIVE)) flags |= P_IS_ALIVE;
1245 if (QUERY_FLAG (tmp, FLAG_DAMNED)) flags |= P_NO_CLERIC;
1246 }
1247
1248 this->light = light;
1249 this->flags_ = flags;
1250 this->move_block = move_block & ~move_allow;
1251 this->move_on = move_on;
1252 this->move_off = move_off;
1253 this->move_slow = move_slow;
1254
1255 /* At this point, we have a floor face (if there is a floor),
1256 * and the floor is set - we are not going to touch it at
1257 * this point.
1258 * middle contains the highest visibility face.
1259 * top contains a player/monster face, if there is one.
1260 *
1261 * We now need to fill in top.face and/or middle.face.
1262 */
1263
1264 /* If the top face also happens to be high visibility, re-do our
1265 * middle face. This should not happen, as we already have the
1266 * else statement above so middle should not get set. OTOH, it
1267 * may be possible for the faces to match but be different objects.
1268 */
1269 if (top == middle)
1270 middle = blank_face;
1271
1272 /* There are three posibilities at this point:
1273 * 1) top face is set, need middle to be set.
1274 * 2) middle is set, need to set top.
1275 * 3) neither middle or top is set - need to set both.
1276 */
1277
1278 for (tmp = last; tmp; tmp = tmp->below)
1279 {
1280 /* Once we get to a floor, stop, since we already have a floor object */
1281 if (QUERY_FLAG (tmp, FLAG_IS_FLOOR))
1282 break;
1283
1284 /* If two top faces are already set, quit processing */
1285 if ((top != blank_face) && (middle != blank_face))
1286 break;
1287
1288 /* Only show visible faces, unless its the editor - show all */
1289 if (!tmp->invisible || editor)
1290 {
1291 /* Fill in top if needed */
1292 if (top == blank_face)
1293 {
1294 top = tmp->face;
1295 top_obj = tmp;
1296 if (top == middle)
1297 middle = blank_face;
1298 }
1299 else
1300 {
1301 /* top is already set - we should only get here if
1302 * middle is not set
1303 *
1304 * Set the middle face and break out, since there is nothing
1305 * more to fill in. We don't check visiblity here, since
1306 *
1307 */
1308 if (tmp->face != top)
1309 {
1310 middle = tmp->face;
1311 middle_obj = tmp;
1312 break;
1313 }
1314 }
1315 }
1316 }
1317
1318 if (middle == floor)
1319 middle = blank_face;
1320
1321 if (top == middle)
1322 middle = blank_face;
1323
1324 faces [0] = top; faces_obj [0] = top != blank_face ? top_obj : 0;
1325 faces [1] = middle; faces_obj [1] = middle != blank_face ? middle_obj : 0;
1326 faces [2] = floor; faces_obj [2] = floor != blank_face ? floor_obj : 0;
1327 }
1328
1329 /* this updates the orig_map->tile_map[tile_num] value after loading
1330 * the map. It also takes care of linking back the freshly loaded
1331 * maps tile_map values if it tiles back to this one. It returns
1332 * the value of orig_map->tile_map[tile_num]. It really only does this
1333 * so that it is easier for calling functions to verify success.
1334 */
1335 static maptile *
1336 load_and_link_tiled_map (maptile *orig_map, int tile_num)
1337 {
1338 maptile *mp = orig_map->load_map_sync (orig_map->tile_path [tile_num], orig_map);
1339
1340 if (!mp || mp->in_memory != MAP_IN_MEMORY)
1341 {
1342 // emergency mode, manufacture a dummy map, this creates a memleak, but thats fine
1343 LOG (llevError, "FATAL: cannot load tiled map %s from %s, leaking memory and worse!\n",
1344 &orig_map->tile_path[tile_num], &orig_map->path);
1345 mp = new maptile (1, 1);
1346 mp->alloc ();
1347 mp->in_memory = MAP_IN_MEMORY;
1348 }
1349
1350 int dest_tile = (tile_num + 2) % 4;
1351
1352 orig_map->tile_map[tile_num] = mp;
1353
1354 // optimisation: back-link map to origin map if euclidean
1355 //TODO: non-euclidean maps MUST GO
1356 if (orig_map->tile_map[tile_num]->tile_path[dest_tile] == orig_map->path)
1357 orig_map->tile_map[tile_num]->tile_map[dest_tile] = orig_map;
1358
1359 return mp;
1360 }
1361
1362 /* this returns TRUE if the coordinates (x,y) are out of
1363 * map m. This function also takes into account any
1364 * tiling considerations, loading adjacant maps as needed.
1365 * This is the function should always be used when it
1366 * necessary to check for valid coordinates.
1367 * This function will recursively call itself for the
1368 * tiled maps.
1369 */
1370 int
1371 out_of_map (maptile *m, int x, int y)
1372 {
1373 /* If we get passed a null map, this is obviously the
1374 * case. This generally shouldn't happen, but if the
1375 * map loads fail below, it could happen.
1376 */
1377 if (!m)
1378 return 0;
1379
1380 if (x < 0)
1381 {
1382 if (!m->tile_path[3])
1383 return 1;
1384
1385 if (!m->tile_map[3] || m->tile_map[3]->in_memory != MAP_IN_MEMORY)
1386 load_and_link_tiled_map (m, 3);
1387
1388 return out_of_map (m->tile_map[3], x + m->tile_map[3]->width, y);
1389 }
1390
1391 if (x >= m->width)
1392 {
1393 if (!m->tile_path[1])
1394 return 1;
1395
1396 if (!m->tile_map[1] || m->tile_map[1]->in_memory != MAP_IN_MEMORY)
1397 load_and_link_tiled_map (m, 1);
1398
1399 return out_of_map (m->tile_map[1], x - m->width, y);
1400 }
1401
1402 if (y < 0)
1403 {
1404 if (!m->tile_path[0])
1405 return 1;
1406
1407 if (!m->tile_map[0] || m->tile_map[0]->in_memory != MAP_IN_MEMORY)
1408 load_and_link_tiled_map (m, 0);
1409
1410 return out_of_map (m->tile_map[0], x, y + m->tile_map[0]->height);
1411 }
1412
1413 if (y >= m->height)
1414 {
1415 if (!m->tile_path[2])
1416 return 1;
1417
1418 if (!m->tile_map[2] || m->tile_map[2]->in_memory != MAP_IN_MEMORY)
1419 load_and_link_tiled_map (m, 2);
1420
1421 return out_of_map (m->tile_map[2], x, y - m->height);
1422 }
1423
1424 /* Simple case - coordinates are within this local
1425 * map.
1426 */
1427 return 0;
1428 }
1429
1430 /* This is basically the same as out_of_map above, but
1431 * instead we return NULL if no map is valid (coordinates
1432 * out of bounds and no tiled map), otherwise it returns
1433 * the map as that the coordinates are really on, and
1434 * updates x and y to be the localized coordinates.
1435 * Using this is more efficient of calling out_of_map
1436 * and then figuring out what the real map is
1437 */
1438 maptile *
1439 get_map_from_coord (maptile *m, sint16 *x, sint16 *y)
1440 {
1441 if (*x < 0)
1442 {
1443 if (!m->tile_path[3])
1444 return 0;
1445
1446 if (!m->tile_map[3] || m->tile_map[3]->in_memory != MAP_IN_MEMORY)
1447 load_and_link_tiled_map (m, 3);
1448
1449 *x += m->tile_map[3]->width;
1450 return (get_map_from_coord (m->tile_map[3], x, y));
1451 }
1452
1453 if (*x >= m->width)
1454 {
1455 if (!m->tile_path[1])
1456 return 0;
1457
1458 if (!m->tile_map[1] || m->tile_map[1]->in_memory != MAP_IN_MEMORY)
1459 load_and_link_tiled_map (m, 1);
1460
1461 *x -= m->width;
1462 return (get_map_from_coord (m->tile_map[1], x, y));
1463 }
1464
1465 if (*y < 0)
1466 {
1467 if (!m->tile_path[0])
1468 return 0;
1469
1470 if (!m->tile_map[0] || m->tile_map[0]->in_memory != MAP_IN_MEMORY)
1471 load_and_link_tiled_map (m, 0);
1472
1473 *y += m->tile_map[0]->height;
1474 return (get_map_from_coord (m->tile_map[0], x, y));
1475 }
1476
1477 if (*y >= m->height)
1478 {
1479 if (!m->tile_path[2])
1480 return 0;
1481
1482 if (!m->tile_map[2] || m->tile_map[2]->in_memory != MAP_IN_MEMORY)
1483 load_and_link_tiled_map (m, 2);
1484
1485 *y -= m->height;
1486 return (get_map_from_coord (m->tile_map[2], x, y));
1487 }
1488
1489 /* Simple case - coordinates are within this local
1490 * map.
1491 */
1492 return m;
1493 }
1494
1495 /**
1496 * Return whether map2 is adjacent to map1. If so, store the distance from
1497 * map1 to map2 in dx/dy.
1498 */
1499 static int
1500 adjacent_map (const maptile *map1, const maptile *map2, int *dx, int *dy)
1501 {
1502 if (!map1 || !map2)
1503 return 0;
1504
1505 if (map1 == map2)
1506 {
1507 *dx = 0;
1508 *dy = 0;
1509 }
1510 else if (map1->tile_map[0] == map2)
1511 { /* up */
1512 *dx = 0;
1513 *dy = -map2->height;
1514 }
1515 else if (map1->tile_map[1] == map2)
1516 { /* right */
1517 *dx = map1->width;
1518 *dy = 0;
1519 }
1520 else if (map1->tile_map[2] == map2)
1521 { /* down */
1522 *dx = 0;
1523 *dy = map1->height;
1524 }
1525 else if (map1->tile_map[3] == map2)
1526 { /* left */
1527 *dx = -map2->width;
1528 *dy = 0;
1529 }
1530 else if (map1->tile_map[0] && map1->tile_map[0]->tile_map[1] == map2)
1531 { /* up right */
1532 *dx = map1->tile_map[0]->width;
1533 *dy = -map1->tile_map[0]->height;
1534 }
1535 else if (map1->tile_map[0] && map1->tile_map[0]->tile_map[3] == map2)
1536 { /* up left */
1537 *dx = -map2->width;
1538 *dy = -map1->tile_map[0]->height;
1539 }
1540 else if (map1->tile_map[1] && map1->tile_map[1]->tile_map[0] == map2)
1541 { /* right up */
1542 *dx = map1->width;
1543 *dy = -map2->height;
1544 }
1545 else if (map1->tile_map[1] && map1->tile_map[1]->tile_map[2] == map2)
1546 { /* right down */
1547 *dx = map1->width;
1548 *dy = map1->tile_map[1]->height;
1549 }
1550 else if (map1->tile_map[2] && map1->tile_map[2]->tile_map[1] == map2)
1551 { /* down right */
1552 *dx = map1->tile_map[2]->width;
1553 *dy = map1->height;
1554 }
1555 else if (map1->tile_map[2] && map1->tile_map[2]->tile_map[3] == map2)
1556 { /* down left */
1557 *dx = -map2->width;
1558 *dy = map1->height;
1559 }
1560 else if (map1->tile_map[3] && map1->tile_map[3]->tile_map[0] == map2)
1561 { /* left up */
1562 *dx = -map1->tile_map[3]->width;
1563 *dy = -map2->height;
1564 }
1565 else if (map1->tile_map[3] && map1->tile_map[3]->tile_map[2] == map2)
1566 { /* left down */
1567 *dx = -map1->tile_map[3]->width;
1568 *dy = map1->tile_map[3]->height;
1569 }
1570 else
1571 return 0;
1572
1573 return 1;
1574 }
1575
1576 /* From map.c
1577 * This is used by get_player to determine where the other
1578 * creature is. get_rangevector takes into account map tiling,
1579 * so you just can not look the the map coordinates and get the
1580 * righte value. distance_x/y are distance away, which
1581 * can be negativbe. direction is the crossfire direction scheme
1582 * that the creature should head. part is the part of the
1583 * monster that is closest.
1584 *
1585 * get_rangevector looks at op1 and op2, and fills in the
1586 * structure for op1 to get to op2.
1587 * We already trust that the caller has verified that the
1588 * two objects are at least on adjacent maps. If not,
1589 * results are not likely to be what is desired.
1590 * if the objects are not on maps, results are also likely to
1591 * be unexpected
1592 *
1593 * currently, the only flag supported (0x1) is don't translate for
1594 * closest body part of 'op1'
1595 */
1596 void
1597 get_rangevector (object *op1, object *op2, rv_vector * retval, int flags)
1598 {
1599 if (!adjacent_map (op1->map, op2->map, &retval->distance_x, &retval->distance_y))
1600 {
1601 /* be conservative and fill in _some_ data */
1602 retval->distance = 100000;
1603 retval->distance_x = 32767;
1604 retval->distance_y = 32767;
1605 retval->direction = 0;
1606 retval->part = 0;
1607 }
1608 else
1609 {
1610 object *best;
1611
1612 retval->distance_x += op2->x - op1->x;
1613 retval->distance_y += op2->y - op1->y;
1614
1615 best = op1;
1616 /* If this is multipart, find the closest part now */
1617 if (!(flags & 0x1) && op1->more)
1618 {
1619 object *tmp;
1620 int best_distance = retval->distance_x * retval->distance_x + retval->distance_y * retval->distance_y, tmpi;
1621
1622 /* we just take the offset of the piece to head to figure
1623 * distance instead of doing all that work above again
1624 * since the distance fields we set above are positive in the
1625 * same axis as is used for multipart objects, the simply arithmetic
1626 * below works.
1627 */
1628 for (tmp = op1->more; tmp != NULL; tmp = tmp->more)
1629 {
1630 tmpi = (op1->x - tmp->x + retval->distance_x) * (op1->x - tmp->x + retval->distance_x) +
1631 (op1->y - tmp->y + retval->distance_y) * (op1->y - tmp->y + retval->distance_y);
1632 if (tmpi < best_distance)
1633 {
1634 best_distance = tmpi;
1635 best = tmp;
1636 }
1637 }
1638 if (best != op1)
1639 {
1640 retval->distance_x += op1->x - best->x;
1641 retval->distance_y += op1->y - best->y;
1642 }
1643 }
1644 retval->part = best;
1645 retval->distance = isqrt (retval->distance_x * retval->distance_x + retval->distance_y * retval->distance_y);
1646 retval->direction = find_dir_2 (-retval->distance_x, -retval->distance_y);
1647 }
1648 }
1649
1650 /* this is basically the same as get_rangevector above, but instead of
1651 * the first parameter being an object, it instead is the map
1652 * and x,y coordinates - this is used for path to player -
1653 * since the object is not infact moving but we are trying to traverse
1654 * the path, we need this.
1655 * flags has no meaning for this function at this time - I kept it in to
1656 * be more consistant with the above function and also in case they are needed
1657 * for something in the future. Also, since no object is pasted, the best
1658 * field of the rv_vector is set to NULL.
1659 */
1660
1661 void
1662 get_rangevector_from_mapcoord (const maptile *m, int x, int y, const object *op2, rv_vector * retval, int flags)
1663 {
1664 if (!adjacent_map (m, op2->map, &retval->distance_x, &retval->distance_y))
1665 {
1666 /* be conservative and fill in _some_ data */
1667 retval->distance = 100000;
1668 retval->distance_x = 32767;
1669 retval->distance_y = 32767;
1670 retval->direction = 0;
1671 retval->part = 0;
1672 }
1673 else
1674 {
1675 retval->distance_x += op2->x - x;
1676 retval->distance_y += op2->y - y;
1677
1678 retval->part = NULL;
1679 retval->distance = isqrt (retval->distance_x * retval->distance_x + retval->distance_y * retval->distance_y);
1680 retval->direction = find_dir_2 (-retval->distance_x, -retval->distance_y);
1681 }
1682 }
1683
1684 /* Returns true of op1 and op2 are effectively on the same map
1685 * (as related to map tiling). Note that this looks for a path from
1686 * op1 to op2, so if the tiled maps are asymetric and op2 has a path
1687 * to op1, this will still return false.
1688 * Note we only look one map out to keep the processing simple
1689 * and efficient. This could probably be a macro.
1690 * MSW 2001-08-05
1691 */
1692 int
1693 on_same_map (const object *op1, const object *op2)
1694 {
1695 int dx, dy;
1696
1697 return adjacent_map (op1->map, op2->map, &dx, &dy);
1698 }
1699
1700 object *
1701 maptile::insert (object *op, int x, int y, object *originator, int flags)
1702 {
1703 if (!op->flag [FLAG_REMOVED])
1704 op->remove ();
1705
1706 return insert_ob_in_map_at (op, this, originator, flags, x, y);
1707 }
1708