ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/common/map.C
Revision: 1.102
Committed: Thu May 17 21:32:08 2007 UTC (17 years ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.101: +13 -12 lines
Log Message:
- prepare common/ for head_ => head change
- add some copyrights for files where they were missing

File Contents

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