/* * This file is part of Deliantra, the Roguelike Realtime MMORPG. * * Copyright (©) 2005,2006,2007,2008 Marc Alexander Lehmann / Robin Redeker / the Deliantra team * Copyright (©) 2001-2003,2007 Mark Wedel & Crossfire Development Team * Copyright (©) 1992,2007 Frank Tore Johansen * * Deliantra is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . * * The authors can be reached via e-mail to */ #include #include "global.h" #include "loader.h" #include "path.h" sint8 maptile::outdoor_darkness; /* This rolls up wall, blocks_magic, blocks_view, etc, all into * one function that just returns a P_.. value (see map.h) * it will also do map translation for tiled maps, returning * new values into newmap, nx, and ny. Any and all of those * values can be null, in which case if a new map is needed (returned * by a P_NEW_MAP value, another call to get_map_from_coord * is needed. The case of not passing values is if we're just * checking for the existence of something on those spaces, but * don't expect to insert/remove anything from those spaces. */ int get_map_flags (maptile *oldmap, maptile **newmap, sint16 x, sint16 y, sint16 *nx, sint16 *ny) { sint16 newx = x; sint16 newy = y; maptile *mp = get_map_from_coord (oldmap, &newx, &newy); if (!mp) return P_OUT_OF_MAP; if (newmap) *newmap = mp; if (nx) *nx = newx; if (ny) *ny = newy; return mp->at (newx, newy).flags () | (mp != oldmap ? P_NEW_MAP : 0); } /* * Returns true if the given coordinate is blocked except by the * object passed is not blocking. This is used with * multipart monsters - if we want to see if a 2x2 monster * can move 1 space to the left, we don't want its own area * to block it from moving there. * Returns TRUE if the space is blocked by something other than the * monster. * m, x, y are the target map/coordinates - needed for map tiling. * the coordinates & map passed in should have been updated for tiling * by the caller. */ int blocked_link (object *ob, maptile *m, int sx, int sy) { object *tmp; int mflags, blocked; /* Make sure the coordinates are valid - they should be, as caller should * have already checked this. */ if (OUT_OF_REAL_MAP (m, sx, sy)) { LOG (llevError, "blocked_link: Passed map, x, y coordinates outside of map\n"); return 1; } /* Save some cycles - instead of calling get_map_flags(), just get the value * directly. */ mflags = m->at (sx, sy).flags (); blocked = GET_MAP_MOVE_BLOCK (m, sx, sy); /* If space is currently not blocked by anything, no need to * go further. Not true for players - all sorts of special * things we need to do for players. */ if (ob->type != PLAYER && !(mflags & P_IS_ALIVE) && (blocked == 0)) return 0; /* if there isn't anytyhing alive on this space, and this space isn't * otherwise blocked, we can return now. Only if there is a living * creature do we need to investigate if it is part of this creature * or another. Likewise, only if something is blocking us do we * need to investigate if there is a special circumstance that would * let the player through (inventory checkers for example) */ if (!(mflags & P_IS_ALIVE) && !OB_TYPE_MOVE_BLOCK (ob, blocked)) return 0; ob = ob->head_ (); /* We basically go through the stack of objects, and if there is * some other object that has NO_PASS or FLAG_ALIVE set, return * true. If we get through the entire stack, that must mean * ob is blocking it, so return 0. */ for (tmp = GET_MAP_OB (m, sx, sy); tmp; tmp = tmp->above) { /* This must be before the checks below. Code for inventory checkers. */ if (tmp->type == CHECK_INV && OB_MOVE_BLOCK (ob, tmp)) { /* If last_sp is set, the player/monster needs an object, * so we check for it. If they don't have it, they can't * pass through this space. */ if (tmp->last_sp) { if (check_inv_recursive (ob, tmp) == NULL) return 1; else continue; } else { /* In this case, the player must not have the object - * if they do, they can't pass through. */ if (check_inv_recursive (ob, tmp) != NULL) /* player has object */ return 1; else continue; } } /* if check_inv */ else { /* Broke apart a big nasty if into several here to make * this more readable. first check - if the space blocks * movement, can't move here. * second - if a monster, can't move there, unless it is a * hidden dm */ if (OB_MOVE_BLOCK (ob, tmp)) return 1; if (tmp->flag [FLAG_ALIVE] && tmp->head_ () != ob && tmp != ob && tmp->type != DOOR && !(tmp->flag [FLAG_WIZ] && tmp->contr->hidden)) return 1; } } return 0; } /* * Returns qthe blocking object if the given object can't fit in the given * spot. This is meant for multi space objects - for single space objecs, * just calling get_map_blocked and checking that against movement type * of object. This function goes through all the parts of the multipart * object and makes sure they can be inserted. * * While this doesn't call out of map, the get_map_flags does. * * This function has been used to deprecate arch_out_of_map - * this function also does that check, and since in most cases, * a call to one would follow the other, doesn't make a lot of sense to * have two seperate functions for this. * * This returns nonzero if this arch can not go on the space provided, * 0 otherwise. the return value will contain the P_.. value * so the caller can know why this object can't go on the map. * Note that callers should not expect P_NEW_MAP to be set * in return codes - since the object is multispace - if * we did return values, what do you return if half the object * is one map, half on another. * * Note this used to be arch_blocked, but with new movement * code, we need to have actual object to check its move_type * against the move_block values. */ bool object::blocked (maptile *m, int x, int y) const { for (archetype *tmp = arch; tmp; tmp = (archetype *)tmp->more) { mapxy pos (m, x + tmp->x, y + tmp->y); if (!pos.normalise ()) return 1; mapspace &ms = *pos; if (ms.flags () & P_IS_ALIVE) return 1; /* However, often ob doesn't have any move type * (signifying non-moving objects) * so the AND operation in OB_TYPE_MOVE_BLOCK doesn't work. */ if (!move_type && ms.move_block != MOVE_ALL) continue; /* Note it is intentional that we check ob - the movement type of the * head of the object should correspond for the entire object. */ if (ms.blocks (move_type)) return 1; } return 0; } /* When the map is loaded, load_object does not actually insert objects * into inventory, but just links them. What this does is go through * and insert them properly. * The object 'container' is the object that contains the inventory. * This is needed so that we can update the containers weight. */ void fix_container (object *container) { object *tmp = container->inv, *next; container->inv = 0; while (tmp) { next = tmp->below; if (tmp->inv) fix_container (tmp); insert_ob_in_ob (tmp, container); tmp = next; } // go through and calculate what all the containers are carrying. //TODO: remove container->update_weight (); } void maptile::set_object_flag (int flag, int value) { if (!spaces) return; for (mapspace *ms = spaces + size (); ms-- > spaces; ) for (object *tmp = ms->bot; tmp; tmp = tmp->above) tmp->flag [flag] = value; } void maptile::post_load_original () { if (!spaces) return; set_object_flag (FLAG_OBJ_ORIGINAL); for (mapspace *ms = spaces + size (); ms-- > spaces; ) for (object *tmp = ms->bot; tmp; tmp = tmp->above) INVOKE_OBJECT (RESET, tmp); } /* link_multipart_objects go through all the objects on the map looking * for objects whose arch says they are multipart yet according to the * info we have, they only have the head (as would be expected when * they are saved). */ void maptile::link_multipart_objects () { if (!spaces) return; for (mapspace *ms = spaces + size (); ms-- > spaces; ) { object *op = ms->bot; while (op) { /* already multipart - don't do anything more */ if (op->head_ () == op && !op->more && op->arch->more) { op->remove (); op->expand_tail (); // FIXME: INS_ON_TOP is just a workaround for the pentagram vs. // multi-tile monster bug, where INS_ABOVE_FLOOR_ONLY put the monsters // below the pentagrams... hopefully INS_ON_TOP doesn't break anything insert (op, op->x, op->y, 0, INS_NO_MERGE | INS_ON_TOP | INS_NO_WALK_ON); op = ms->bot; // we are mutating the mapspace too much with INS_ON_TOP // so we have to reset the iteration through the mapspace } else op = op->above; } } } /* * Loads (ands parses) the objects into a given map from the specified * file pointer. */ bool maptile::_load_objects (object_thawer &f) { for (;;) { coroapi::cede_to_tick (); // cede once in a while switch (f.kw) { case KW_arch: if (object *op = object::read (f, this)) { // TODO: why? if (op->inv) op->update_weight (); if (IN_RANGE_EXC (op->x, 0, width) && IN_RANGE_EXC (op->y, 0, height)) { // we insert manually because // a) its way faster // b) we remove manually, too, and there are good reasons for that // c) its correct mapspace &ms = at (op->x, op->y); op->flag [FLAG_REMOVED] = false; op->above = 0; op->below = ms.top; if (ms.top) ms.top->above = op; else ms.bot = op; ms.top = op; ms.flags_ = 0; } else { f.parse_warn (format ("object %s out of range", op->debug_desc ())); op->destroy (); } } continue; case KW_EOF: return true; default: if (!f.parse_error ("map file")) return false; break; } f.next (); } return true; } void maptile::activate () { if (spaces) for (mapspace *ms = spaces + size (); ms-- > spaces; ) for (object *op = ms->bot; op; op = op->above) op->activate_recursive (); } void maptile::deactivate () { if (spaces) for (mapspace *ms = spaces + size (); ms-- > spaces; ) for (object *op = ms->bot; op; op = op->above) op->deactivate_recursive (); } bool maptile::_save_objects (object_freezer &f, int flags) { coroapi::cede_to_tick (); if (flags & IO_HEADER) _save_header (f); if (!spaces) return false; for (int i = 0; i < size (); ++i) { bool unique = 0; for (object *op = spaces [i].bot; op; op = op->above) { unique |= op->flag [FLAG_UNIQUE] && op->flag [FLAG_IS_FLOOR]; if (expect_false (!op->can_map_save ())) continue; if (expect_false (unique || op->flag [FLAG_UNIQUE])) { if (flags & IO_UNIQUES) op->write (f); } else if (expect_true (flags & IO_OBJECTS)) op->write (f); } } coroapi::cede_to_tick (); return true; } bool maptile::_save_objects (const char *path, int flags) { object_freezer freezer; if (!_save_objects (freezer, flags)) return false; return freezer.save (path); } maptile::maptile () { in_memory = MAP_SWAPPED; /* The maps used to pick up default x and y values from the * map archetype. Mimic that behaviour. */ width = 16; height = 16; timeout = 300; max_nrof = 1000; // 1000 items of anything max_volume = 2000000; // 2m³ } maptile::maptile (int w, int h) { in_memory = MAP_SWAPPED; width = w; height = h; reset_timeout = 0; timeout = 300; enter_x = 0; enter_y = 0; alloc (); } /* * Allocates the arrays contained in a maptile. * This basically allocates the dynamic array of spaces for the * map. */ void maptile::alloc () { if (spaces) return; spaces = salloc0 (size ()); } /* Takes a string from a map definition and outputs a pointer to the array of shopitems * corresponding to that string. Memory is allocated for this, it must be freed * at a later date. * Called by parse_map_headers below. */ static shopitems * parse_shop_string (const char *input_string) { char *shop_string, *p, *q, *next_semicolon, *next_colon; shopitems *items = NULL; int i = 0, number_of_entries = 0; const typedata *current_type; shop_string = strdup (input_string); p = shop_string; /* first we'll count the entries, we'll need that for allocating the array shortly */ while (p) { p = strchr (p, ';'); number_of_entries++; if (p) p++; } p = shop_string; strip_endline (p); items = new shopitems[number_of_entries + 1]; for (i = 0; i < number_of_entries; i++) { if (!p) { LOG (llevError, "parse_shop_string: I seem to have run out of string, that shouldn't happen.\n"); break; } next_semicolon = strchr (p, ';'); next_colon = strchr (p, ':'); /* if there is a stregth specified, figure out what it is, we'll need it soon. */ if (next_colon && (!next_semicolon || next_colon < next_semicolon)) items[i].strength = atoi (strchr (p, ':') + 1); if (isdigit (*p) || *p == '*') { items[i].typenum = atoi (p); /* atoi returns 0 when we have an asterisk */ current_type = get_typedata (items[i].typenum); if (current_type) { items[i].name = current_type->name; items[i].name_pl = current_type->name_pl; } } else { /*we have a named type, let's figure out what it is */ q = strpbrk (p, ";:"); if (q) *q = '\0'; current_type = get_typedata_by_name (p); if (current_type) { items[i].name = current_type->name; items[i].typenum = current_type->number; items[i].name_pl = current_type->name_pl; } else { /* oh uh, something's wrong, let's free up this one, and try * the next entry while we're at it, better print a warning */ LOG (llevError, "invalid type %s defined in shopitems in string %s\n", p, input_string); } } items[i].index = number_of_entries; if (next_semicolon) p = ++next_semicolon; else p = NULL; } free (shop_string); return items; } /* opposite of parse string, this puts the string that was originally fed in to * the map (or something equivilent) into output_string. */ static void print_shop_string (maptile *m, char *output_string) { int i; char tmp[MAX_BUF]; strcpy (output_string, ""); for (i = 0; i < m->shopitems[0].index; i++) { if (m->shopitems[i].typenum) { if (m->shopitems[i].strength) sprintf (tmp, "%s:%d;", m->shopitems[i].name, m->shopitems[i].strength); else sprintf (tmp, "%s;", m->shopitems[i].name); } else { if (m->shopitems[i].strength) sprintf (tmp, "*:%d;", m->shopitems[i].strength); else sprintf (tmp, "*"); } strcat (output_string, tmp); } } /* This loads the header information of the map. The header * contains things like difficulty, size, timeout, etc. * this used to be stored in the map object, but with the * addition of tiling, fields beyond that easily named in an * object structure were needed, so it just made sense to * put all the stuff in the map object so that names actually make * sense. * This could be done in lex (like the object loader), but I think * currently, there are few enough fields this is not a big deal. * MSW 2001-07-01 */ bool maptile::_load_header (object_thawer &thawer) { for (;;) { switch (thawer.kw) { case KW_msg: thawer.get_ml (KW_endmsg, msg); break; case KW_lore: // CF+ extension thawer.get_ml (KW_endlore, maplore); break; case KW_maplore: thawer.get_ml (KW_endmaplore, maplore); break; case KW_arch: if (strcmp (thawer.get_str (), "map")) LOG (llevError, "%s: loading map and got a non 'arch map' line (arch %s), skipping.\n", &path, thawer.get_str ()); break; case KW_oid: thawer.get (this, thawer.get_sint32 ()); break; case KW_file_format_version: break; // nop case KW_name: thawer.get (name); break; case KW_attach: thawer.get (attach); break; case KW_reset_time: thawer.get (reset_time); break; case KW_shopgreed: thawer.get (shopgreed); break; case KW_shopmin: thawer.get (shopmin); break; case KW_shopmax: thawer.get (shopmax); break; case KW_shoprace: thawer.get (shoprace); break; case KW_outdoor: thawer.get (outdoor); break; case KW_temp: thawer.get (temp); break; case KW_pressure: thawer.get (pressure); break; case KW_humid: thawer.get (humid); break; case KW_windspeed: thawer.get (windspeed); break; case KW_winddir: thawer.get (winddir); break; case KW_sky: thawer.get (sky); break; case KW_per_player: thawer.get (per_player); break; case KW_per_party: thawer.get (per_party); break; case KW_no_reset: thawer.get (no_reset); break; case KW_no_drop: thawer.get (no_drop); break; case KW_region: default_region = region::find (thawer.get_str ()); break; case KW_shopitems: shopitems = parse_shop_string (thawer.get_str ()); break; // old names new names case KW_hp: case KW_enter_x: thawer.get (enter_x); break; case KW_sp: case KW_enter_y: thawer.get (enter_y); break; case KW_x: case KW_width: thawer.get (width); break; case KW_y: case KW_height: thawer.get (height); break; case KW_weight: case KW_reset_timeout: thawer.get (reset_timeout); break; case KW_value: case KW_swap_time: thawer.get (timeout); break; case KW_level: case KW_difficulty: thawer.get (difficulty); difficulty = clamp (difficulty, 1, settings.max_level); break; case KW_invisible: case KW_darkness: thawer.get (darkness); break; case KW_stand_still: case KW_fixed_resettime: thawer.get (fixed_resettime); break; case KW_tile_path_1: thawer.get (tile_path [0]); break; case KW_tile_path_2: thawer.get (tile_path [1]); break; case KW_tile_path_3: thawer.get (tile_path [2]); break; case KW_tile_path_4: thawer.get (tile_path [3]); break; case KW_ERROR: set_key_text (thawer.kw_str, thawer.value); break; case KW_end: thawer.next (); return true; default: if (!thawer.parse_error ("map", 0)) return false; break; } thawer.next (); } abort (); } /****************************************************************************** * This is the start of unique map handling code *****************************************************************************/ /* This goes through the maptile and removed any unique items on the map. */ void maptile::clear_unique_items () { for (int i = 0; i < size (); ++i) { int unique = 0; for (object *op = spaces [i].bot; op; ) { object *above = op->above; if (QUERY_FLAG (op, FLAG_IS_FLOOR) && QUERY_FLAG (op, FLAG_UNIQUE)) unique = 1; if (op->head_ () == op && (QUERY_FLAG (op, FLAG_UNIQUE) || unique)) op->destroy (); op = above; } } } bool maptile::_save_header (object_freezer &freezer) { #define MAP_OUT(k) freezer.put (KW_ ## k, k) #define MAP_OUT2(k,v) freezer.put (KW_ ## k, v) MAP_OUT2 (arch, "map"); if (name) MAP_OUT (name); MAP_OUT (swap_time); MAP_OUT (reset_time); MAP_OUT (reset_timeout); MAP_OUT (fixed_resettime); MAP_OUT (no_reset); MAP_OUT (no_drop); MAP_OUT (difficulty); if (default_region) MAP_OUT2 (region, default_region->name); if (shopitems) { char shop[MAX_BUF]; print_shop_string (this, shop); MAP_OUT2 (shopitems, shop); } MAP_OUT (shopgreed); MAP_OUT (shopmin); MAP_OUT (shopmax); if (shoprace) MAP_OUT (shoprace); MAP_OUT (darkness); MAP_OUT (width); MAP_OUT (height); MAP_OUT (enter_x); MAP_OUT (enter_y); if (msg) freezer.put (KW_msg , KW_endmsg , msg); if (maplore) freezer.put (KW_maplore, KW_endmaplore, maplore); MAP_OUT (outdoor); MAP_OUT (temp); MAP_OUT (pressure); MAP_OUT (humid); MAP_OUT (windspeed); MAP_OUT (winddir); MAP_OUT (sky); MAP_OUT (per_player); MAP_OUT (per_party); if (tile_path [0]) MAP_OUT2 (tile_path_1, tile_path [0]); if (tile_path [1]) MAP_OUT2 (tile_path_2, tile_path [1]); if (tile_path [2]) MAP_OUT2 (tile_path_3, tile_path [2]); if (tile_path [3]) MAP_OUT2 (tile_path_4, tile_path [3]); freezer.put (this); freezer.put (KW_end); return true; } bool maptile::_save_header (const char *path) { object_freezer freezer; if (!_save_header (freezer)) return false; return freezer.save (path); } /* * Remove and free all objects in the given map. */ void maptile::clear () { if (spaces) { for (mapspace *ms = spaces + size (); ms-- > spaces; ) while (object *op = ms->bot) { // manually remove, as to not trigger anything if (ms->bot = op->above) ms->bot->below = 0; op->flag [FLAG_REMOVED] = true; object *head = op->head_ (); if (op == head) op->destroy (); else if (head->map != op->map) { LOG (llevDebug, "bad luck for object crossing map borders: %s", head->debug_desc ()); head->destroy (); } } sfree0 (spaces, size ()); } if (buttons) free_objectlinkpt (buttons), buttons = 0; sfree0 (regions, size ()); delete [] regionmap; regionmap = 0; } void maptile::clear_header () { name = 0; msg = 0; maplore = 0; shoprace = 0; delete [] shopitems, shopitems = 0; for (int i = 0; i < 4; i++) tile_path [i] = 0; } maptile::~maptile () { assert (destroyed ()); } void maptile::clear_links_to (maptile *m) { /* We need to look through all the maps and see if any maps * are pointing at this one for tiling information. Since * tiling can be asymetric, we just can not look to see which * maps this map tiles with and clears those. */ for (int i = 0; i < 4; i++) if (tile_map[i] == m) tile_map[i] = 0; } void maptile::do_destroy () { attachable::do_destroy (); clear (); } /* decay and destroy perishable items in a map */ void maptile::do_decay_objects () { if (!spaces) return; for (mapspace *ms = spaces + size (); ms-- > spaces; ) for (object *above, *op = ms->bot; op; op = above) { above = op->above; bool destroy = 0; // do not decay anything above unique floor tiles (yet :) if (QUERY_FLAG (op, FLAG_IS_FLOOR) && QUERY_FLAG (op, FLAG_UNIQUE)) break; if (QUERY_FLAG (op, FLAG_IS_FLOOR) || QUERY_FLAG (op, FLAG_OBJ_ORIGINAL) || QUERY_FLAG (op, FLAG_UNIQUE) || QUERY_FLAG (op, FLAG_OVERLAY_FLOOR) || QUERY_FLAG (op, FLAG_UNPAID) || op->is_alive ()) ; // do not decay else if (op->is_weapon ()) { op->stats.dam--; if (op->stats.dam < 0) destroy = 1; } else if (op->is_armor ()) { op->stats.ac--; if (op->stats.ac < 0) destroy = 1; } else if (op->type == FOOD) { op->stats.food -= rndm (5, 20); if (op->stats.food < 0) destroy = 1; } else { int mat = op->materials; if (mat & M_PAPER || mat & M_LEATHER || mat & M_WOOD || mat & M_ORGANIC || mat & M_CLOTH || mat & M_LIQUID || (mat & M_IRON && rndm (1, 5) == 1) || (mat & M_GLASS && rndm (1, 2) == 1) || ((mat & M_STONE || mat & M_ADAMANT) && rndm (1, 10) == 1) || ((mat & M_SOFT_METAL || mat & M_BONE) && rndm (1, 3) == 1) || (mat & M_ICE && temp > 32)) destroy = 1; } /* adjust overall chance below */ if (destroy && rndm (0, 1)) op->destroy (); } } /* * Updates every button on the map (by calling update_button() for them). */ void maptile::update_buttons () { for (oblinkpt *obp = buttons; obp; obp = obp->next) for (objectlink *ol = obp->link; ol; ol = ol->next) { if (!ol->ob) { LOG (llevError, "Internal error in update_button (%s (%dx%d), connected %ld).\n", ol->ob ? (const char *) ol->ob->name : "null", ol->ob ? ol->ob->x : -1, ol->ob ? ol->ob->y : -1, obp->value); continue; } if (ol->ob->type == BUTTON || ol->ob->type == PEDESTAL) { update_button (ol->ob); break; } } } /* * This routine is supposed to find out the difficulty of the map. * difficulty does not have a lot to do with character level, * but does have a lot to do with treasure on the map. * * Difficulty can now be set by the map creature. If the value stored * in the map is zero, then use this routine. Maps should really * have a difficulty set than using this function - human calculation * is much better than this functions guesswork. */ int maptile::estimate_difficulty () const { long monster_cnt = 0; double avgexp = 0; sint64 total_exp = 0; for (mapspace *ms = spaces + size (); ms-- > spaces; ) for (object *op = ms->bot; op; op = op->above) { if (QUERY_FLAG (op, FLAG_MONSTER)) { total_exp += op->stats.exp; monster_cnt++; } if (QUERY_FLAG (op, FLAG_GENERATOR)) { total_exp += op->stats.exp; if (archetype *at = op->other_arch) { total_exp += at->stats.exp * 8; monster_cnt++; } for (object *inv = op->inv; inv; inv = inv->below) { total_exp += op->stats.exp * 8; monster_cnt++; } } } avgexp = (double) total_exp / monster_cnt; for (int i = 1; i <= settings.max_level; i++) if ((level_exp (i, 1) - level_exp (i - 1, 1)) > (100 * avgexp)) return i; return 1; } /* change_map_light() - used to change map light level (darkness) * up or down. Returns true if successful. It should now be * possible to change a value by more than 1. * Move this from los.c to map.c since this is more related * to maps than los. * postive values make it darker, negative make it brighter */ int maptile::change_map_light (int change) { int new_level = darkness + change; /* Nothing to do */ if (!change || (new_level <= 0 && darkness == 0) || (new_level >= MAX_DARKNESS && darkness >= MAX_DARKNESS)) return 0; /* inform all players on the map */ if (change > 0) new_info_map (NDI_BLACK | NDI_UNIQUE, this, "It becomes darker."); else new_info_map (NDI_BLACK | NDI_UNIQUE, this, "It becomes brighter."); /* Do extra checking. since darkness is a unsigned value, * we need to be extra careful about negative values. * In general, the checks below are only needed if change * is not +/-1 */ if (new_level < 0) darkness = 0; else if (new_level >= MAX_DARKNESS) darkness = MAX_DARKNESS; else darkness = new_level; /* All clients need to get re-updated for the change */ update_all_map_los (this); return 1; } /* * This function updates various attributes about a specific space * on the map (what it looks like, whether it blocks magic, * has a living creatures, prevents people from passing * through, etc) */ void mapspace::update_ () { object *last = 0; uint8 flags = P_UPTODATE, anywhere = 0; sint8 light = 0; MoveType move_block = 0, move_slow = 0, move_on = 0, move_off = 0, move_allow = 0; //object *middle = 0; //object *top = 0; //object *floor = 0; // this seems to generate better code than using locals, above object *&top = faces_obj[0] = 0; object *&middle = faces_obj[1] = 0; object *&floor = faces_obj[2] = 0; for (object *tmp = bot; tmp; last = tmp, tmp = tmp->above) { // Lights are additive, up to MAX_LIGHT_RADIUS, see los.C) light += tmp->glow_radius; /* This call is needed in order to update objects the player * is standing in that have animations (ie, grass, fire, etc). * However, it also causes the look window to be re-drawn * 3 times each time the player moves, because many of the * functions the move_player calls eventualy call this. * * Always put the player down for drawing. */ if (!tmp->invisible) { if ((tmp->type == PLAYER || QUERY_FLAG (tmp, FLAG_MONSTER))) top = tmp; else if (QUERY_FLAG (tmp, FLAG_IS_FLOOR)) { /* If we got a floor, that means middle and top were below it, * so should not be visible, so we clear them. */ middle = 0; top = 0; floor = tmp; } /* Flag anywhere have high priority */ else if (QUERY_FLAG (tmp, FLAG_SEE_ANYWHERE)) { middle = tmp; anywhere = 1; } /* Find the highest visible face around. If equal * visibilities, we still want the one nearer to the * top */ else if (!middle || (::faces [tmp->face].visibility > ::faces [middle->face].visibility && !anywhere)) middle = tmp; } if (tmp == tmp->above) { LOG (llevError, "Error in structure of map\n"); exit (-1); } move_slow |= tmp->move_slow; move_block |= tmp->move_block; move_on |= tmp->move_on; move_off |= tmp->move_off; move_allow |= tmp->move_allow; if (QUERY_FLAG (tmp, FLAG_BLOCKSVIEW)) flags |= P_BLOCKSVIEW; if (QUERY_FLAG (tmp, FLAG_NO_MAGIC)) flags |= P_NO_MAGIC; if (tmp->type == PLAYER) flags |= P_PLAYER; if (tmp->type == SAFE_GROUND) flags |= P_SAFE; if (QUERY_FLAG (tmp, FLAG_ALIVE)) flags |= P_IS_ALIVE; if (QUERY_FLAG (tmp, FLAG_DAMNED)) flags |= P_NO_CLERIC; } this->light = min (light, MAX_LIGHT_RADIUS); this->flags_ = flags; this->move_block = move_block & ~move_allow; this->move_on = move_on; this->move_off = move_off; this->move_slow = move_slow; /* At this point, we have a floor face (if there is a floor), * and the floor is set - we are not going to touch it at * this point. * middle contains the highest visibility face. * top contains a player/monster face, if there is one. * * We now need to fill in top.face and/or middle.face. */ /* If the top face also happens to be high visibility, re-do our * middle face. This should not happen, as we already have the * else statement above so middle should not get set. OTOH, it * may be possible for the faces to match but be different objects. */ if (top == middle) middle = 0; /* There are three posibilities at this point: * 1) top face is set, need middle to be set. * 2) middle is set, need to set top. * 3) neither middle or top is set - need to set both. */ for (object *tmp = last; tmp; tmp = tmp->below) { /* Once we get to a floor, stop, since we already have a floor object */ if (QUERY_FLAG (tmp, FLAG_IS_FLOOR)) break; /* If two top faces are already set, quit processing */ if (top && middle) break; /* Only show visible faces */ if (!tmp->invisible) { /* Fill in top if needed */ if (!top) { top = tmp; if (top == middle) middle = 0; } else { /* top is already set - we should only get here if * middle is not set * * Set the middle face and break out, since there is nothing * more to fill in. We don't check visiblity here, since * */ if (tmp != top) { middle = tmp; break; } } } } if (middle == floor) middle = 0; if (top == middle) middle = 0; #if 0 faces_obj [0] = top; faces_obj [1] = middle; faces_obj [2] = floor; #endif } uint64 mapspace::volume () const { uint64 vol = 0; for (object *op = top; op && !op->flag [FLAG_NO_PICK]; op = op->below) vol += op->volume (); return vol; } bool maptile::tile_available (int dir, bool load) { if (!tile_path[dir]) return 0; if (tile_map[dir] && (!load || tile_map[dir]->in_memory == MAP_ACTIVE)) return 1; if ((tile_map[dir] = find_async (tile_path[dir], this, load))) return 1; return 0; } /* this returns TRUE if the coordinates (x,y) are out of * map m. This function also takes into account any * tiling considerations, loading adjacant maps as needed. * This is the function should always be used when it * necessary to check for valid coordinates. * This function will recursively call itself for the * tiled maps. */ int out_of_map (maptile *m, int x, int y) { /* If we get passed a null map, this is obviously the * case. This generally shouldn't happen, but if the * map loads fail below, it could happen. */ if (!m) return 0; if (x < 0) { if (!m->tile_available (3)) return 1; return out_of_map (m->tile_map[3], x + m->tile_map[3]->width, y); } if (x >= m->width) { if (!m->tile_available (1)) return 1; return out_of_map (m->tile_map[1], x - m->width, y); } if (y < 0) { if (!m->tile_available (0)) return 1; return out_of_map (m->tile_map[0], x, y + m->tile_map[0]->height); } if (y >= m->height) { if (!m->tile_available (2)) return 1; return out_of_map (m->tile_map[2], x, y - m->height); } /* Simple case - coordinates are within this local * map. */ return 0; } /* This is basically the same as out_of_map above, but * instead we return NULL if no map is valid (coordinates * out of bounds and no tiled map), otherwise it returns * the map as that the coordinates are really on, and * updates x and y to be the localised coordinates. * Using this is more efficient of calling out_of_map * and then figuring out what the real map is */ maptile * maptile::xy_find (sint16 &x, sint16 &y) { if (x < 0) { if (!tile_available (3)) return 0; x += tile_map[3]->width; return tile_map[3]->xy_find (x, y); } if (x >= width) { if (!tile_available (1)) return 0; x -= width; return tile_map[1]->xy_find (x, y); } if (y < 0) { if (!tile_available (0)) return 0; y += tile_map[0]->height; return tile_map[0]->xy_find (x, y); } if (y >= height) { if (!tile_available (2)) return 0; y -= height; return tile_map[2]->xy_find (x, y); } /* Simple case - coordinates are within this local * map. */ return this; } /** * Return whether map2 is adjacent to map1. If so, store the distance from * map1 to map2 in dx/dy. */ int adjacent_map (const maptile *map1, const maptile *map2, int *dx, int *dy) { if (!map1 || !map2) return 0; //TODO: this doesn't actually check correctly when intermediate maps are not loaded //fix: compare paths instead (this is likely faster, too!) if (map1 == map2) { *dx = 0; *dy = 0; } else if (map1->tile_map[0] == map2) { /* up */ *dx = 0; *dy = -map2->height; } else if (map1->tile_map[1] == map2) { /* right */ *dx = map1->width; *dy = 0; } else if (map1->tile_map[2] == map2) { /* down */ *dx = 0; *dy = map1->height; } else if (map1->tile_map[3] == map2) { /* left */ *dx = -map2->width; *dy = 0; } else if (map1->tile_map[0] && map1->tile_map[0]->tile_map[1] == map2) { /* up right */ *dx = map1->tile_map[0]->width; *dy = -map1->tile_map[0]->height; } else if (map1->tile_map[0] && map1->tile_map[0]->tile_map[3] == map2) { /* up left */ *dx = -map2->width; *dy = -map1->tile_map[0]->height; } else if (map1->tile_map[1] && map1->tile_map[1]->tile_map[0] == map2) { /* right up */ *dx = map1->width; *dy = -map2->height; } else if (map1->tile_map[1] && map1->tile_map[1]->tile_map[2] == map2) { /* right down */ *dx = map1->width; *dy = map1->tile_map[1]->height; } else if (map1->tile_map[2] && map1->tile_map[2]->tile_map[1] == map2) { /* down right */ *dx = map1->tile_map[2]->width; *dy = map1->height; } else if (map1->tile_map[2] && map1->tile_map[2]->tile_map[3] == map2) { /* down left */ *dx = -map2->width; *dy = map1->height; } else if (map1->tile_map[3] && map1->tile_map[3]->tile_map[0] == map2) { /* left up */ *dx = -map1->tile_map[3]->width; *dy = -map2->height; } else if (map1->tile_map[3] && map1->tile_map[3]->tile_map[2] == map2) { /* left down */ *dx = -map1->tile_map[3]->width; *dy = map1->tile_map[3]->height; } else return 0; return 1; } maptile * maptile::xy_load (sint16 &x, sint16 &y) { maptile *map = xy_find (x, y); if (map) map->load_sync (); return map; } maptile * get_map_from_coord (maptile *m, sint16 *x, sint16 *y) { return m->xy_load (*x, *y); } /* From map.c * This is used by get_player to determine where the other * creature is. get_rangevector takes into account map tiling, * so you just can not look the the map coordinates and get the * righte value. distance_x/y are distance away, which * can be negative. direction is the crossfire direction scheme * that the creature should head. part is the part of the * monster that is closest. * * get_rangevector looks at op1 and op2, and fills in the * structure for op1 to get to op2. * We already trust that the caller has verified that the * two objects are at least on adjacent maps. If not, * results are not likely to be what is desired. * if the objects are not on maps, results are also likely to * be unexpected * * currently, the only flag supported (0x1) is don't translate for * closest body part of 'op1' */ void get_rangevector (object *op1, object *op2, rv_vector * retval, int flags) { if (!adjacent_map (op1->map, op2->map, &retval->distance_x, &retval->distance_y)) { /* be conservative and fill in _some_ data */ retval->distance = 10000; retval->distance_x = 10000; retval->distance_y = 10000; retval->direction = 0; retval->part = 0; } else { object *best; retval->distance_x += op2->x - op1->x; retval->distance_y += op2->y - op1->y; best = op1; /* If this is multipart, find the closest part now */ if (!(flags & 0x1) && op1->more) { int best_distance = retval->distance_x * retval->distance_x + retval->distance_y * retval->distance_y, tmpi; /* we just take the offset of the piece to head to figure * distance instead of doing all that work above again * since the distance fields we set above are positive in the * same axis as is used for multipart objects, the simply arithmetic * below works. */ for (object *tmp = op1->more; tmp; tmp = tmp->more) { tmpi = (op1->x - tmp->x + retval->distance_x) * (op1->x - tmp->x + retval->distance_x) + (op1->y - tmp->y + retval->distance_y) * (op1->y - tmp->y + retval->distance_y); if (tmpi < best_distance) { best_distance = tmpi; best = tmp; } } if (best != op1) { retval->distance_x += op1->x - best->x; retval->distance_y += op1->y - best->y; } } retval->part = best; retval->distance = upos_max (abs (retval->distance_x), abs (retval->distance_y)); retval->direction = find_dir_2 (-retval->distance_x, -retval->distance_y); } } /* this is basically the same as get_rangevector above, but instead of * the first parameter being an object, it instead is the map * and x,y coordinates - this is used for path to player - * since the object is not infact moving but we are trying to traverse * the path, we need this. * flags has no meaning for this function at this time - I kept it in to * be more consistant with the above function and also in case they are needed * for something in the future. Also, since no object is pasted, the best * field of the rv_vector is set to NULL. */ void get_rangevector_from_mapcoord (const maptile *m, int x, int y, const object *op2, rv_vector *retval, int flags) { if (!adjacent_map (m, op2->map, &retval->distance_x, &retval->distance_y)) { /* be conservative and fill in _some_ data */ retval->distance = 100000; retval->distance_x = 32767; retval->distance_y = 32767; retval->direction = 0; retval->part = 0; } else { retval->distance_x += op2->x - x; retval->distance_y += op2->y - y; retval->part = NULL; retval->distance = idistance (retval->distance_x, retval->distance_y); retval->direction = find_dir_2 (-retval->distance_x, -retval->distance_y); } } /* Returns true of op1 and op2 are effectively on the same map * (as related to map tiling). Note that this looks for a path from * op1 to op2, so if the tiled maps are asymetric and op2 has a path * to op1, this will still return false. * Note we only look one map out to keep the processing simple * and efficient. This could probably be a macro. * MSW 2001-08-05 */ int on_same_map (const object *op1, const object *op2) { int dx, dy; return adjacent_map (op1->map, op2->map, &dx, &dy); } object * maptile::insert (object *op, int x, int y, object *originator, int flags) { return insert_ob_in_map_at (op, this, originator, flags, x, y); } region * maptile::region (int x, int y) const { if (regions && regionmap && !OUT_OF_REAL_MAP (this, x, y)) if (struct region *reg = regionmap [regions [y * width + x]]) return reg; if (default_region) return default_region; return ::region::default_region (); } /* picks a random object from a style map. */ object * maptile::pick_random_object (rand_gen &gen) const { /* while returning a null object will result in a crash, that * is actually preferable to an infinite loop. That is because * most servers will automatically restart in case of crash. * Change the logic on getting the random space - shouldn't make * any difference, but this seems clearer to me. */ for (int i = 1000; --i;) { object *pick = at (gen (width), gen (height)).bot; // do not prefer big monsters just because they are big. if (pick && pick->is_head ()) return pick->head_ (); } // instead of crashing in the unlikely(?) case, try to return *something* return archetype::find ("bug"); } void maptile::play_sound (faceidx sound, int x, int y) const { if (!sound) return; for_all_players_on_map (pl, this) if (client *ns = pl->ns) { int dx = x - pl->ob->x; int dy = y - pl->ob->y; int distance = idistance (dx, dy); if (distance <= MAX_SOUND_DISTANCE) ns->play_sound (sound, dx, dy); } }