ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/common/map.C
Revision: 1.115
Committed: Wed Aug 1 01:53:13 2007 UTC (16 years, 9 months ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.114: +0 -7 lines
Log Message:
tweaked sound a bit, added death sounds with another property (wastage?)

File Contents

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