/* * This file is part of Deliantra, the Roguelike Realtime MMORPG. * * Copyright (©) 2005,2006,2007,2008 Marc Alexander Lehmann / Robin Redeker / the Deliantra team * Copyright (©) 2002,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 /* * This code is no longer highly inefficient 8) */ /* * elmex: * This function takes a objectlink list with all the objects are going to be activated. * state is a true/false flag that will actiavte objects that have FLAG_ACTIVATE_ON_PUSH/RELEASE set. * The source argument can be 0 or the source object for this activation. */ void activate_connection_link (objectlink * ol, bool state, object *source = 0) { for (; ol; ol = ol->next) { if (!ol->ob) { LOG (llevError, "Internal error in activate_connection_link.\n"); continue; } /* a button link object can become freed when the map is saving. As * a map is saved, objects are removed and freed, and if an object is * on top of a button, this function is eventually called. If a map * is getting moved out of memory, the status of buttons and levers * probably isn't important - it will get sorted out when the map is * re-loaded. As such, just exit this function if that is the case. */ if (QUERY_FLAG (ol->ob, FLAG_FREED)) return; object *tmp = ol->ob; /* if the criteria isn't appropriate, don't do anything */ if (state && !QUERY_FLAG (tmp, FLAG_ACTIVATE_ON_PUSH)) continue; if (!state && !QUERY_FLAG (tmp, FLAG_ACTIVATE_ON_RELEASE)) continue; switch (tmp->type) { case GATE: case HOLE: if (!tmp->active) tmp->play_sound (tmp->sound ? tmp->sound : sound_find (tmp->type == GATE ? "trigger_gate" : "trigger_hole")); tmp->value = tmp->stats.maxsp ? !state : state; tmp->set_speed (0.5); break; case CF_HANDLE: SET_ANIMATION (tmp, (tmp->value = tmp->stats.maxsp ? !state : state)); update_object (tmp, UP_OBJ_FACE); break; case SIGN: if (!tmp->stats.food || tmp->last_eat < tmp->stats.food) { tmp->play_sound (tmp->sound ? tmp->sound : sound_find ("trigger_sign")); new_info_map (NDI_UNIQUE | NDI_NAVY, tmp->map, tmp->msg); if (tmp->stats.food) tmp->last_eat++; } break; case ALTAR: tmp->play_sound (tmp->sound ? tmp->sound : sound_find ("trigger_altar")); tmp->value = 1; SET_ANIMATION (tmp, tmp->value); update_object (tmp, UP_OBJ_FACE); break; case BUTTON: case PEDESTAL: tmp->play_sound (tmp->sound ? tmp->sound : sound_find ("trigger_button")); tmp->value = state; SET_ANIMATION (tmp, tmp->value); update_object (tmp, UP_OBJ_FACE); break; case MOOD_FLOOR: do_mood_floor (tmp, source); break; case TIMED_GATE: if (!tmp->active) tmp->play_sound (tmp->sound ? tmp->sound : sound_find ("trigger_gate")); tmp->set_speed (tmp->arch->speed); tmp->value = tmp->arch->value; tmp->stats.sp = 1; tmp->stats.hp = tmp->stats.maxhp; /* Handle multipart gates. We copy the value for the other parts * from the head - this ensures that the data will consistent */ for (object *part = tmp->more; part; part = part->more) { part->value = tmp->value; part->stats.sp = tmp->stats.sp; part->stats.hp = tmp->stats.hp; part->set_speed (tmp->speed); } break; case DIRECTOR: case FIREWALL: if (!QUERY_FLAG (tmp, FLAG_ANIMATE) && tmp->type == FIREWALL) move_firewall (tmp); else { if ((tmp->stats.sp += tmp->stats.maxsp) > 8) /* next direction */ tmp->stats.sp = ((tmp->stats.sp - 1) % 8) + 1; animate_turning (tmp); } break; case TELEPORTER: move_teleporter (tmp); break; case CREATOR: move_creator (tmp); break; case TRIGGER_MARKER: //tmp->play_sound (tmp->sound ? tmp->sound : sound_find ("trigger_marker")); move_marker (tmp); break; case DUPLICATOR: move_duplicator (tmp); break; } } } /* * elmex: * This is the new push_button function, it got split up so that * you can activate connections without a button now! * old but still valid comment: * * Push the specified object. This can affect other buttons/gates/handles * altars/pedestals/holes in the whole map. * Changed the routine to loop through _all_ objects. * Better hurry with that linked list... * */ void push_button (object *op) { oblinkpt *obp = get_button_links (op); if (!obp) return; if (INVOKE_MAP (TRIGGER, op->map, ARG_INT64 (obp->value), ARG_INT (op->value))) return; activate_connection_link (obp->link, op->value, op); } /* * elmex: * This activates a connection, similar to push_button (object *op) but it takes * only a map, a connection value and a true or false flag that indicated whether * the connection was 'state' or 'released'. So that you can activate objects * who have FLAG_ACTIVATE_ON_PUSH/RELEASE set properly. * */ void activate_connection (maptile *map, long connection, bool state) { if (INVOKE_MAP (TRIGGER, map, ARG_INT64 (connection), ARG_INT (state))) return; oblinkpt *obp = get_connection_links (map, connection); if (obp) activate_connection_link (obp->link, state); } /* * Updates everything connected with the button op. * After changing the state of a button, this function must be called * to make sure that all gates and other buttons connected to the * button reacts to the (eventual) change of state. */ void update_button (object *op) { int any_down = 0, old_value = op->value; oblinkpt *obp = 0; objectlink *ol; obp = get_button_links (op); if (obp) for (ol = obp->link; ol; ol = ol->next) { if (!ol->ob) { LOG (llevDebug, "Internal error in update_button (%s).\n", &op->name); continue; } object *tmp = ol->ob; if (tmp->type == BUTTON) { sint32 total = 0; for (object *ab = tmp->above; ab; ab = ab->above) /* Basically, if the move_type matches that on what the * button wants, we count it. The second check is so that * objects who don't move (swords, etc) will count. Note that * this means that more work is needed to make buttons * that are only triggered by flying objects. */ if ((ab->move_type & tmp->move_on) || ab->move_type == 0) total += ab->head_ ()->total_weight (); tmp->value = total >= tmp->weight; any_down = any_down || tmp->value; } else if (tmp->type == PEDESTAL) { tmp->value = 0; for (object *ab = tmp->above; ab; ab = ab->above) { object *head = ab->head_ (); /* Same note regarding move_type for buttons above apply here. */ if (((ab->move_type & tmp->move_on) || ab->move_type == 0) && (head->race == tmp->slaying || (head->type == SPECIAL_KEY && head->slaying == tmp->slaying) || (tmp->slaying == shstr_player && head->type == PLAYER))) tmp->value = 1; } any_down = any_down || tmp->value; } } if (any_down) /* If any other buttons were down, force this to remain down */ op->value = 1; //LOG(llevDebug, "update_button: %s (%d, %d=%d)\n", &op->name, op->count, op->value, old_value); /* If this button hasn't changed, don't do anything */ if (op->value != old_value) { SET_ANIMATION (op, op->value); update_object (op, UP_OBJ_FACE); push_button (op); /* Make all other buttons the same */ } } void use_trigger (object *op) { /* Toggle value */ op->value = !op->value; push_button (op); } /* * Note: animate_object should be used instead of this, * but it can't handle animations in the 8 directions */ void animate_turning (object *op) /* only one part objects */ { if (++op->state >= NUM_ANIMATIONS (op) / 8) op->state = 0; SET_ANIMATION (op, (op->stats.sp - 1) * NUM_ANIMATIONS (op) / 8 + op->state); update_object (op, UP_OBJ_FACE); } #define ARCH_SACRIFICE(xyz) ((xyz)->slaying) #define NROF_SACRIFICE(xyz) ((uint32)(xyz)->stats.food) /* Returns true if the sacrifice meets the needs of the altar. * * Function put in (0.92.1) so that identify altars won't grab money * unnecessarily - we can see if there is sufficient money, see if something * needs to be identified, and then remove money if needed. * * 0.93.4: Linked objects (ie, objects that are connected) can not be * sacrificed. This fixes a bug of trying to put multiple altars/related * objects on the same space that take the same sacrifice. */ int check_altar_sacrifice (const object *altar, const object *sacrifice) { if (!QUERY_FLAG (sacrifice, FLAG_ALIVE) && !QUERY_FLAG (sacrifice, FLAG_IS_LINKED) && sacrifice->type != PLAYER) { if (ARCH_SACRIFICE (altar) == shstr_money && sacrifice->type == MONEY && sacrifice->nrof * sacrifice->value >= NROF_SACRIFICE (altar)) return 1; if ((ARCH_SACRIFICE (altar) == sacrifice->arch->archname || ARCH_SACRIFICE (altar) == sacrifice->name || ARCH_SACRIFICE (altar) == sacrifice->slaying || strstr (query_base_name (sacrifice, 0), ARCH_SACRIFICE (altar))) && NROF_SACRIFICE (altar) <= sacrifice->number_of ()) return 1; } return 0; } /* * operate_altar checks if sacrifice was accepted and removes sacrificed * objects. If sacrifice was succeed return 1 else 0. Might be better to * call check_altar_sacrifice (above) than depend on the return value, * since operate_altar will remove the sacrifice also. * * If this function returns 1, '*sacrifice' is modified to point to the * remaining sacrifice, or is set to NULL if the sacrifice was used up. */ int operate_altar (object *altar, object **sacrifice) { if (!altar->map) { LOG (llevError, "BUG: operate_altar(): altar has no map\n"); return 0; } if (!altar->slaying || altar->value) return 0; if (!check_altar_sacrifice (altar, *sacrifice)) return 0; /* check_altar_sacrifice should have already verified that enough money * has been dropped. */ if (ARCH_SACRIFICE (altar) == shstr_money) { int number = NROF_SACRIFICE (altar) / (*sacrifice)->value; /* Round up any sacrifices. Altars don't make change either */ if (NROF_SACRIFICE (altar) % (*sacrifice)->value) number++; if (!(*sacrifice)->decrease (number)) *sacrifice = 0; } else if (!(*sacrifice)->decrease (NROF_SACRIFICE (altar))) *sacrifice = 0; if (altar->msg) new_info_map (NDI_BLACK, altar->map, altar->msg); return 1; } void trigger_move (object *op, int state) /* 1 down and 0 up */ { op->stats.wc = state; if (state) { use_trigger (op); op->set_speed (op->stats.exp > 0 ? 1. / op->stats.exp : 1.); op->speed_left = -1; } else { use_trigger (op); op->set_speed (0); } } /* * cause != NULL: something has moved on top of op * * cause == NULL: nothing has moved, we have been called from * animate_trigger(). * * TRIGGER_ALTAR: Returns 1 if 'cause' was destroyed, 0 if not. * * TRIGGER: Returns 1 if handle could be moved, 0 if not. * * TRIGGER_BUTTON, TRIGGER_PEDESTAL: Returns 0. */ int check_trigger (object *op, object *cause) { object *tmp; int push = 0, tot = 0; int in_movement = op->stats.wc || op->speed; switch (op->type) { case TRIGGER_BUTTON: if (op->weight > 0) { if (cause) { for (tmp = op->above; tmp; tmp = tmp->above) /* Comment reproduced from update_buttons(): */ /* Basically, if the move_type matches that on what the * button wants, we count it. The second check is so that * objects that don't move (swords, etc) will count. Note that * this means that more work is needed to make buttons * that are only triggered by flying objects. */ if ((tmp->move_type & op->move_on) || tmp->move_type == 0) tot += tmp->head_ ()->total_weight (); if (tot >= op->weight) push = 1; if (op->stats.ac == push) return 0; op->stats.ac = push; if (NUM_ANIMATIONS (op) > 1) { SET_ANIMATION (op, push); update_object (op, UP_OBJ_FACE); } if (in_movement || !push) return 0; } trigger_move (op, push); } return 0; case TRIGGER_PEDESTAL: if (cause) { for (tmp = op->above; tmp; tmp = tmp->above) { object *head = tmp->head_ (); /* See comment in TRIGGER_BUTTON about move_types */ if (((head->move_type & op->move_on) || head->move_type == 0) && (head->race == op->slaying || (!strcmp (op->slaying, "player") && head->type == PLAYER))) { push = 1; break; } } if (op->stats.ac == push) return 0; op->stats.ac = push; if (NUM_ANIMATIONS (op) > 1) { SET_ANIMATION (op, push); update_object (op, UP_OBJ_FACE); } update_object (op, UP_OBJ_FACE); if (in_movement || !push) return 0; } trigger_move (op, push); return 0; case TRIGGER_ALTAR: if (cause) { if (in_movement) return 0; if (operate_altar (op, &cause)) { if (NUM_ANIMATIONS (op) > 1) { SET_ANIMATION (op, 1); update_object (op, UP_OBJ_FACE); } if (op->last_sp >= 0) { trigger_move (op, 1); if (op->last_sp > 0) op->last_sp = -op->last_sp; } else { /* for trigger altar with last_sp, the ON/OFF * status (-> +/- value) is "simulated": */ op->value = !op->value; trigger_move (op, 1); op->last_sp = -op->last_sp; op->value = !op->value; } return cause == NULL; } else return 0; } else { if (NUM_ANIMATIONS (op) > 1) { SET_ANIMATION (op, 0); update_object (op, UP_OBJ_FACE); } /* If trigger_altar has "last_sp > 0" set on the map, * it will push the connected value only once per sacrifice. * Otherwise (default), the connected value will be * pushed twice: First by sacrifice, second by reset! -AV */ if (!op->last_sp) trigger_move (op, 0); else { op->stats.wc = 0; op->value = !op->value; op->set_speed (0); } } return 0; case TRIGGER: if (cause) { if (in_movement) return 0; push = 1; } if (NUM_ANIMATIONS (op) > 1) { SET_ANIMATION (op, push); update_object (op, UP_OBJ_FACE); } trigger_move (op, push); return 1; default: LOG (llevDebug, "Unknown trigger type: %s (%d)\n", &op->name, op->type); return 0; } } void add_button_link (object *button, maptile *map, int connected) { oblinkpt *obp; objectlink *ol = get_objectlink (); if (!map) { LOG (llevError, "Tried to add button-link without map.\n"); return; } button->path_attuned = connected; /* peterm: I need this so I can rebuild a connected map from a template map. */ SET_FLAG (button, FLAG_IS_LINKED); ol->ob = button; for (obp = map->buttons; obp && obp->value != connected; obp = obp->next) ; if (obp) { ol->next = obp->link; obp->link = ol; } else { obp = get_objectlinkpt (); obp->value = connected; obp->next = map->buttons; map->buttons = obp; obp->link = ol; } } /* * Remove the object from the linked lists of buttons in the map. * This is only needed by editors. */ void remove_button_link (object *op) { oblinkpt *obp; objectlink **olp, *ol; if (op->map == NULL) { LOG (llevError, "remove_button_link() in object without map.\n"); return; } if (!QUERY_FLAG (op, FLAG_IS_LINKED)) { LOG (llevError, "remove_button_linked() in unlinked object.\n"); return; } for (obp = op->map->buttons; obp; obp = obp->next) for (olp = &obp->link; (ol = *olp); olp = &ol->next) if (ol->ob == op) { /* LOG(llevDebug, "Removed link %d in button %s and map %s.\n", obp->value, op->name, op->map->path); */ *olp = ol->next; delete ol; return; } LOG (llevError, "remove_button_linked(): couldn't find object.\n"); CLEAR_FLAG (op, FLAG_IS_LINKED); } /* * Gets the objectlink for this connection from the map. */ oblinkpt * get_connection_links (maptile *map, long connection) { for (oblinkpt * obp = map->buttons; obp; obp = obp->next) if (obp->value == connection) return obp; return 0; } /* * Return the first objectlink in the objects linked to this one */ oblinkpt * get_button_links (const object *button) { oblinkpt *obp; objectlink *ol; if (!button->map) return NULL; for (obp = button->map->buttons; obp; obp = obp->next) for (ol = obp->link; ol; ol = ol->next) if (ol->ob == button) return obp; return NULL; } /* * Made as a separate function to increase efficiency */ int get_button_value (const object *button) { oblinkpt *obp; objectlink *ol; if (!button->map) return 0; for (obp = button->map->buttons; obp; obp = obp->next) for (ol = obp->link; ol; ol = ol->next) if (ol->ob == button) return obp->value; return 0; } /* This routine makes monsters who are * standing on the 'mood floor' change their * disposition if it is different. * If floor is to be triggered must have * a speed of zero (default is 1 for all * but the charm floor type). * by b.t. thomas@nomad.astro.psu.edu */ void do_mood_floor (object *op, object *source) { if (!source) source = op; mapspace &ms = op->ms (); if (!(ms.flags () & P_IS_ALIVE)) return; object *tmp; for (tmp = ms.top; tmp; tmp = tmp->below) if (QUERY_FLAG (tmp, FLAG_MONSTER)) break; /* doesn't effect players, and if there is a player on this space, won't also * be a monster here. */ //TODO: have players really FLAG_MONSTER? kept it for safety if (!tmp || tmp->type == PLAYER) return; switch (op->last_sp) { case 0: /* furious--make all monsters mad */ if (QUERY_FLAG (tmp, FLAG_UNAGGRESSIVE)) CLEAR_FLAG (tmp, FLAG_UNAGGRESSIVE); if (QUERY_FLAG (tmp, FLAG_FRIENDLY)) { tmp->attack_movement = 0; /* lots of checks here, but want to make sure we don't * dereference a null value */ if (tmp->type == GOLEM && tmp->owner && tmp->owner->type == PLAYER && tmp->owner->contr->golem == tmp) tmp->owner->contr->golem = 0; tmp->owner = 0; remove_friendly_object (tmp); } break; case 1: /* angry -- get neutral monsters mad */ if (QUERY_FLAG (tmp, FLAG_UNAGGRESSIVE) && !QUERY_FLAG (tmp, FLAG_FRIENDLY)) CLEAR_FLAG (tmp, FLAG_UNAGGRESSIVE); break; case 2: /* calm -- pacify unfriendly monsters */ SET_FLAG (tmp, FLAG_UNAGGRESSIVE); break; case 3: /* make all monsters fall asleep */ SET_FLAG (tmp, FLAG_SLEEP); break; case 4: /* charm all monsters */ if (op == source) break; /* only if 'connected' */ if (object *pl = source->ms ().player ()) { tmp->set_owner (pl); SET_FLAG (tmp, FLAG_MONSTER); tmp->stats.exp = 0; add_friendly_object (tmp); tmp->attack_movement = PETMOVE; } break; case 6: // kill monsters if (!QUERY_FLAG (tmp, FLAG_FRIENDLY)) break; // FALL THROUGH case 5: // kill all alives if (!tmp->flag [FLAG_PRECIOUS]) { archetype::get (shstr_burnout)->insert_at (tmp, source); tmp->destroy (true); } break; default: break; } } /* this function returns the object it matches, or NULL if non. * It will descend through containers to find the object. * slaying = match object slaying flag * race = match object archetype name flag * hp = match object type (excpt type '0'== PLAYER) */ object * check_inv_recursive (object *op, const object *trig) { object *tmp, *ret = NULL; /* First check the object itself. */ if ((trig->stats.hp && (op->type == trig->stats.hp)) || (trig->slaying && (op->slaying == trig->slaying)) || (trig->race && (op->arch->archname == trig->race))) return op; for (tmp = op->inv; tmp; tmp = tmp->below) { if (tmp->inv) { ret = check_inv_recursive (tmp, trig); if (ret) return ret; } else if ((trig->stats.hp && (tmp->type == trig->stats.hp)) || (trig->slaying && (tmp->slaying == trig->slaying)) || (trig->race && (tmp->arch->archname == trig->race))) return tmp; } return NULL; } /* check_inv(), a function to search the inventory, * of a player and then based on a set of conditions, * the square will activate connected items. * Monsters can't trigger this square (for now) * Values are: last_sp = 1/0 obj/no obj triggers * last_heal = 1/0 remove/dont remove obj if triggered * -b.t. (thomas@nomad.astro.psu.edu * * Tue Dec 19 15:34:00 CET 2006 elmex: changed the function to ignore op * because the check-inventory semantic essentially only applies when * something is above the inventory checker. * The semantic prior this change was: trigger if something has moved on or off * and has a matching item. Imagine what happens if someone steps on the inventory * checker with a matching item, has it, activates the connection, throws the item * away, and then leaves the inventory checker. That would've caused an always-enabled * state in the inventory checker. This won't happen anymore now. * * Wed Jan 10 11:34:26 CET 2007 elmex: fixed this function, we now check * whether op is on this mapspace or not, because the value (1|0) depends * on this information. also make sure to only push_button if op has * a matching item (because when we do a push_button with value=0 timed gates * will still open)! (i hope i got the semantics right this time) * */ void check_inv (object *op, object *trig) { trig->value = 0; // deactivate if none of the following conditions apply object *pl = trig->ms ().player (); object *match = check_inv_recursive (op, trig); // elmex: a note about (pl == op): // if pl == 0 then the player has left this space // if pl != 0 then a player is on this mapspace, but then // we still have to check whether it's the player that triggered // this inv-checker, because if not, then the op left this inv-checker // and we have to set the value to 0 if (match && trig->last_sp) // match == having { if (trig->last_heal) match->decrease (); trig->value = (pl == op ? 1 : 0); // 1 if matching player entered, and 0 if he left push_button (trig); } else if (!match && !trig->last_sp) // match == not having { trig->value = (pl == op ? 1 : 0); // 1 if matching player entered, and 0 if he left push_button (trig); } }