ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/common/map.C
Revision: 1.114
Committed: Thu Jul 26 00:27:07 2007 UTC (16 years, 10 months ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.113: +27 -66 lines
Log Message:
very very preliminary, non-working sound framework

File Contents

# Content
1 /*
2 * This file is part of Crossfire TRT, the Roguelike Realtime MORPG.
3 *
4 * 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 *
8 * 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 *
13 * 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 *
18 * 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 *
21 * The authors can be reached via e-mail to <crossfire@schmorp.de>
22 */
23
24 #include <unistd.h>
25
26 #include "global.h"
27 #include "funcpoint.h"
28
29 #include "loader.h"
30
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 int
44 get_map_flags (maptile *oldmap, maptile **newmap, sint16 x, sint16 y, sint16 *nx, sint16 *ny)
45 {
46 sint16 newx = x;
47 sint16 newy = y;
48
49 maptile *mp = get_map_from_coord (oldmap, &newx, &newy);
50
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
58 return mp->at (newx, newy).flags () | (mp != oldmap ? P_NEW_MAP : 0);
59 }
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 int
74 blocked_link (object *ob, maptile *m, int sx, int sy)
75 {
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 }
87
88 /* Save some cycles - instead of calling get_map_flags(), just get the value
89 * directly.
90 */
91 mflags = m->at (sx, sy).flags ();
92
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 ob = ob->head_ ();
113
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 for (tmp = GET_MAP_OB (m, sx, sy); tmp; tmp = tmp->above)
120 {
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 * second - if a monster, can't move there, unless it is a
153 * hidden dm
154 */
155 if (OB_MOVE_BLOCK (ob, tmp))
156 return 1;
157
158 if (tmp->flag [FLAG_ALIVE]
159 && tmp->head_ () != ob
160 && tmp != ob
161 && tmp->type != DOOR
162 && !(tmp->flag [FLAG_WIZ] && tmp->contr->hidden))
163 return 1;
164 }
165
166 }
167 return 0;
168 }
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 int
197 ob_blocked (const object *ob, maptile *m, sint16 x, sint16 y)
198 {
199 archetype *tmp;
200 int flag;
201 maptile *m1;
202 sint16 sx, sy;
203
204 if (!ob)
205 {
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
210 /* don't have object, so don't know what types would block */
211 return m1->at (sx, sy).move_block;
212 }
213
214 for (tmp = ob->arch; tmp; tmp = (archetype *)tmp->more)
215 {
216 flag = get_map_flags (m, &m1, x + tmp->x, y + tmp->y, &sx, &sy);
217
218 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 mapspace &ms = m1->at (sx, sy);
224
225 /* 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
230 if (ob->move_type == 0 && ms.move_block != MOVE_ALL)
231 continue;
232
233 /* 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 if (OB_TYPE_MOVE_BLOCK (ob, ms.move_block))
237 return P_NO_PASS;
238 }
239
240 return 0;
241 }
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 void
250 fix_container (object *container)
251 {
252 object *tmp = container->inv, *next;
253
254 container->inv = 0;
255 while (tmp)
256 {
257 next = tmp->below;
258 if (tmp->inv)
259 fix_container (tmp);
260
261 insert_ob_in_ob (tmp, container);
262 tmp = next;
263 }
264
265 /* sum_weight will go through and calculate what all the containers are
266 * carrying.
267 */
268 sum_weight (container);
269 }
270
271 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 /* 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 * they are saved).
286 */
287 void
288 maptile::link_multipart_objects ()
289 {
290 if (!spaces)
291 return;
292
293 for (mapspace *ms = spaces + size (); ms-- > spaces; )
294 {
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 }
317
318 /*
319 * Loads (ands parses) the objects into a given map from the specified
320 * file pointer.
321 */
322 bool
323 maptile::_load_objects (object_thawer &f)
324 {
325 for (;;)
326 {
327 coroapi::cede_to_tick_every (100); // cede once in a while
328
329 switch (f.kw)
330 {
331 case KW_arch:
332 if (object *op = object::read (f, this))
333 {
334 if (op->inv)
335 sum_weight (op);
336
337 insert_ob_in_map (op, this, op, INS_NO_MERGE | INS_NO_WALK_ON | INS_ON_TOP | INS_MAP_LOAD);
338 }
339
340 continue;
341
342 case KW_EOF:
343 return true;
344
345 default:
346 if (!f.parse_error ("map file"))
347 return false;
348 break;
349 }
350
351 f.next ();
352 }
353
354 return true;
355 }
356
357 void
358 maptile::activate ()
359 {
360 active = true;
361
362 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 }
367
368 void
369 maptile::deactivate ()
370 {
371 active = false;
372
373 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 }
378
379 bool
380 maptile::_save_objects (object_freezer &f, int flags)
381 {
382 coroapi::cede_to_tick ();
383
384 if (flags & IO_HEADER)
385 _save_header (f);
386
387 if (!spaces)
388 return false;
389
390 for (int i = 0; i < size (); ++i)
391 {
392 int unique = 0;
393 for (object *op = spaces [i].bot; op; op = op->above)
394 {
395 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 {
403 if (flags & IO_UNIQUES)
404 op->write (f);
405 }
406 else if (flags & IO_OBJECTS)
407 op->write (f);
408 }
409 }
410
411 coroapi::cede_to_tick ();
412
413 return true;
414 }
415
416 bool
417 maptile::_load_objects (const char *path, bool skip_header)
418 {
419 object_thawer f (path);
420
421 if (!f)
422 return false;
423
424 f.next ();
425
426 if (skip_header)
427 for (;;)
428 {
429 keyword kw = f.kw;
430 f.skip ();
431 if (kw == KW_end)
432 break;
433 }
434
435 return _load_objects (f);
436 }
437
438 bool
439 maptile::_save_objects (const char *path, int flags)
440 {
441 object_freezer freezer;
442
443 if (!_save_objects (freezer, flags))
444 return false;
445
446 return freezer.save (path);
447 }
448
449 maptile::maptile ()
450 {
451 in_memory = MAP_SWAPPED;
452
453 /* The maps used to pick up default x and y values from the
454 * map archetype. Mimic that behaviour.
455 */
456 width = 16;
457 height = 16;
458 timeout = 300;
459 max_nrof = 1000; // 1000 items of anything
460 max_volume = 2000000; // 2m³
461 }
462
463 maptile::maptile (int w, int h)
464 {
465 in_memory = MAP_SWAPPED;
466
467 width = w;
468 height = h;
469 reset_timeout = 0;
470 timeout = 300;
471 enter_x = 0;
472 enter_y = 0;
473
474 alloc ();
475 }
476
477 /*
478 * Allocates the arrays contained in a maptile.
479 * This basically allocates the dynamic array of spaces for the
480 * map.
481 */
482 void
483 maptile::alloc ()
484 {
485 if (spaces)
486 return;
487
488 spaces = salloc0<mapspace> (size ());
489 }
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 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 shop_string = strdup (input_string);
505 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
515 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
526 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
563 items[i].index = number_of_entries;
564 if (next_semicolon)
565 p = ++next_semicolon;
566 else
567 p = NULL;
568 }
569
570 free (shop_string);
571 return items;
572 }
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 static void
577 print_shop_string (maptile *m, char *output_string)
578 {
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 sprintf (tmp, "%s:%d;", m->shopitems[i].name, m->shopitems[i].strength);
589 else
590 sprintf (tmp, "%s;", m->shopitems[i].name);
591 }
592 else
593 {
594 if (m->shopitems[i].strength)
595 sprintf (tmp, "*:%d;", m->shopitems[i].strength);
596 else
597 sprintf (tmp, "*");
598 }
599
600 strcat (output_string, tmp);
601 }
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 bool
616 maptile::_load_header (object_thawer &thawer)
617 {
618 for (;;)
619 {
620 thawer.next ();
621
622 switch (thawer.kw)
623 {
624 case KW_msg:
625 thawer.get_ml (KW_endmsg, msg);
626 break;
627
628 case KW_lore: // CF+ extension
629 thawer.get_ml (KW_endlore, maplore);
630 break;
631
632 case KW_maplore:
633 thawer.get_ml (KW_endmaplore, maplore);
634 break;
635
636 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
641 case KW_oid:
642 thawer.get (this, thawer.get_sint32 ());
643 break;
644
645 case KW_file_format_version: break; // nop
646
647 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 case KW_no_reset: thawer.get (no_reset); break;
665
666 case KW_region: default_region = region::find (thawer.get_str ()); break;
667 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
685 case KW_ERROR:
686 set_key (thawer.kw_str, thawer.value);
687 break;
688
689 case KW_end:
690 return true;
691
692 default:
693 if (!thawer.parse_error ("map", 0))
694 return false;
695 break;
696 }
697 }
698
699 abort ();
700 }
701
702 bool
703 maptile::_load_header (const char *path)
704 {
705 object_thawer thawer (path);
706
707 if (!thawer)
708 return false;
709
710 return _load_header (thawer);
711 }
712
713 /******************************************************************************
714 * This is the start of unique map handling code
715 *****************************************************************************/
716
717 /* This goes through the maptile and removed any unique items on the map. */
718 void
719 maptile::clear_unique_items ()
720 {
721 for (int i = 0; i < size (); ++i)
722 {
723 int unique = 0;
724 for (object *op = spaces [i].bot; op; )
725 {
726 object *above = op->above;
727
728 if (QUERY_FLAG (op, FLAG_IS_FLOOR) && QUERY_FLAG (op, FLAG_UNIQUE))
729 unique = 1;
730
731 if (op->head_ () == op && (QUERY_FLAG (op, FLAG_UNIQUE) || unique))
732 {
733 op->destroy_inv (false);
734 op->destroy ();
735 }
736
737 op = above;
738 }
739 }
740 }
741
742 bool
743 maptile::_save_header (object_freezer &freezer)
744 {
745 #define MAP_OUT(k) freezer.put (KW_ ## k, k)
746 #define MAP_OUT2(k,v) freezer.put (KW_ ## k, v)
747
748 MAP_OUT2 (arch, "map");
749
750 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 MAP_OUT (no_reset);
756 MAP_OUT (difficulty);
757
758 if (default_region) MAP_OUT2 (region, default_region->name);
759
760 if (shopitems)
761 {
762 char shop[MAX_BUF];
763 print_shop_string (this, shop);
764 MAP_OUT2 (shopitems, shop);
765 }
766
767 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
777 if (msg) freezer.put (KW_msg , KW_endmsg , msg);
778 if (maplore) freezer.put (KW_maplore, KW_endmaplore, maplore);
779
780 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
788 MAP_OUT (per_player);
789 MAP_OUT (per_party);
790
791 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
796 freezer.put (this);
797 freezer.put (KW_end);
798
799 return true;
800 }
801
802 bool
803 maptile::_save_header (const char *path)
804 {
805 object_freezer freezer;
806
807 if (!_save_header (freezer))
808 return false;
809
810 return freezer.save (path);
811 }
812
813 /*
814 * Remove and free all objects in the given map.
815 */
816 void
817 maptile::clear ()
818 {
819 sfree (regions, size ()); regions = 0;
820 delete [] regionmap; regionmap = 0;
821
822 if (spaces)
823 {
824 for (mapspace *ms = spaces + size (); ms-- > spaces; )
825 while (object *op = ms->bot)
826 {
827 op = op->head_ ();
828 op->destroy_inv (false);
829 op->destroy ();
830 }
831
832 sfree (spaces, size ()), spaces = 0;
833 }
834
835 if (buttons)
836 free_objectlinkpt (buttons), buttons = 0;
837 }
838
839 void
840 maptile::clear_header ()
841 {
842 name = 0;
843 msg = 0;
844 maplore = 0;
845 shoprace = 0;
846 delete [] shopitems, shopitems = 0;
847
848 for (int i = 0; i < 4; i++)
849 tile_path [i] = 0;
850 }
851
852 maptile::~maptile ()
853 {
854 assert (destroyed ());
855 }
856
857 void
858 maptile::clear_links_to (maptile *m)
859 {
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 * tiling can be asymetric, we just can not look to see which
863 * maps this map tiles with and clears those.
864 */
865 for (int i = 0; i < 4; i++)
866 if (tile_map[i] == m)
867 tile_map[i] = 0;
868 }
869
870 void
871 maptile::do_destroy ()
872 {
873 attachable::do_destroy ();
874
875 clear ();
876 }
877
878 /* 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 /*
947 * Updates every button on the map (by calling update_button() for them).
948 */
949 void
950 maptile::update_buttons ()
951 {
952 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
962 if (ol->ob->type == BUTTON || ol->ob->type == PEDESTAL)
963 {
964 update_button (ol->ob);
965 break;
966 }
967 }
968 }
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 int
981 maptile::estimate_difficulty () const
982 {
983 long monster_cnt = 0;
984 double avgexp = 0;
985 sint64 total_exp = 0;
986
987 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
996 if (QUERY_FLAG (op, FLAG_GENERATOR))
997 {
998 total_exp += op->stats.exp;
999
1000 if (archetype *at = type_to_archetype (GENERATE_TYPE (op)))
1001 total_exp += at->stats.exp * 8;
1002
1003 monster_cnt++;
1004 }
1005 }
1006
1007 avgexp = (double) total_exp / monster_cnt;
1008
1009 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
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 int
1024 maptile::change_map_light (int change)
1025 {
1026 int new_level = darkness + change;
1027
1028 /* Nothing to do */
1029 if (!change || (new_level <= 0 && darkness == 0) || (new_level >= MAX_DARKNESS && darkness >= MAX_DARKNESS))
1030 return 0;
1031
1032 /* inform all players on the map */
1033 if (change > 0)
1034 new_info_map (NDI_BLACK | NDI_UNIQUE, this, "It becomes darker.");
1035 else
1036 new_info_map (NDI_BLACK | NDI_UNIQUE, this, "It becomes brighter.");
1037
1038 /* Do extra checking. since darkness is a unsigned value,
1039 * 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 darkness = 0;
1045 else if (new_level >= MAX_DARKNESS)
1046 darkness = MAX_DARKNESS;
1047 else
1048 darkness = new_level;
1049
1050 /* All clients need to get re-updated for the change */
1051 update_all_map_los (this);
1052 return 1;
1053 }
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 void
1062 mapspace::update_ ()
1063 {
1064 object *tmp, *last = 0;
1065 uint8 flags = P_UPTODATE, light = 0, anywhere = 0;
1066 MoveType move_block = 0, move_slow = 0, move_on = 0, move_off = 0, move_allow = 0;
1067
1068 //object *middle = 0;
1069 //object *top = 0;
1070 //object *floor = 0;
1071 // this seems to generate better code than using locals, above
1072 object *&top = faces_obj[0] = 0;
1073 object *&middle = faces_obj[1] = 0;
1074 object *&floor = faces_obj[2] = 0;
1075
1076 for (tmp = bot; tmp; last = tmp, tmp = tmp->above)
1077 {
1078 /* This could be made additive I guess (two lights better than
1079 * one). But if so, it shouldn't be a simple additive - 2
1080 * light bulbs do not illuminate twice as far as once since
1081 * it is a dissapation factor that is cubed.
1082 */
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 top = tmp;
1098 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 middle = 0;
1104 top = 0;
1105 floor = tmp;
1106 }
1107 /* Flag anywhere have high priority */
1108 else if (QUERY_FLAG (tmp, FLAG_SEE_ANYWHERE))
1109 {
1110 middle = tmp;
1111 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 else if (!middle || (::faces [tmp->face].visibility > ::faces [middle->face].visibility && !anywhere))
1118 middle = tmp;
1119 }
1120
1121 if (tmp == tmp->above)
1122 {
1123 LOG (llevError, "Error in structure of map\n");
1124 exit (-1);
1125 }
1126
1127 move_slow |= tmp->move_slow;
1128 move_block |= tmp->move_block;
1129 move_on |= tmp->move_on;
1130 move_off |= tmp->move_off;
1131 move_allow |= tmp->move_allow;
1132
1133 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
1141 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
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 middle = 0;
1164
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 if (top && middle)
1179 break;
1180
1181 /* Only show visible faces */
1182 if (!tmp->invisible)
1183 {
1184 /* Fill in top if needed */
1185 if (!top)
1186 {
1187 top = tmp;
1188 if (top == middle)
1189 middle = 0;
1190 }
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 if (tmp != top)
1201 {
1202 middle = tmp;
1203 break;
1204 }
1205 }
1206 }
1207 }
1208
1209 if (middle == floor)
1210 middle = 0;
1211
1212 if (top == middle)
1213 middle = 0;
1214
1215 #if 0
1216 faces_obj [0] = top;
1217 faces_obj [1] = middle;
1218 faces_obj [2] = floor;
1219 #endif
1220 }
1221
1222 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 /* 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 * maps tile_map values if it tiles back to this one. It returns
1236 * the value of orig_map->tile_map[tile_num].
1237 */
1238 static inline maptile *
1239 find_and_link (maptile *orig_map, int tile_num)
1240 {
1241 maptile *mp = orig_map->tile_map [tile_num];
1242
1243 if (!mp)
1244 {
1245 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 }
1257
1258 int dest_tile = (tile_num + 2) % 4;
1259
1260 orig_map->tile_map [tile_num] = mp;
1261
1262 // 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 orig_map->tile_map[tile_num]->tile_map[dest_tile] = orig_map;
1266
1267 return mp;
1268 }
1269
1270 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 /* 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 int
1285 out_of_map (maptile *m, int x, int y)
1286 {
1287 /* 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
1294 if (x < 0)
1295 {
1296 if (!m->tile_path[3])
1297 return 1;
1298
1299 if (!m->tile_map[3] || m->tile_map[3]->in_memory != MAP_IN_MEMORY)
1300 find_and_link (m, 3);
1301
1302 return out_of_map (m->tile_map[3], x + m->tile_map[3]->width, y);
1303 }
1304
1305 if (x >= m->width)
1306 {
1307 if (!m->tile_path[1])
1308 return 1;
1309
1310 if (!m->tile_map[1] || m->tile_map[1]->in_memory != MAP_IN_MEMORY)
1311 find_and_link (m, 1);
1312
1313 return out_of_map (m->tile_map[1], x - m->width, y);
1314 }
1315
1316 if (y < 0)
1317 {
1318 if (!m->tile_path[0])
1319 return 1;
1320
1321 if (!m->tile_map[0] || m->tile_map[0]->in_memory != MAP_IN_MEMORY)
1322 find_and_link (m, 0);
1323
1324 return out_of_map (m->tile_map[0], x, y + m->tile_map[0]->height);
1325 }
1326
1327 if (y >= m->height)
1328 {
1329 if (!m->tile_path[2])
1330 return 1;
1331
1332 if (!m->tile_map[2] || m->tile_map[2]->in_memory != MAP_IN_MEMORY)
1333 find_and_link (m, 2);
1334
1335 return out_of_map (m->tile_map[2], x, y - m->height);
1336 }
1337
1338 /* Simple case - coordinates are within this local
1339 * map.
1340 */
1341 return 0;
1342 }
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 * updates x and y to be the localised coordinates.
1349 * Using this is more efficient of calling out_of_map
1350 * and then figuring out what the real map is
1351 */
1352 maptile *
1353 maptile::xy_find (sint16 &x, sint16 &y)
1354 {
1355 if (x < 0)
1356 {
1357 if (!tile_path[3])
1358 return 0;
1359
1360 find_and_link (this, 3);
1361 x += tile_map[3]->width;
1362 return tile_map[3]->xy_find (x, y);
1363 }
1364
1365 if (x >= width)
1366 {
1367 if (!tile_path[1])
1368 return 0;
1369
1370 find_and_link (this, 1);
1371 x -= width;
1372 return tile_map[1]->xy_find (x, y);
1373 }
1374
1375 if (y < 0)
1376 {
1377 if (!tile_path[0])
1378 return 0;
1379
1380 find_and_link (this, 0);
1381 y += tile_map[0]->height;
1382 return tile_map[0]->xy_find (x, y);
1383 }
1384
1385 if (y >= height)
1386 {
1387 if (!tile_path[2])
1388 return 0;
1389
1390 find_and_link (this, 2);
1391 y -= height;
1392 return tile_map[2]->xy_find (x, y);
1393 }
1394
1395 /* Simple case - coordinates are within this local
1396 * map.
1397 */
1398 return this;
1399 }
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 int
1406 adjacent_map (const maptile *map1, const maptile *map2, int *dx, int *dy)
1407 {
1408 if (!map1 || !map2)
1409 return 0;
1410
1411 //TODO: this doesn't actually check corretcly when intermediate maps are not loaded
1412 //fix: compare paths instead (this is likely faster, too!)
1413 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 *dy = -map2->height;
1422 }
1423 else if (map1->tile_map[1] == map2)
1424 { /* right */
1425 *dx = map1->width;
1426 *dy = 0;
1427 }
1428 else if (map1->tile_map[2] == map2)
1429 { /* down */
1430 *dx = 0;
1431 *dy = map1->height;
1432 }
1433 else if (map1->tile_map[3] == map2)
1434 { /* left */
1435 *dx = -map2->width;
1436 *dy = 0;
1437 }
1438 else if (map1->tile_map[0] && map1->tile_map[0]->tile_map[1] == map2)
1439 { /* up right */
1440 *dx = map1->tile_map[0]->width;
1441 *dy = -map1->tile_map[0]->height;
1442 }
1443 else if (map1->tile_map[0] && map1->tile_map[0]->tile_map[3] == map2)
1444 { /* up left */
1445 *dx = -map2->width;
1446 *dy = -map1->tile_map[0]->height;
1447 }
1448 else if (map1->tile_map[1] && map1->tile_map[1]->tile_map[0] == map2)
1449 { /* right up */
1450 *dx = map1->width;
1451 *dy = -map2->height;
1452 }
1453 else if (map1->tile_map[1] && map1->tile_map[1]->tile_map[2] == map2)
1454 { /* right down */
1455 *dx = map1->width;
1456 *dy = map1->tile_map[1]->height;
1457 }
1458 else if (map1->tile_map[2] && map1->tile_map[2]->tile_map[1] == map2)
1459 { /* down right */
1460 *dx = map1->tile_map[2]->width;
1461 *dy = map1->height;
1462 }
1463 else if (map1->tile_map[2] && map1->tile_map[2]->tile_map[3] == map2)
1464 { /* down left */
1465 *dx = -map2->width;
1466 *dy = map1->height;
1467 }
1468 else if (map1->tile_map[3] && map1->tile_map[3]->tile_map[0] == map2)
1469 { /* left up */
1470 *dx = -map1->tile_map[3]->width;
1471 *dy = -map2->height;
1472 }
1473 else if (map1->tile_map[3] && map1->tile_map[3]->tile_map[2] == map2)
1474 { /* left down */
1475 *dx = -map1->tile_map[3]->width;
1476 *dy = map1->tile_map[3]->height;
1477 }
1478 else
1479 return 0;
1480
1481 return 1;
1482 }
1483
1484 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 /* 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 * can be negative. direction is the crossfire direction scheme
1507 * 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 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 retval->distance = 10000;
1528 retval->distance_x = 10000;
1529 retval->distance_y = 10000;
1530 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 for (object *tmp = op1->more; tmp; tmp = tmp->more)
1553 {
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 }
1561 }
1562
1563 if (best != op1)
1564 {
1565 retval->distance_x += op1->x - best->x;
1566 retval->distance_y += op1->y - best->y;
1567 }
1568 }
1569
1570 retval->part = best;
1571 retval->distance = upos_max (abs (retval->distance_x), abs (retval->distance_y));
1572 retval->direction = find_dir_2 (-retval->distance_x, -retval->distance_y);
1573 }
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 void
1587 get_rangevector_from_mapcoord (const maptile *m, int x, int y, const object *op2, rv_vector *retval, int flags)
1588 {
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 retval->distance = idistance (retval->distance_x, retval->distance_y);
1605 retval->direction = find_dir_2 (-retval->distance_x, -retval->distance_y);
1606 }
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 * op1 to op2, so if the tiled maps are asymetric and op2 has a path
1612 * 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 int
1618 on_same_map (const object *op1, const object *op2)
1619 {
1620 int dx, dy;
1621
1622 return adjacent_map (op1->map, op2->map, &dx, &dy);
1623 }
1624
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 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 /* 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 if (pick && pick->head_ () == pick)
1668 return pick->head_ ();
1669 }
1670
1671 // instead of crashing in the unlikely(?) case, try to return *something*
1672 return get_archetype ("blocked");
1673 }
1674
1675 /**
1676 * Maximum distance a player may hear a sound from.
1677 * This is only used for new client/server sound. If the sound source
1678 * on the map is farther away than this, we don't sent it to the client.
1679 */
1680 #define MAX_SOUND_DISTANCE 10
1681
1682 void
1683 maptile::play_sound (faceidx sound, int x, int y) const
1684 {
1685 if (!sound)
1686 return;
1687
1688 for_all_players (pl)
1689 if (pl->ob->map == this)
1690 if (client *ns = pl->ns)
1691 {
1692 int dx = x - pl->ob->x;
1693 int dy = y - pl->ob->y;
1694
1695 int distance = idistance (dx, dy);
1696
1697 if (distance <= MAX_SOUND_DISTANCE)
1698 ns->play_sound (sound, dx, dy);
1699 }
1700 }
1701