ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/common/map.C
Revision: 1.56
Committed: Sat Dec 30 10:16:10 2006 UTC (17 years, 4 months ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.55: +393 -1035 lines
Log Message:
preliminary snapshot check-in, DO NOT USE IN PRODUCTION SYSTEMS
See the Changes file for details

File Contents

# User Rev Content
1 elmex 1.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 root 1.30 The authors can be reached via e-mail at <crossfire@schmorp.de>
22 elmex 1.1 */
23    
24     #include <global.h>
25     #include <funcpoint.h>
26    
27     #include <loader.h>
28 pippijn 1.38 #include <unistd.h>
29 elmex 1.1
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 root 1.29 const char *
39     create_pathname (const char *name)
40     {
41     static char buf[MAX_BUF];
42 elmex 1.1
43 root 1.29 /* 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 elmex 1.1 }
52    
53     /*
54     * same as create_pathname, but for the overlay maps.
55     */
56 root 1.29 const char *
57     create_overlay_pathname (const char *name)
58     {
59     static char buf[MAX_BUF];
60 elmex 1.1
61 root 1.29 /* 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 elmex 1.1 }
70    
71     /*
72     * same as create_pathname, but for the template maps.
73     */
74 root 1.29 const char *
75     create_template_pathname (const char *name)
76     {
77     static char buf[MAX_BUF];
78 elmex 1.1
79 root 1.29 /* 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 elmex 1.1 }
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 root 1.29 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 elmex 1.1
104 root 1.29 sprintf (buf, "%s/%s/", settings.localdir, settings.uniquedir);
105 elmex 1.1
106 root 1.29 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 elmex 1.1 }
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 root 1.29 int
131     check_path (const char *name, int prepend_dir)
132 elmex 1.1 {
133 root 1.29 char buf[MAX_BUF];
134    
135     char *endbuf;
136     struct stat statbuf;
137     int mode = 0;
138 elmex 1.1
139 root 1.29 if (prepend_dir)
140     strcpy (buf, create_pathname (name));
141     else
142     strcpy (buf, name);
143 elmex 1.1
144 root 1.29 /* 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 elmex 1.1
164 root 1.29 return (mode);
165 elmex 1.1 }
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 root 1.29 int
178 root 1.46 get_map_flags (maptile *oldmap, maptile **newmap, sint16 x, sint16 y, sint16 *nx, sint16 *ny)
179 elmex 1.1 {
180 root 1.56 sint16 newx = x;
181     sint16 newy = y;
182 root 1.46
183 root 1.56 maptile *mp = get_map_from_coord (oldmap, &newx, &newy);
184 root 1.46
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 elmex 1.3
192 root 1.56 return mp->at (newx, newy).flags () | (mp != oldmap ? P_NEW_MAP : 0);
193 elmex 1.1 }
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 root 1.29 int
208 root 1.31 blocked_link (object *ob, maptile *m, int sx, int sy)
209 root 1.29 {
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 elmex 1.1 }
221    
222 root 1.29 /* Save some cycles - instead of calling get_map_flags(), just get the value
223     * directly.
224     */
225 root 1.46 mflags = m->at (sx, sy).flags ();
226 root 1.29
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 root 1.45 for (tmp = GET_MAP_OB (m, sx, sy); tmp; tmp = tmp->above)
255 root 1.29 {
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 root 1.10 }
296 elmex 1.1
297     }
298 root 1.29 return 0;
299 elmex 1.1 }
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 root 1.29 int
329 root 1.31 ob_blocked (const object *ob, maptile *m, sint16 x, sint16 y)
330 root 1.29 {
331     archetype *tmp;
332     int flag;
333 root 1.31 maptile *m1;
334 root 1.29 sint16 sx, sy;
335 elmex 1.1
336 root 1.29 if (ob == NULL)
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 elmex 1.1
342 root 1.29 /* don't have object, so don't know what types would block */
343 root 1.47 return m1->at (sx, sy).move_block;
344 elmex 1.1 }
345    
346 root 1.47 for (tmp = ob->arch; tmp; tmp = tmp->more)
347 root 1.29 {
348     flag = get_map_flags (m, &m1, x + tmp->clone.x, y + tmp->clone.y, &sx, &sy);
349 elmex 1.1
350 root 1.29 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 root 1.47 mapspace &ms = m1->at (sx, sy);
356    
357 root 1.29 /* 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 elmex 1.1
362 root 1.47 if (ob->move_type == 0 && ms.move_block != MOVE_ALL)
363 root 1.29 continue;
364 elmex 1.1
365 root 1.29 /* 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 root 1.47 if (OB_TYPE_MOVE_BLOCK (ob, ms.move_block))
369 root 1.45 return P_NO_PASS;
370     }
371 elmex 1.1
372 root 1.29 return 0;
373 elmex 1.1 }
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 root 1.29 void
382     fix_container (object *container)
383 elmex 1.1 {
384 root 1.29 object *tmp = container->inv, *next;
385 elmex 1.1
386 root 1.56 container->inv = 0;
387     while (tmp)
388 root 1.29 {
389     next = tmp->below;
390     if (tmp->inv)
391     fix_container (tmp);
392 root 1.56
393     insert_ob_in_ob (tmp, container);
394 root 1.29 tmp = next;
395     }
396 root 1.56
397 root 1.29 /* sum_weight will go through and calculate what all the containers are
398     * carrying.
399     */
400     sum_weight (container);
401 elmex 1.1 }
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 root 1.56 void
410     maptile::link_multipart_objects ()
411 elmex 1.1 {
412 root 1.56 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 root 1.29
420 root 1.56 /* 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 root 1.29
434 root 1.56 /* 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 root 1.29
452 root 1.56 tmp = above;
453     }
454 elmex 1.1 }
455 root 1.29
456 elmex 1.1 /*
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 root 1.56 bool
462     maptile::load_objects (object_thawer &thawer)
463 root 1.24 {
464     int unique;
465     object *op, *prev = NULL, *last_more = NULL, *otmp;
466 elmex 1.1
467 root 1.40 op = object::create ();
468 root 1.56 op->map = this; /* To handle buttons correctly */
469 elmex 1.1
470 root 1.56 while (int i = load_object (thawer, op, 0))
471 root 1.24 {
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 root 1.29 LOG (llevDebug, "Discarding object without arch: %s\n", op->name ? (const char *) op->name : "(null)");
479 root 1.24 continue;
480 root 1.10 }
481    
482 root 1.24 switch (i)
483     {
484 root 1.41 case LL_NORMAL:
485 root 1.56 insert_ob_in_map (op, this, op, INS_NO_MERGE | INS_NO_WALK_ON | INS_ON_TOP | INS_MAP_LOAD);
486 root 1.41
487     if (op->inv)
488     sum_weight (op);
489    
490     prev = op, last_more = op;
491     break;
492    
493     case LL_MORE:
494 root 1.56 insert_ob_in_map (op, this, op, INS_NO_MERGE | INS_NO_WALK_ON | INS_ABOVE_FLOOR_ONLY);
495 root 1.41 op->head = prev, last_more->more = op, last_more = op;
496     break;
497 root 1.10 }
498 root 1.24
499 root 1.40 op = object::create ();
500 root 1.56 op->map = this;
501 elmex 1.1 }
502 root 1.24
503 root 1.56 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 (1);
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 (1);
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 root 1.24 {
558 root 1.56 int unique = 0;
559     for (object *op = spaces [i].bot; op; op = op->above)
560 root 1.24 {
561 root 1.56 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 root 1.24 {
569 root 1.56 if (flags & IO_UNIQUES)
570     save_object (freezer, op, 1);
571 root 1.10 }
572 root 1.56 else if (flags & IO_OBJECTS)
573     save_object (freezer, op, 1);
574 root 1.10 }
575 elmex 1.1 }
576 root 1.24
577 root 1.56 return true;
578 elmex 1.1 }
579    
580 root 1.56 bool
581     maptile::load_objects (const char *path, bool skip_header)
582 root 1.29 {
583 root 1.56 object_thawer thawer (path);
584 elmex 1.1
585 root 1.56 if (!thawer)
586     return false;
587    
588     if (skip_header)
589     for (;;)
590 root 1.29 {
591 root 1.56 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 root 1.51
602 root 1.56 bool
603     maptile::save_objects (const char *path, int flags)
604     {
605     object_freezer freezer;
606 root 1.29
607 root 1.56 if (!save_objects (freezer, flags))
608     return false;
609 root 1.29
610 root 1.56 return freezer.save (path);
611 elmex 1.1 }
612    
613 root 1.34 maptile::maptile ()
614     {
615     in_memory = MAP_SWAPPED;
616 root 1.54
617 root 1.34 /* The maps used to pick up default x and y values from the
618     * map archetype. Mimic that behaviour.
619     */
620 root 1.53 width = 16;
621     height = 16;
622     reset_timeout = 0;
623     timeout = 300;
624     enter_x = 0;
625     enter_y = 0;
626 root 1.34 }
627    
628 root 1.56 maptile::maptile (int w, int h)
629 root 1.54 {
630 root 1.56 in_memory = MAP_SWAPPED;
631 root 1.54
632 root 1.56 width = w;
633     height = h;
634     reset_timeout = 0;
635     timeout = 300;
636     enter_x = 0;
637     enter_y = 0;
638 root 1.54
639 root 1.56 alloc ();
640 elmex 1.1 }
641    
642     /*
643 root 1.31 * Allocates the arrays contained in a maptile.
644 elmex 1.1 * This basically allocates the dynamic array of spaces for the
645     * map.
646     */
647 root 1.29 void
648 root 1.56 maptile::alloc ()
649 root 1.29 {
650 root 1.34 if (spaces)
651 root 1.56 return;
652 elmex 1.1
653 root 1.53 spaces = salloc0<mapspace> (size ());
654 elmex 1.1 }
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 root 1.29 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 root 1.43 shop_string = strdup (input_string);
670 root 1.29 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 root 1.54
680 root 1.29 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 root 1.54
691 root 1.29 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 root 1.54
728 root 1.29 items[i].index = number_of_entries;
729     if (next_semicolon)
730     p = ++next_semicolon;
731     else
732     p = NULL;
733 elmex 1.1 }
734 root 1.54
735 root 1.29 free (shop_string);
736     return items;
737 elmex 1.1 }
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 root 1.29 static void
742 root 1.31 print_shop_string (maptile *m, char *output_string)
743 root 1.29 {
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 root 1.54 sprintf (tmp, "%s:%d;", m->shopitems[i].name, m->shopitems[i].strength);
754 root 1.29 else
755     sprintf (tmp, "%s;", m->shopitems[i].name);
756 root 1.10 }
757 root 1.29 else
758     {
759     if (m->shopitems[i].strength)
760 root 1.54 sprintf (tmp, "*:%d;", m->shopitems[i].strength);
761 root 1.29 else
762     sprintf (tmp, "*");
763     }
764 root 1.54
765 root 1.29 strcat (output_string, tmp);
766 elmex 1.1 }
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 root 1.56 bool
781     maptile::load_header (object_thawer &thawer)
782 elmex 1.1 {
783 root 1.29 char buf[HUGE_BUF], msgbuf[HUGE_BUF], maplorebuf[HUGE_BUF], *key = NULL, *value, *end;
784     int msgpos = 0;
785     int maplorepos = 0;
786    
787 root 1.56 for (;;)
788 root 1.29 {
789 root 1.56 keyword kw = thawer.get_kv ();
790 root 1.36
791 root 1.56 switch (kw)
792 root 1.29 {
793 root 1.56 case KW_EOF:
794     LOG (llevError, "%s: end of file while reading map header, aborting header load.", &path);
795     return false;
796 root 1.36
797 root 1.56 case KW_end:
798     return true;
799 root 1.36
800 root 1.56 case KW_msg:
801     thawer.get_ml (KW_endmsg, msg);
802     break;
803 root 1.22
804 root 1.56 case KW_lore: // CF+ extension
805     thawer.get_ml (KW_endlore, maplore);
806     break;
807 root 1.10
808 root 1.56 case KW_maplore:
809     thawer.get_ml (KW_endmaplore, maplore);
810     break;
811 root 1.10
812 root 1.56 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 root 1.29
817 root 1.56 case KW_oid:
818     thawer.get (this, thawer.get_sint32 ());
819     break;
820 root 1.29
821 root 1.56 case KW_file_format_version: break; // nop
822 root 1.29
823 root 1.56 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 root 1.10
860 root 1.56 default:
861     LOG (llevError, "%s: skipping unknown key in map header: %s\n", &path, keyword_str [kw]);
862     break;
863 root 1.10 }
864 elmex 1.1 }
865 root 1.41
866 root 1.56 abort ();
867 elmex 1.1 }
868    
869 root 1.56 bool
870     maptile::load_header (const char *path)
871 root 1.29 {
872 root 1.56 object_thawer thawer (path);
873 root 1.9
874 root 1.29 if (!thawer)
875 root 1.56 return false;
876 elmex 1.1
877 root 1.56 return load_header (thawer);
878     }
879 elmex 1.1
880 root 1.56 /******************************************************************************
881     * This is the start of unique map handling code
882     *****************************************************************************/
883 root 1.29
884 root 1.56 /* This goes through the maptile and removed any unique items on the map. */
885     void
886     maptile::clear_unique_items ()
887 root 1.29 {
888 root 1.56 for (int i = 0; i < size (); ++i)
889 root 1.29 {
890 root 1.56 int unique = 0;
891     for (object *op = spaces [i].bot; op; )
892     {
893     object *above = op->above;
894 elmex 1.1
895 root 1.56 if (QUERY_FLAG (op, FLAG_IS_FLOOR) && QUERY_FLAG (op, FLAG_UNIQUE))
896     unique = 1;
897 root 1.14
898 root 1.56 if (op->head == NULL && (QUERY_FLAG (op, FLAG_UNIQUE) || unique))
899     {
900     op->destroy_inv (false);
901     op->destroy ();
902     }
903 elmex 1.1
904 root 1.56 op = above;
905     }
906 root 1.29 }
907 elmex 1.1 }
908    
909 root 1.56 bool
910     maptile::save_header (object_freezer &freezer)
911 root 1.29 {
912 root 1.56 #define MAP_OUT(k) freezer.put (KW_ ## k, k)
913     #define MAP_OUT2(k,v) freezer.put (KW_ ## k, v)
914 elmex 1.1
915 root 1.56 MAP_OUT2 (arch, "map");
916 elmex 1.1
917 root 1.56 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 root 1.9
924 root 1.56 if (region) MAP_OUT2 (region, region->name);
925 root 1.29
926 root 1.56 if (shopitems)
927 root 1.29 {
928 root 1.56 char shop[MAX_BUF];
929     print_shop_string (this, shop);
930     MAP_OUT2 (shopitems, shop);
931 elmex 1.1 }
932    
933 root 1.56 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 root 1.14
943 root 1.56 if (msg) freezer.put (KW_msg , KW_endmsg , msg);
944     if (maplore) freezer.put (KW_maplore, KW_endmaplore, maplore);
945 elmex 1.1
946 root 1.56 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 elmex 1.1
954 root 1.56 MAP_OUT (per_player);
955     MAP_OUT (per_party);
956 elmex 1.1
957 root 1.56 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 root 1.40
962 root 1.56 MAP_OUT2 (end, 0);
963 root 1.40
964 root 1.56 return true;
965 elmex 1.1 }
966    
967 root 1.56 bool
968     maptile::save_header (const char *path)
969 root 1.29 {
970 root 1.17 object_freezer freezer;
971 root 1.10
972 root 1.56 if (!save_header (freezer))
973     return false;
974 elmex 1.1
975 root 1.56 return freezer.save (path);
976 elmex 1.1 }
977    
978     /*
979     * Remove and free all objects in the given map.
980     */
981 root 1.29 void
982 root 1.56 maptile::clear ()
983 root 1.29 {
984 root 1.56 if (!spaces)
985 root 1.47 return;
986 root 1.29
987 root 1.56 for (mapspace *ms = spaces + size (); ms-- > spaces; )
988     while (object *op = ms->bot)
989 root 1.29 {
990 root 1.56 if (op->head)
991     op = op->head;
992    
993     op->destroy_inv (false);
994     op->destroy ();
995     }
996 root 1.29
997 root 1.56 sfree (spaces, size ()), spaces = 0;
998 root 1.29
999 root 1.56 if (buttons)
1000     free_objectlinkpt (buttons), buttons = 0;
1001 elmex 1.1 }
1002    
1003 root 1.29 void
1004 root 1.56 maptile::clear_header ()
1005 root 1.29 {
1006 root 1.56 name = 0;
1007     msg = 0;
1008     maplore = 0;
1009     shoprace = 0;
1010     delete [] shopitems, shopitems = 0;
1011 root 1.42
1012 root 1.47 for (int i = 0; i < 4; i++)
1013 root 1.56 tile_path [i] = 0;
1014 root 1.47 }
1015 root 1.42
1016 root 1.47 maptile::~maptile ()
1017     {
1018 root 1.53 assert (destroyed ());
1019 elmex 1.1 }
1020    
1021 root 1.29 void
1022 root 1.56 maptile::clear_links_to (maptile *m)
1023 root 1.29 {
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 root 1.47 * tiling can be asymetric, we just can not look to see which
1027 root 1.29 * maps this map tiles with and clears those.
1028     */
1029 root 1.56 for (int i = 0; i < 4; i++)
1030     if (tile_map[i] == m)
1031     tile_map[i] = 0;
1032 root 1.47 }
1033 elmex 1.1
1034 root 1.47 void
1035 root 1.56 maptile::do_destroy ()
1036 root 1.47 {
1037 root 1.56 attachable::do_destroy ();
1038    
1039     clear ();
1040 elmex 1.1 }
1041    
1042     /*
1043 root 1.56 * Updates every button on the map (by calling update_button() for them).
1044 elmex 1.1 */
1045 root 1.56 void
1046     maptile::update_buttons ()
1047 root 1.29 {
1048 root 1.56 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 elmex 1.1
1058 root 1.56 if (ol->ob->type == BUTTON || ol->ob->type == PEDESTAL)
1059     {
1060     update_button (ol->ob);
1061     break;
1062     }
1063     }
1064 elmex 1.1 }
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 root 1.29 int
1077 root 1.56 maptile::estimate_difficulty () const
1078 root 1.29 {
1079 elmex 1.1 long monster_cnt = 0;
1080     double avgexp = 0;
1081     sint64 total_exp = 0;
1082    
1083 root 1.56 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 elmex 1.1
1092 root 1.56 if (QUERY_FLAG (op, FLAG_GENERATOR))
1093     {
1094     total_exp += op->stats.exp;
1095 elmex 1.1
1096 root 1.56 if (archetype *at = type_to_archetype (GENERATE_TYPE (op)))
1097     total_exp += at->clone.stats.exp * 8;
1098 elmex 1.1
1099 root 1.56 monster_cnt++;
1100     }
1101     }
1102 elmex 1.1
1103     avgexp = (double) total_exp / monster_cnt;
1104    
1105 root 1.56 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 elmex 1.1
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 root 1.29 int
1120 root 1.56 maptile::change_map_light (int change)
1121 root 1.29 {
1122 root 1.56 int new_level = darkness + change;
1123 root 1.29
1124     /* Nothing to do */
1125 root 1.56 if (!change || (new_level <= 0 && darkness == 0) || (new_level >= MAX_DARKNESS && darkness >= MAX_DARKNESS))
1126     return 0;
1127 elmex 1.1
1128 root 1.29 /* inform all players on the map */
1129     if (change > 0)
1130 root 1.56 new_info_map (NDI_BLACK | NDI_UNIQUE, this, "It becomes darker.");
1131 root 1.29 else
1132 root 1.56 new_info_map (NDI_BLACK | NDI_UNIQUE, this, "It becomes brighter.");
1133 root 1.29
1134 root 1.56 /* Do extra checking. since darkness is a unsigned value,
1135 root 1.29 * 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 root 1.56 darkness = 0;
1141 root 1.29 else if (new_level >= MAX_DARKNESS)
1142 root 1.56 darkness = MAX_DARKNESS;
1143 root 1.29 else
1144 root 1.56 darkness = new_level;
1145 elmex 1.1
1146 root 1.29 /* All clients need to get re-updated for the change */
1147 root 1.56 update_all_map_los (this);
1148 root 1.29 return 1;
1149 elmex 1.1 }
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 root 1.29 void
1158 root 1.46 mapspace::update_ ()
1159 root 1.29 {
1160 root 1.45 object *tmp, *last = 0;
1161 root 1.46 uint8 flags = 0, light = 0, anywhere = 0;
1162 root 1.29 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 root 1.45 top = blank_face;
1168     floor = blank_face;
1169 root 1.29
1170 root 1.45 middle_obj = 0;
1171     top_obj = 0;
1172     floor_obj = 0;
1173 root 1.29
1174 root 1.49 for (tmp = bot; tmp; last = tmp, tmp = tmp->above)
1175 root 1.29 {
1176     /* This could be made additive I guess (two lights better than
1177 root 1.45 * one). But if so, it shouldn't be a simple additive - 2
1178 root 1.29 * light bulbs do not illuminate twice as far as once since
1179 root 1.45 * it is a dissapation factor that is cubed.
1180 root 1.29 */
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 root 1.45
1228 root 1.29 if (tmp == tmp->above)
1229     {
1230     LOG (llevError, "Error in structure of map\n");
1231     exit (-1);
1232     }
1233    
1234 root 1.45 move_slow |= tmp->move_slow;
1235 root 1.29 move_block |= tmp->move_block;
1236 root 1.45 move_on |= tmp->move_on;
1237     move_off |= tmp->move_off;
1238 root 1.29 move_allow |= tmp->move_allow;
1239    
1240 root 1.45 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 root 1.29
1248 root 1.46 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 root 1.29
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 root 1.10
1288 root 1.29 /* 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 root 1.10 }
1314     }
1315     }
1316 elmex 1.1 }
1317 root 1.45
1318 root 1.29 if (middle == floor)
1319     middle = blank_face;
1320 root 1.45
1321 root 1.29 if (top == middle)
1322     middle = blank_face;
1323 root 1.45
1324 root 1.46 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 elmex 1.1 }
1328    
1329 root 1.29 void
1330 root 1.31 set_map_reset_time (maptile *map)
1331 root 1.29 {
1332 root 1.55 int timeout = map->reset_timeout;
1333 elmex 1.1
1334 root 1.29 if (timeout <= 0)
1335     timeout = MAP_DEFAULTRESET;
1336     if (timeout >= MAP_MAXRESET)
1337     timeout = MAP_MAXRESET;
1338 root 1.55
1339 root 1.49 map->reset_time = time (0) + timeout;
1340 elmex 1.1 }
1341    
1342     /* this updates the orig_map->tile_map[tile_num] value after loading
1343     * the map. It also takes care of linking back the freshly loaded
1344     * maps tile_map values if it tiles back to this one. It returns
1345     * the value of orig_map->tile_map[tile_num]. It really only does this
1346     * so that it is easier for calling functions to verify success.
1347     */
1348 root 1.31 static maptile *
1349     load_and_link_tiled_map (maptile *orig_map, int tile_num)
1350 elmex 1.1 {
1351 root 1.56 maptile *mp = orig_map->find_map (orig_map->tile_path[tile_num]);
1352     mp->load ();
1353    
1354 root 1.29 int dest_tile = (tile_num + 2) % 4;
1355 elmex 1.1
1356 root 1.56 orig_map->tile_map[tile_num] = mp;
1357 elmex 1.1
1358 root 1.29 /* need to do a strcmp here as the orig_map->path is not a shared string */
1359 root 1.56 if (orig_map->tile_map[tile_num]->tile_path[dest_tile] && orig_map->tile_map[tile_num]->tile_path[dest_tile] == orig_map->path)
1360 root 1.29 orig_map->tile_map[tile_num]->tile_map[dest_tile] = orig_map;
1361 elmex 1.1
1362 root 1.56 return mp;
1363 elmex 1.1 }
1364    
1365     /* this returns TRUE if the coordinates (x,y) are out of
1366     * map m. This function also takes into account any
1367     * tiling considerations, loading adjacant maps as needed.
1368     * This is the function should always be used when it
1369     * necessary to check for valid coordinates.
1370     * This function will recursively call itself for the
1371     * tiled maps.
1372     */
1373 root 1.29 int
1374 root 1.31 out_of_map (maptile *m, int x, int y)
1375 elmex 1.1 {
1376 root 1.29 /* If we get passed a null map, this is obviously the
1377     * case. This generally shouldn't happen, but if the
1378     * map loads fail below, it could happen.
1379     */
1380     if (!m)
1381     return 0;
1382 elmex 1.1
1383 root 1.29 if (x < 0)
1384     {
1385     if (!m->tile_path[3])
1386     return 1;
1387 root 1.46
1388 root 1.29 if (!m->tile_map[3] || m->tile_map[3]->in_memory != MAP_IN_MEMORY)
1389 root 1.46 load_and_link_tiled_map (m, 3);
1390    
1391 root 1.56 return out_of_map (m->tile_map[3], x + m->tile_map[3]->width, y);
1392 elmex 1.1 }
1393 root 1.46
1394 root 1.48 if (x >= m->width)
1395 root 1.29 {
1396     if (!m->tile_path[1])
1397     return 1;
1398 root 1.46
1399 root 1.29 if (!m->tile_map[1] || m->tile_map[1]->in_memory != MAP_IN_MEMORY)
1400 root 1.46 load_and_link_tiled_map (m, 1);
1401    
1402 root 1.56 return out_of_map (m->tile_map[1], x - m->width, y);
1403 elmex 1.1 }
1404 root 1.46
1405 root 1.29 if (y < 0)
1406     {
1407     if (!m->tile_path[0])
1408     return 1;
1409 root 1.46
1410 root 1.29 if (!m->tile_map[0] || m->tile_map[0]->in_memory != MAP_IN_MEMORY)
1411 root 1.46 load_and_link_tiled_map (m, 0);
1412    
1413 root 1.56 return out_of_map (m->tile_map[0], x, y + m->tile_map[0]->height);
1414 elmex 1.1 }
1415 root 1.46
1416 root 1.48 if (y >= m->height)
1417 root 1.29 {
1418     if (!m->tile_path[2])
1419     return 1;
1420 root 1.46
1421 root 1.29 if (!m->tile_map[2] || m->tile_map[2]->in_memory != MAP_IN_MEMORY)
1422 root 1.46 load_and_link_tiled_map (m, 2);
1423    
1424 root 1.56 return out_of_map (m->tile_map[2], x, y - m->height);
1425 elmex 1.1 }
1426    
1427 root 1.29 /* Simple case - coordinates are within this local
1428     * map.
1429     */
1430     return 0;
1431 elmex 1.1 }
1432    
1433     /* This is basically the same as out_of_map above, but
1434     * instead we return NULL if no map is valid (coordinates
1435     * out of bounds and no tiled map), otherwise it returns
1436     * the map as that the coordinates are really on, and
1437     * updates x and y to be the localized coordinates.
1438     * Using this is more efficient of calling out_of_map
1439     * and then figuring out what the real map is
1440     */
1441 root 1.31 maptile *
1442 root 1.56 get_map_from_coord (maptile *m, sint16 *x, sint16 *y)
1443 elmex 1.1 {
1444 root 1.29 if (*x < 0)
1445     {
1446     if (!m->tile_path[3])
1447 root 1.46 return 0;
1448 root 1.56
1449 root 1.29 if (!m->tile_map[3] || m->tile_map[3]->in_memory != MAP_IN_MEMORY)
1450     load_and_link_tiled_map (m, 3);
1451 elmex 1.1
1452 root 1.48 *x += m->tile_map[3]->width;
1453 root 1.29 return (get_map_from_coord (m->tile_map[3], x, y));
1454 elmex 1.1 }
1455 root 1.46
1456 root 1.48 if (*x >= m->width)
1457 root 1.29 {
1458     if (!m->tile_path[1])
1459 root 1.46 return 0;
1460    
1461 root 1.29 if (!m->tile_map[1] || m->tile_map[1]->in_memory != MAP_IN_MEMORY)
1462     load_and_link_tiled_map (m, 1);
1463 elmex 1.1
1464 root 1.48 *x -= m->width;
1465 root 1.29 return (get_map_from_coord (m->tile_map[1], x, y));
1466 elmex 1.1 }
1467 root 1.46
1468 root 1.29 if (*y < 0)
1469     {
1470     if (!m->tile_path[0])
1471 root 1.46 return 0;
1472    
1473 root 1.29 if (!m->tile_map[0] || m->tile_map[0]->in_memory != MAP_IN_MEMORY)
1474     load_and_link_tiled_map (m, 0);
1475 elmex 1.1
1476 root 1.48 *y += m->tile_map[0]->height;
1477 root 1.29 return (get_map_from_coord (m->tile_map[0], x, y));
1478 elmex 1.1 }
1479 root 1.46
1480 root 1.48 if (*y >= m->height)
1481 root 1.29 {
1482     if (!m->tile_path[2])
1483 root 1.46 return 0;
1484    
1485 root 1.29 if (!m->tile_map[2] || m->tile_map[2]->in_memory != MAP_IN_MEMORY)
1486     load_and_link_tiled_map (m, 2);
1487 elmex 1.1
1488 root 1.48 *y -= m->height;
1489 root 1.29 return (get_map_from_coord (m->tile_map[2], x, y));
1490 elmex 1.1 }
1491    
1492 root 1.29 /* Simple case - coordinates are within this local
1493     * map.
1494     */
1495 elmex 1.1
1496 root 1.56 m->last_access = runtime;
1497 root 1.29 return m;
1498 elmex 1.1 }
1499    
1500     /**
1501     * Return whether map2 is adjacent to map1. If so, store the distance from
1502     * map1 to map2 in dx/dy.
1503     */
1504 root 1.29 static int
1505 root 1.31 adjacent_map (const maptile *map1, const maptile *map2, int *dx, int *dy)
1506 root 1.29 {
1507     if (!map1 || !map2)
1508     return 0;
1509    
1510     if (map1 == map2)
1511     {
1512     *dx = 0;
1513     *dy = 0;
1514     }
1515     else if (map1->tile_map[0] == map2)
1516     { /* up */
1517     *dx = 0;
1518 root 1.48 *dy = -map2->height;
1519 root 1.29 }
1520     else if (map1->tile_map[1] == map2)
1521     { /* right */
1522 root 1.48 *dx = map1->width;
1523 root 1.29 *dy = 0;
1524     }
1525     else if (map1->tile_map[2] == map2)
1526     { /* down */
1527     *dx = 0;
1528 root 1.48 *dy = map1->height;
1529 root 1.29 }
1530     else if (map1->tile_map[3] == map2)
1531     { /* left */
1532 root 1.48 *dx = -map2->width;
1533 root 1.29 *dy = 0;
1534     }
1535     else if (map1->tile_map[0] && map1->tile_map[0]->tile_map[1] == map2)
1536     { /* up right */
1537 root 1.48 *dx = map1->tile_map[0]->width;
1538     *dy = -map1->tile_map[0]->height;
1539 root 1.29 }
1540     else if (map1->tile_map[0] && map1->tile_map[0]->tile_map[3] == map2)
1541     { /* up left */
1542 root 1.48 *dx = -map2->width;
1543     *dy = -map1->tile_map[0]->height;
1544 root 1.29 }
1545     else if (map1->tile_map[1] && map1->tile_map[1]->tile_map[0] == map2)
1546     { /* right up */
1547 root 1.48 *dx = map1->width;
1548     *dy = -map2->height;
1549 root 1.29 }
1550     else if (map1->tile_map[1] && map1->tile_map[1]->tile_map[2] == map2)
1551     { /* right down */
1552 root 1.48 *dx = map1->width;
1553     *dy = map1->tile_map[1]->height;
1554 root 1.29 }
1555     else if (map1->tile_map[2] && map1->tile_map[2]->tile_map[1] == map2)
1556     { /* down right */
1557 root 1.48 *dx = map1->tile_map[2]->width;
1558     *dy = map1->height;
1559 root 1.29 }
1560     else if (map1->tile_map[2] && map1->tile_map[2]->tile_map[3] == map2)
1561     { /* down left */
1562 root 1.48 *dx = -map2->width;
1563     *dy = map1->height;
1564 root 1.29 }
1565     else if (map1->tile_map[3] && map1->tile_map[3]->tile_map[0] == map2)
1566     { /* left up */
1567 root 1.48 *dx = -map1->tile_map[3]->width;
1568     *dy = -map2->height;
1569 root 1.29 }
1570     else if (map1->tile_map[3] && map1->tile_map[3]->tile_map[2] == map2)
1571     { /* left down */
1572 root 1.48 *dx = -map1->tile_map[3]->width;
1573     *dy = map1->tile_map[3]->height;
1574 root 1.29 }
1575     else
1576 root 1.56 return 0;
1577 elmex 1.1
1578 root 1.29 return 1;
1579 elmex 1.1 }
1580    
1581     /* From map.c
1582     * This is used by get_player to determine where the other
1583     * creature is. get_rangevector takes into account map tiling,
1584     * so you just can not look the the map coordinates and get the
1585     * righte value. distance_x/y are distance away, which
1586     * can be negativbe. direction is the crossfire direction scheme
1587     * that the creature should head. part is the part of the
1588     * monster that is closest.
1589     *
1590     * get_rangevector looks at op1 and op2, and fills in the
1591     * structure for op1 to get to op2.
1592     * We already trust that the caller has verified that the
1593     * two objects are at least on adjacent maps. If not,
1594     * results are not likely to be what is desired.
1595     * if the objects are not on maps, results are also likely to
1596     * be unexpected
1597     *
1598     * currently, the only flag supported (0x1) is don't translate for
1599     * closest body part of 'op1'
1600     */
1601 root 1.29 void
1602     get_rangevector (object *op1, object *op2, rv_vector * retval, int flags)
1603     {
1604     if (!adjacent_map (op1->map, op2->map, &retval->distance_x, &retval->distance_y))
1605     {
1606     /* be conservative and fill in _some_ data */
1607     retval->distance = 100000;
1608     retval->distance_x = 32767;
1609     retval->distance_y = 32767;
1610     retval->direction = 0;
1611     retval->part = 0;
1612     }
1613     else
1614     {
1615     object *best;
1616    
1617     retval->distance_x += op2->x - op1->x;
1618     retval->distance_y += op2->y - op1->y;
1619    
1620     best = op1;
1621     /* If this is multipart, find the closest part now */
1622     if (!(flags & 0x1) && op1->more)
1623     {
1624     object *tmp;
1625     int best_distance = retval->distance_x * retval->distance_x + retval->distance_y * retval->distance_y, tmpi;
1626    
1627     /* we just take the offset of the piece to head to figure
1628     * distance instead of doing all that work above again
1629     * since the distance fields we set above are positive in the
1630     * same axis as is used for multipart objects, the simply arithmetic
1631     * below works.
1632     */
1633     for (tmp = op1->more; tmp != NULL; tmp = tmp->more)
1634     {
1635     tmpi = (op1->x - tmp->x + retval->distance_x) * (op1->x - tmp->x + retval->distance_x) +
1636     (op1->y - tmp->y + retval->distance_y) * (op1->y - tmp->y + retval->distance_y);
1637     if (tmpi < best_distance)
1638     {
1639     best_distance = tmpi;
1640     best = tmp;
1641 elmex 1.1 }
1642     }
1643 root 1.29 if (best != op1)
1644     {
1645     retval->distance_x += op1->x - best->x;
1646     retval->distance_y += op1->y - best->y;
1647 elmex 1.1 }
1648     }
1649 root 1.29 retval->part = best;
1650     retval->distance = isqrt (retval->distance_x * retval->distance_x + retval->distance_y * retval->distance_y);
1651     retval->direction = find_dir_2 (-retval->distance_x, -retval->distance_y);
1652 elmex 1.1 }
1653     }
1654    
1655     /* this is basically the same as get_rangevector above, but instead of
1656     * the first parameter being an object, it instead is the map
1657     * and x,y coordinates - this is used for path to player -
1658     * since the object is not infact moving but we are trying to traverse
1659     * the path, we need this.
1660     * flags has no meaning for this function at this time - I kept it in to
1661     * be more consistant with the above function and also in case they are needed
1662     * for something in the future. Also, since no object is pasted, the best
1663     * field of the rv_vector is set to NULL.
1664     */
1665    
1666 root 1.29 void
1667 root 1.31 get_rangevector_from_mapcoord (const maptile *m, int x, int y, const object *op2, rv_vector * retval, int flags)
1668 root 1.29 {
1669     if (!adjacent_map (m, op2->map, &retval->distance_x, &retval->distance_y))
1670     {
1671     /* be conservative and fill in _some_ data */
1672     retval->distance = 100000;
1673     retval->distance_x = 32767;
1674     retval->distance_y = 32767;
1675     retval->direction = 0;
1676     retval->part = 0;
1677     }
1678     else
1679     {
1680     retval->distance_x += op2->x - x;
1681     retval->distance_y += op2->y - y;
1682    
1683     retval->part = NULL;
1684     retval->distance = isqrt (retval->distance_x * retval->distance_x + retval->distance_y * retval->distance_y);
1685     retval->direction = find_dir_2 (-retval->distance_x, -retval->distance_y);
1686 elmex 1.1 }
1687     }
1688    
1689     /* Returns true of op1 and op2 are effectively on the same map
1690     * (as related to map tiling). Note that this looks for a path from
1691 root 1.56 * op1 to op2, so if the tiled maps are asymetric and op2 has a path
1692 elmex 1.1 * to op1, this will still return false.
1693     * Note we only look one map out to keep the processing simple
1694     * and efficient. This could probably be a macro.
1695     * MSW 2001-08-05
1696     */
1697 root 1.29 int
1698     on_same_map (const object *op1, const object *op2)
1699     {
1700     int dx, dy;
1701 elmex 1.1
1702 root 1.29 return adjacent_map (op1->map, op2->map, &dx, &dy);
1703 elmex 1.1 }
1704 root 1.52
1705     object *
1706     maptile::insert (object *op, int x, int y, object *originator, int flags)
1707     {
1708     if (!op->flag [FLAG_REMOVED])
1709     op->remove ();
1710    
1711     return insert_ob_in_map_at (op, this, originator, flags, x, y);
1712     }
1713