| 1 |
/* |
| 2 |
* This file is part of Deliantra, the Roguelike Realtime MMORPG. |
| 3 |
* |
| 4 |
* Copyright (©) 2017,2018 Marc Alexander Lehmann / the Deliantra team |
| 5 |
* Copyright (©) 2005,2006,2007,2008,2009,2010,2011,2012,2013,2014,2015,2016 Marc Alexander Lehmann / Robin Redeker / the Deliantra team |
| 6 |
* Copyright (©) 2002 Mark Wedel & Crossfire Development Team |
| 7 |
* Copyright (©) 1992 Frank Tore Johansen |
| 8 |
* |
| 9 |
* Deliantra is free software: you can redistribute it and/or modify it under |
| 10 |
* the terms of the Affero GNU General Public License as published by the |
| 11 |
* Free Software Foundation, either version 3 of the License, or (at your |
| 12 |
* option) any later version. |
| 13 |
* |
| 14 |
* This program is distributed in the hope that it will be useful, |
| 15 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 17 |
* GNU General Public License for more details. |
| 18 |
* |
| 19 |
* You should have received a copy of the Affero GNU General Public License |
| 20 |
* and the GNU General Public License along with this program. If not, see |
| 21 |
* <http://www.gnu.org/licenses/>. |
| 22 |
* |
| 23 |
* The authors can be reached via e-mail to <support@deliantra.net> |
| 24 |
*/ |
| 25 |
|
| 26 |
#include <global.h> |
| 27 |
|
| 28 |
/* |
| 29 |
* This code is no longer highly inefficient 8) |
| 30 |
*/ |
| 31 |
|
| 32 |
/* |
| 33 |
* elmex: |
| 34 |
* This function takes a objectlink list with all the objects are going to be activated. |
| 35 |
* state is a true/false flag that will actiavte objects that have FLAG_ACTIVATE_ON_PUSH/RELEASE set. |
| 36 |
* The activator argument can be 0 or the source object for this activation. |
| 37 |
* the originator is the player or monster who did something. |
| 38 |
*/ |
| 39 |
static void |
| 40 |
activate_connection_link (objectlink *ol, int state, object *activator, object *originator) |
| 41 |
{ |
| 42 |
for (; ol; ol = ol->next) |
| 43 |
{ |
| 44 |
if (!ol->ob) |
| 45 |
{ |
| 46 |
LOG (llevError, "Internal error in activate_connection_link.\n"); |
| 47 |
continue; |
| 48 |
} |
| 49 |
|
| 50 |
/* a button link object can become freed when the map is saving. As |
| 51 |
* a map is saved, objects are removed and freed, and if an object is |
| 52 |
* on top of a button, this function is eventually called. If a map |
| 53 |
* is getting moved out of memory, the status of buttons and levers |
| 54 |
* probably isn't important - it will get sorted out when the map is |
| 55 |
* re-loaded. As such, just exit this function if that is the case. |
| 56 |
*/ |
| 57 |
|
| 58 |
if (ol->ob->flag [FLAG_FREED]) |
| 59 |
return; |
| 60 |
|
| 61 |
object *tmp = ol->ob; |
| 62 |
|
| 63 |
/* if the criteria isn't appropriate, don't do anything */ |
| 64 |
if (state && !tmp->flag [FLAG_ACTIVATE_ON_PUSH]) |
| 65 |
continue; |
| 66 |
|
| 67 |
if (!state && !tmp->flag [FLAG_ACTIVATE_ON_RELEASE]) |
| 68 |
continue; |
| 69 |
|
| 70 |
switch (tmp->type) |
| 71 |
{ |
| 72 |
case GATE: |
| 73 |
case HOLE: |
| 74 |
if (!tmp->active) |
| 75 |
tmp->play_sound (tmp->sound |
| 76 |
? tmp->sound |
| 77 |
: sound_find (tmp->type == GATE ? "trigger_gate" : "trigger_hole")); |
| 78 |
tmp->value = tmp->stats.maxsp ? !state : state; |
| 79 |
tmp->set_speed (0.5); |
| 80 |
break; |
| 81 |
|
| 82 |
case T_HANDLE: |
| 83 |
tmp->value = tmp->stats.maxsp ? !state : state; |
| 84 |
tmp->update_anim_frame (tmp->value); |
| 85 |
break; |
| 86 |
|
| 87 |
case SIGN: |
| 88 |
if (!tmp->stats.food || tmp->last_eat < tmp->stats.food) |
| 89 |
{ |
| 90 |
tmp->play_sound (tmp->sound ? tmp->sound : sound_find ("msg_voice")); |
| 91 |
|
| 92 |
if (originator && originator->contr) |
| 93 |
originator->contr->infobox (MSG_CHANNEL ("examine"), format ("T<%s>\n\n%s", &tmp->name, &tmp->msg)); |
| 94 |
|
| 95 |
new_info_map_except (NDI_UNIQUE | NDI_NAVY, tmp->map, originator, tmp->msg); |
| 96 |
|
| 97 |
if (tmp->stats.food) |
| 98 |
tmp->last_eat++; |
| 99 |
} |
| 100 |
break; |
| 101 |
|
| 102 |
case ALTAR: |
| 103 |
tmp->play_sound (tmp->sound ? tmp->sound : sound_find ("trigger_altar")); |
| 104 |
tmp->value = 1; |
| 105 |
tmp->update_anim_frame (tmp->value); |
| 106 |
break; |
| 107 |
|
| 108 |
case BUTTON: |
| 109 |
case PEDESTAL: |
| 110 |
tmp->play_sound (tmp->sound ? tmp->sound : sound_find ("trigger_button")); |
| 111 |
tmp->value = state; |
| 112 |
tmp->update_anim_frame (tmp->value); |
| 113 |
break; |
| 114 |
|
| 115 |
case MOOD_FLOOR: |
| 116 |
do_mood_floor (tmp, activator); |
| 117 |
break; |
| 118 |
|
| 119 |
case TIMED_GATE: |
| 120 |
if (!tmp->active) |
| 121 |
tmp->play_sound (tmp->sound ? tmp->sound : sound_find ("trigger_gate")); |
| 122 |
|
| 123 |
tmp->set_speed (tmp->arch->speed); |
| 124 |
tmp->value = tmp->arch->value; |
| 125 |
tmp->stats.sp = 1; |
| 126 |
tmp->stats.hp = tmp->stats.maxhp; |
| 127 |
/* Handle multipart gates. We copy the value for the other parts |
| 128 |
* from the head - this ensures that the data will consistent |
| 129 |
*/ |
| 130 |
for (object *part = tmp->more; part; part = part->more) |
| 131 |
{ |
| 132 |
part->value = tmp->value; |
| 133 |
part->stats.sp = tmp->stats.sp; |
| 134 |
part->stats.hp = tmp->stats.hp; |
| 135 |
part->set_speed (tmp->speed); |
| 136 |
} |
| 137 |
break; |
| 138 |
|
| 139 |
case DIRECTOR: |
| 140 |
case FIREWALL: |
| 141 |
if (!tmp->flag [FLAG_ANIMATE] && tmp->type == FIREWALL) |
| 142 |
move_firewall (tmp); |
| 143 |
else |
| 144 |
{ |
| 145 |
tmp->stats.sp = absdir (tmp->stats.sp + tmp->stats.maxsp); /* next direction */ |
| 146 |
animate_turning (tmp); |
| 147 |
} |
| 148 |
break; |
| 149 |
|
| 150 |
case TELEPORTER: |
| 151 |
move_teleporter (tmp); |
| 152 |
break; |
| 153 |
|
| 154 |
case CREATOR: |
| 155 |
move_creator (tmp); |
| 156 |
break; |
| 157 |
|
| 158 |
case TRIGGER_MARKER: |
| 159 |
//tmp->play_sound (tmp->sound ? tmp->sound : sound_find ("trigger_marker")); |
| 160 |
move_marker (tmp); |
| 161 |
break; |
| 162 |
|
| 163 |
case DUPLICATOR: |
| 164 |
move_duplicator (tmp); |
| 165 |
break; |
| 166 |
|
| 167 |
case MAPSCRIPT: |
| 168 |
cfperl_mapscript_activate (tmp, state, activator, originator); |
| 169 |
break; |
| 170 |
} |
| 171 |
} |
| 172 |
} |
| 173 |
|
| 174 |
/* |
| 175 |
* elmex: |
| 176 |
* This is the new push_button function, it got split up so that |
| 177 |
* you can activate connections without a button now! |
| 178 |
* old but still valid comment: |
| 179 |
* |
| 180 |
* Push the specified object. This can affect other buttons/gates/handles |
| 181 |
* altars/pedestals/holes in the whole map. |
| 182 |
* Changed the routine to loop through _all_ objects. |
| 183 |
* Better hurry with that linked list... |
| 184 |
* |
| 185 |
*/ |
| 186 |
void |
| 187 |
push_button (object *op, object *originator) |
| 188 |
{ |
| 189 |
if (oblinkpt *obp = op->find_link ()) |
| 190 |
{ |
| 191 |
if (INVOKE_MAP (TRIGGER, op->map, ARG_STRING (&obp->id), ARG_INT (op->value), ARG_OBJECT (op), ARG_OBJECT (originator))) |
| 192 |
return; |
| 193 |
|
| 194 |
activate_connection_link (obp->link, op->value, op, originator); |
| 195 |
} |
| 196 |
} |
| 197 |
|
| 198 |
/* |
| 199 |
* elmex: |
| 200 |
* This activates a connection, similar to push_button (object *op) but it takes |
| 201 |
* only a map, a connection value and a true or false flag that indicated whether |
| 202 |
* the connection was 'state' or 'released'. So that you can activate objects |
| 203 |
* who have FLAG_ACTIVATE_ON_PUSH/RELEASE set properly. |
| 204 |
* |
| 205 |
*/ |
| 206 |
void |
| 207 |
maptile::trigger (shstr_tmp id, int state, object *activator, object *originator) |
| 208 |
{ |
| 209 |
if (INVOKE_MAP (TRIGGER, this, ARG_STRING (&id), ARG_INT (state), ARG_OBJECT (originator))) |
| 210 |
return; |
| 211 |
|
| 212 |
if (oblinkpt *obp = find_link (id)) |
| 213 |
activate_connection_link (obp->link, state, activator, originator); |
| 214 |
} |
| 215 |
|
| 216 |
/* |
| 217 |
* Updates everything connected with the button op. |
| 218 |
* After changing the state of a button, this function must be called |
| 219 |
* to make sure that all gates and other buttons connected to the |
| 220 |
* button reacts to the (eventual) change of state. |
| 221 |
*/ |
| 222 |
void |
| 223 |
update_button (object *op, object *originator) |
| 224 |
{ |
| 225 |
int any_down = 0, old_value = op->value; |
| 226 |
|
| 227 |
if (oblinkpt *obp = op->find_link ()) |
| 228 |
for (objectlink *ol = obp->link; ol; ol = ol->next) |
| 229 |
{ |
| 230 |
if (!ol->ob) |
| 231 |
{ |
| 232 |
LOG (llevDebug, "Internal error in update_button (%s).\n", &op->name); |
| 233 |
continue; |
| 234 |
} |
| 235 |
|
| 236 |
object *tmp = ol->ob; |
| 237 |
|
| 238 |
if (tmp->type == BUTTON) |
| 239 |
{ |
| 240 |
weight_t total = 0; |
| 241 |
|
| 242 |
for (object *ab = tmp->above; ab; ab = ab->above) |
| 243 |
/* Basically, if the move_type matches that on what the |
| 244 |
* button wants, we count it. The second check is so that |
| 245 |
* objects who don't move (swords, etc) will count. Note that |
| 246 |
* this means that more work is needed to make buttons |
| 247 |
* that are only triggered by flying objects. |
| 248 |
*/ |
| 249 |
if ((ab->move_type & tmp->move_on) || ab->move_type == 0) |
| 250 |
total += ab->head_ ()->total_weight (); |
| 251 |
|
| 252 |
tmp->value = total >= tmp->weight; |
| 253 |
|
| 254 |
any_down = any_down || tmp->value; |
| 255 |
} |
| 256 |
else if (tmp->type == PEDESTAL) |
| 257 |
{ |
| 258 |
bool is_match = is_match_expr (tmp->slaying); |
| 259 |
tmp->value = 0; |
| 260 |
|
| 261 |
for (object *ab = tmp->above; ab; ab = ab->above) |
| 262 |
{ |
| 263 |
object *head = ab->head_ (); |
| 264 |
|
| 265 |
/* Same note regarding move_type for buttons above apply here. */ |
| 266 |
if (((ab->move_type & tmp->move_on) || ab->move_type == 0)) |
| 267 |
if (is_match |
| 268 |
? match (tmp->slaying, head, tmp, originator) |
| 269 |
: (head->race == tmp->slaying |
| 270 |
|| (head->type == SPECIAL_KEY && head->slaying == tmp->slaying) |
| 271 |
|| (tmp->slaying == shstr_player && head->type == PLAYER))) |
| 272 |
{ |
| 273 |
tmp->value = 1; |
| 274 |
break; |
| 275 |
} |
| 276 |
} |
| 277 |
|
| 278 |
any_down = any_down || tmp->value; |
| 279 |
} |
| 280 |
else if (tmp->type == T_MATCH) |
| 281 |
{ |
| 282 |
tmp->value = 0; |
| 283 |
|
| 284 |
for (object *ab = tmp->above; ab; ab = ab->above) |
| 285 |
{ |
| 286 |
object *head = ab->head_ (); |
| 287 |
|
| 288 |
/* Same note regarding move_type for buttons above apply here. */ |
| 289 |
if (((ab->move_type & tmp->move_on) || ab->move_type == 0)) |
| 290 |
if (match (tmp->slaying, head, tmp, originator)) |
| 291 |
{ |
| 292 |
tmp->value = 1; |
| 293 |
break; |
| 294 |
} |
| 295 |
} |
| 296 |
|
| 297 |
any_down = any_down || tmp->value; |
| 298 |
} |
| 299 |
} |
| 300 |
|
| 301 |
if (any_down) /* If any other buttons were down, force this to remain down */ |
| 302 |
op->value = 1; |
| 303 |
|
| 304 |
//LOG(llevDebug, "update_button: %s (%d, %d=%d)\n", &op->name, op->count, op->value, old_value); |
| 305 |
|
| 306 |
/* If this button hasn't changed, don't do anything */ |
| 307 |
if (op->value != old_value) |
| 308 |
{ |
| 309 |
op->update_anim_frame (op->value); |
| 310 |
push_button (op, originator); /* Make all other buttons the same */ |
| 311 |
} |
| 312 |
} |
| 313 |
|
| 314 |
void |
| 315 |
use_trigger (object *op, object *originator) |
| 316 |
{ |
| 317 |
/* Toggle value */ |
| 318 |
op->value = !op->value; |
| 319 |
|
| 320 |
push_button (op, originator); |
| 321 |
} |
| 322 |
|
| 323 |
/* |
| 324 |
* Note: animate_object should be used instead of this, |
| 325 |
* but it can't handle animations in the 8 directions |
| 326 |
*/ |
| 327 |
void |
| 328 |
animate_turning (object *op) /* only one part objects */ |
| 329 |
{ |
| 330 |
if (++op->state >= op->anim_frames () / 8) |
| 331 |
op->state = 0; |
| 332 |
|
| 333 |
op->update_anim_frame ((op->stats.sp - 1) * op->anim_frames () / 8 + op->state); |
| 334 |
} |
| 335 |
|
| 336 |
#define ARCH_SACRIFICE(xyz) ((xyz)->slaying) |
| 337 |
#define NROF_SACRIFICE(xyz) ((uint32)(xyz)->stats.food) |
| 338 |
|
| 339 |
/* Returns true if the sacrifice meets the needs of the altar. |
| 340 |
* |
| 341 |
* Function put in (0.92.1) so that identify altars won't grab money |
| 342 |
* unnecessarily - we can see if there is sufficient money, see if something |
| 343 |
* needs to be identified, and then remove money if needed. |
| 344 |
* |
| 345 |
* 0.93.4: Linked objects (ie, objects that are connected) can not be |
| 346 |
* sacrificed. This fixes a bug of trying to put multiple altars/related |
| 347 |
* objects on the same space that take the same sacrifice. |
| 348 |
*/ |
| 349 |
int |
| 350 |
check_altar_sacrifice (object *altar, object *sacrifice, object *originator) |
| 351 |
{ |
| 352 |
if (sacrifice->flag [FLAG_UNPAID] |
| 353 |
|| sacrifice->flag [FLAG_IS_LINKED] |
| 354 |
|| sacrifice->is_player ()) |
| 355 |
return 0; |
| 356 |
|
| 357 |
if (is_match_expr (ARCH_SACRIFICE (altar))) |
| 358 |
return match (ARCH_SACRIFICE (altar), sacrifice, altar, originator); |
| 359 |
|
| 360 |
if (!sacrifice->flag [FLAG_ALIVE]) |
| 361 |
{ |
| 362 |
if (ARCH_SACRIFICE (altar) == shstr_money |
| 363 |
&& sacrifice->type == MONEY |
| 364 |
&& sacrifice->nrof * sacrifice->value >= NROF_SACRIFICE (altar)) |
| 365 |
return 1; |
| 366 |
|
| 367 |
if ((ARCH_SACRIFICE (altar) == sacrifice->arch->archname |
| 368 |
|| ARCH_SACRIFICE (altar) == sacrifice->name |
| 369 |
|| ARCH_SACRIFICE (altar) == sacrifice->slaying |
| 370 |
|| strstr (query_base_name (sacrifice, 0), ARCH_SACRIFICE (altar))) |
| 371 |
&& NROF_SACRIFICE (altar) <= sacrifice->number_of ()) |
| 372 |
return 1; |
| 373 |
} |
| 374 |
|
| 375 |
return 0; |
| 376 |
} |
| 377 |
|
| 378 |
/* |
| 379 |
* operate_altar checks if sacrifice was accepted and removes sacrificed |
| 380 |
* objects. If sacrifice was succeed return 1 else 0. Might be better to |
| 381 |
* call check_altar_sacrifice (above) than depend on the return value, |
| 382 |
* since operate_altar will remove the sacrifice also. |
| 383 |
* |
| 384 |
* If this function returns 1, '*sacrifice' is modified to point to the |
| 385 |
* remaining sacrifice, or is set to NULL if the sacrifice was used up. |
| 386 |
*/ |
| 387 |
int |
| 388 |
operate_altar (object *altar, object **sacrifice, object *originator) |
| 389 |
{ |
| 390 |
if (!altar->map) |
| 391 |
{ |
| 392 |
LOG (llevError, "BUG: operate_altar(): altar has no map\n"); |
| 393 |
return 0; |
| 394 |
} |
| 395 |
|
| 396 |
if (!altar->slaying || altar->value) |
| 397 |
return 0; |
| 398 |
|
| 399 |
if (!check_altar_sacrifice (altar, *sacrifice, originator)) |
| 400 |
return 0; |
| 401 |
|
| 402 |
/* check_altar_sacrifice should have already verified that enough money |
| 403 |
* has been dropped. |
| 404 |
*/ |
| 405 |
if (ARCH_SACRIFICE (altar) == shstr_money) |
| 406 |
{ |
| 407 |
int number = NROF_SACRIFICE (altar) / (*sacrifice)->value; |
| 408 |
|
| 409 |
/* Round up any sacrifices. Altars don't make change either */ |
| 410 |
if (NROF_SACRIFICE (altar) % (*sacrifice)->value) |
| 411 |
number++; |
| 412 |
|
| 413 |
if (!(*sacrifice)->decrease (number)) |
| 414 |
*sacrifice = 0; |
| 415 |
} |
| 416 |
else |
| 417 |
if (!(*sacrifice)->decrease (NROF_SACRIFICE (altar))) |
| 418 |
*sacrifice = 0; |
| 419 |
|
| 420 |
if (altar->msg) |
| 421 |
new_info_map (NDI_BLACK, altar->map, altar->msg); |
| 422 |
|
| 423 |
return 1; |
| 424 |
} |
| 425 |
|
| 426 |
static void |
| 427 |
trigger_move (object *op, int state, object *originator) /* 1 down and 0 up */ |
| 428 |
{ |
| 429 |
op->stats.wc = state; |
| 430 |
|
| 431 |
if (state) |
| 432 |
{ |
| 433 |
use_trigger (op, originator); |
| 434 |
op->set_speed (op->stats.exp > 0 ? 1. / op->stats.exp : 1.); |
| 435 |
op->speed_left = -1; |
| 436 |
} |
| 437 |
else |
| 438 |
{ |
| 439 |
use_trigger (op, originator); |
| 440 |
op->set_speed (0); |
| 441 |
} |
| 442 |
} |
| 443 |
|
| 444 |
|
| 445 |
/* |
| 446 |
* cause != NULL: something has moved on top of op |
| 447 |
* |
| 448 |
* cause == NULL: nothing has moved, we have been called from |
| 449 |
* animate_trigger(). |
| 450 |
* |
| 451 |
* TRIGGER_ALTAR: Returns 1 if 'cause' was destroyed, 0 if not. |
| 452 |
* |
| 453 |
* TRIGGER: Returns 1 if handle could be moved, 0 if not. |
| 454 |
* |
| 455 |
* TRIGGER_BUTTON, TRIGGER_PEDESTAL: Returns 0. |
| 456 |
*/ |
| 457 |
int |
| 458 |
check_trigger (object *op, object *cause, object *originator) |
| 459 |
{ |
| 460 |
object *tmp; |
| 461 |
int push = 0, tot = 0; |
| 462 |
int in_movement = op->stats.wc || op->has_active_speed (); |
| 463 |
|
| 464 |
switch (op->type) |
| 465 |
{ |
| 466 |
case TRIGGER_BUTTON: |
| 467 |
if (op->weight > 0) |
| 468 |
{ |
| 469 |
if (cause) |
| 470 |
{ |
| 471 |
for (tmp = op->above; tmp; tmp = tmp->above) |
| 472 |
/* Comment reproduced from update_buttons(): */ |
| 473 |
/* Basically, if the move_type matches that on what the |
| 474 |
* button wants, we count it. The second check is so that |
| 475 |
* objects that don't move (swords, etc) will count. Note that |
| 476 |
* this means that more work is needed to make buttons |
| 477 |
* that are only triggered by flying objects. |
| 478 |
*/ |
| 479 |
if ((tmp->move_type & op->move_on) || tmp->move_type == 0) |
| 480 |
tot += tmp->head_ ()->total_weight (); |
| 481 |
|
| 482 |
if (tot >= op->weight) |
| 483 |
push = 1; |
| 484 |
|
| 485 |
if (op->stats.ac == push) |
| 486 |
return 0; |
| 487 |
|
| 488 |
op->stats.ac = push; |
| 489 |
op->update_anim_frame (push); |
| 490 |
|
| 491 |
if (in_movement || !push) |
| 492 |
return 0; |
| 493 |
} |
| 494 |
|
| 495 |
trigger_move (op, push, cause); |
| 496 |
} |
| 497 |
|
| 498 |
return 0; |
| 499 |
|
| 500 |
case TRIGGER_PEDESTAL: |
| 501 |
if (cause) |
| 502 |
{ |
| 503 |
for (tmp = op->above; tmp; tmp = tmp->above) |
| 504 |
{ |
| 505 |
object *head = tmp->head_ (); |
| 506 |
|
| 507 |
/* See comment in TRIGGER_BUTTON about move_types */ |
| 508 |
if (((head->move_type & op->move_on) || head->move_type == 0) |
| 509 |
&& (head->race == op->slaying || (op->slaying == shstr_player && head->type == PLAYER))) |
| 510 |
{ |
| 511 |
push = 1; |
| 512 |
break; |
| 513 |
} |
| 514 |
} |
| 515 |
|
| 516 |
if (op->stats.ac == push) |
| 517 |
return 0; |
| 518 |
|
| 519 |
op->stats.ac = push; |
| 520 |
op->update_anim_frame (push); |
| 521 |
|
| 522 |
if (in_movement || !push) |
| 523 |
return 0; |
| 524 |
} |
| 525 |
|
| 526 |
trigger_move (op, push, cause); |
| 527 |
return 0; |
| 528 |
|
| 529 |
case TRIGGER_ALTAR: |
| 530 |
if (cause) |
| 531 |
{ |
| 532 |
if (in_movement) |
| 533 |
return 0; |
| 534 |
|
| 535 |
if (operate_altar (op, &cause)) /* TODO: originator? */ |
| 536 |
{ |
| 537 |
op->update_anim_frame (1); |
| 538 |
|
| 539 |
if (op->last_sp >= 0) |
| 540 |
{ |
| 541 |
trigger_move (op, 1, cause); |
| 542 |
|
| 543 |
if (op->last_sp > 0) |
| 544 |
op->last_sp = -op->last_sp; |
| 545 |
} |
| 546 |
else |
| 547 |
{ |
| 548 |
/* for trigger altar with last_sp, the ON/OFF |
| 549 |
* status (-> +/- value) is "simulated": |
| 550 |
*/ |
| 551 |
op->value = !op->value; |
| 552 |
trigger_move (op, 1, cause); |
| 553 |
op->last_sp = -op->last_sp; |
| 554 |
op->value = !op->value; |
| 555 |
} |
| 556 |
|
| 557 |
return cause == NULL; |
| 558 |
} |
| 559 |
else |
| 560 |
return 0; |
| 561 |
} |
| 562 |
else |
| 563 |
{ |
| 564 |
op->update_anim_frame (0); |
| 565 |
|
| 566 |
/* If trigger_altar has "last_sp > 0" set on the map, |
| 567 |
* it will push the connected value only once per sacrifice. |
| 568 |
* Otherwise (default), the connected value will be |
| 569 |
* pushed twice: First by sacrifice, second by reset! -AV |
| 570 |
*/ |
| 571 |
if (!op->last_sp) |
| 572 |
trigger_move (op, 0, cause); |
| 573 |
else |
| 574 |
{ |
| 575 |
op->stats.wc = 0; |
| 576 |
op->value = !op->value; |
| 577 |
op->set_speed (0); |
| 578 |
} |
| 579 |
} |
| 580 |
return 0; |
| 581 |
|
| 582 |
case TRIGGER: |
| 583 |
if (cause) |
| 584 |
{ |
| 585 |
if (in_movement) |
| 586 |
return 0; |
| 587 |
|
| 588 |
push = 1; |
| 589 |
} |
| 590 |
|
| 591 |
op->update_anim_frame (push); |
| 592 |
|
| 593 |
trigger_move (op, push, cause); |
| 594 |
return 1; |
| 595 |
|
| 596 |
default: |
| 597 |
LOG (llevDebug, "Unknown trigger type: %s (%d)\n", &op->name, op->type); |
| 598 |
return 0; |
| 599 |
} |
| 600 |
} |
| 601 |
|
| 602 |
void |
| 603 |
object::add_link (maptile *map, shstr_tmp id) |
| 604 |
{ |
| 605 |
if (!map) |
| 606 |
{ |
| 607 |
LOG (llevError, "Tried to add button-link without map.\n"); |
| 608 |
return; |
| 609 |
} |
| 610 |
|
| 611 |
flag [FLAG_IS_LINKED] = true; |
| 612 |
|
| 613 |
objectlink *ol = get_objectlink (); |
| 614 |
ol->ob = this; |
| 615 |
|
| 616 |
for (oblinkpt *obp = map->buttons; obp; obp = obp->next) |
| 617 |
if (obp->id == id) |
| 618 |
{ |
| 619 |
ol->next = obp->link; |
| 620 |
obp->link = ol; |
| 621 |
return; |
| 622 |
} |
| 623 |
|
| 624 |
oblinkpt *obp = get_objectlinkpt (); |
| 625 |
obp->id = id; |
| 626 |
|
| 627 |
obp->next = map->buttons; |
| 628 |
map->buttons = obp; |
| 629 |
obp->link = ol; |
| 630 |
} |
| 631 |
|
| 632 |
/* |
| 633 |
* Remove the object from the linked lists of buttons in the map. |
| 634 |
* This is only needed by editors. |
| 635 |
*/ |
| 636 |
void |
| 637 |
object::remove_link () |
| 638 |
{ |
| 639 |
if (!map) |
| 640 |
{ |
| 641 |
LOG (llevError, "remove_button_link() in object without map.\n"); |
| 642 |
return; |
| 643 |
} |
| 644 |
|
| 645 |
if (!flag [FLAG_IS_LINKED]) |
| 646 |
{ |
| 647 |
LOG (llevError, "remove_button_linked() in unlinked object.\n"); |
| 648 |
return; |
| 649 |
} |
| 650 |
|
| 651 |
flag [FLAG_IS_LINKED] = false; |
| 652 |
|
| 653 |
for (oblinkpt *obp = map->buttons; obp; obp = obp->next) |
| 654 |
for (objectlink **olp = &obp->link; *olp; olp = &(*olp)->next) |
| 655 |
if ((*olp)->ob == this) |
| 656 |
{ |
| 657 |
objectlink *ol = *olp; |
| 658 |
*olp = ol->next; |
| 659 |
delete ol; |
| 660 |
return; |
| 661 |
} |
| 662 |
|
| 663 |
LOG (llevError, "remove_button_linked(): couldn't find object.\n"); |
| 664 |
} |
| 665 |
|
| 666 |
/* |
| 667 |
* Updates every button on the map (by calling update_button() for them). |
| 668 |
*/ |
| 669 |
void |
| 670 |
maptile::update_buttons () |
| 671 |
{ |
| 672 |
for (oblinkpt *obp = buttons; obp; obp = obp->next) |
| 673 |
for (objectlink *ol = obp->link; ol; ol = ol->next) |
| 674 |
{ |
| 675 |
if (!ol->ob) |
| 676 |
{ |
| 677 |
LOG (llevError, "Internal error in update_button (%s (%dx%d), connected %ld).\n", |
| 678 |
ol->ob ? (const char *) ol->ob->name : "null", ol->ob ? ol->ob->x : -1, ol->ob ? ol->ob->y : -1, &obp->id); |
| 679 |
continue; |
| 680 |
} |
| 681 |
|
| 682 |
if (ol->ob->type == BUTTON || ol->ob->type == PEDESTAL) |
| 683 |
{ |
| 684 |
update_button (ol->ob, 0); |
| 685 |
break; |
| 686 |
} |
| 687 |
} |
| 688 |
} |
| 689 |
|
| 690 |
/* |
| 691 |
* Gets the objectlink for this connection from the map. |
| 692 |
*/ |
| 693 |
oblinkpt * |
| 694 |
maptile::find_link (shstr_tmp id) |
| 695 |
{ |
| 696 |
for (oblinkpt *obp = buttons; obp; obp = obp->next) |
| 697 |
if (obp->id == id) |
| 698 |
return obp; |
| 699 |
|
| 700 |
return 0; |
| 701 |
} |
| 702 |
|
| 703 |
/* |
| 704 |
* Return the first objectlink in the objects linked to this one |
| 705 |
*/ |
| 706 |
oblinkpt * |
| 707 |
object::find_link () const |
| 708 |
{ |
| 709 |
if (map) |
| 710 |
for (oblinkpt *obp = map->buttons; obp; obp = obp->next) |
| 711 |
for (objectlink *ol = obp->link; ol; ol = ol->next) |
| 712 |
if (ol->ob == this) |
| 713 |
return obp; |
| 714 |
|
| 715 |
return 0; |
| 716 |
} |
| 717 |
|
| 718 |
/* This routine makes monsters who are |
| 719 |
* standing on the 'mood floor' change their |
| 720 |
* disposition if it is different. |
| 721 |
* If floor is to be triggered must have |
| 722 |
* a speed of zero (default is 1 for all |
| 723 |
* but the charm floor type). |
| 724 |
* by b.t. thomas@nomad.astro.psu.edu |
| 725 |
*/ |
| 726 |
void |
| 727 |
do_mood_floor (object *op, object *source) |
| 728 |
{ |
| 729 |
if (!source) |
| 730 |
source = op; |
| 731 |
|
| 732 |
mapspace &ms = op->ms (); |
| 733 |
|
| 734 |
if (!(ms.flags () & P_IS_ALIVE)) |
| 735 |
return; |
| 736 |
|
| 737 |
object *tmp; |
| 738 |
|
| 739 |
for (tmp = ms.top; tmp; tmp = tmp->below) |
| 740 |
if (tmp->flag [FLAG_MONSTER]) |
| 741 |
break; |
| 742 |
|
| 743 |
/* doesn't effect players, and if there is a player on this space, won't also |
| 744 |
* be a monster here. |
| 745 |
*/ |
| 746 |
//TODO: have players really FLAG_MONSTER? kept it for safety |
| 747 |
if (!tmp || tmp->type == PLAYER) |
| 748 |
return; |
| 749 |
|
| 750 |
switch (op->last_sp) |
| 751 |
{ |
| 752 |
case 0: /* furious--make all monsters mad */ |
| 753 |
if (tmp->flag [FLAG_UNAGGRESSIVE]) |
| 754 |
tmp->clr_flag (FLAG_UNAGGRESSIVE); |
| 755 |
|
| 756 |
if (tmp->flag [FLAG_FRIENDLY]) |
| 757 |
{ |
| 758 |
tmp->attack_movement = 0; |
| 759 |
/* lots of checks here, but want to make sure we don't |
| 760 |
* dereference a null value |
| 761 |
*/ |
| 762 |
if (tmp->type == GOLEM |
| 763 |
&& tmp->owner |
| 764 |
&& tmp->owner->type == PLAYER |
| 765 |
&& tmp->owner->contr->golem == tmp) |
| 766 |
tmp->owner->contr->golem = 0; |
| 767 |
|
| 768 |
tmp->owner = 0; |
| 769 |
|
| 770 |
remove_friendly_object (tmp); |
| 771 |
} |
| 772 |
break; |
| 773 |
|
| 774 |
case 1: /* angry -- get neutral monsters mad */ |
| 775 |
if (tmp->flag [FLAG_UNAGGRESSIVE] && !tmp->flag [FLAG_FRIENDLY]) |
| 776 |
tmp->clr_flag (FLAG_UNAGGRESSIVE); |
| 777 |
break; |
| 778 |
|
| 779 |
case 2: /* calm -- pacify unfriendly monsters */ |
| 780 |
tmp->set_flag (FLAG_UNAGGRESSIVE); |
| 781 |
break; |
| 782 |
|
| 783 |
case 3: /* make all monsters fall asleep */ |
| 784 |
tmp->set_flag (FLAG_SLEEP); |
| 785 |
break; |
| 786 |
|
| 787 |
case 4: /* charm all monsters */ |
| 788 |
if (op == source) |
| 789 |
break; /* only if 'connected' */ |
| 790 |
|
| 791 |
if (object *pl = source->ms ().player ()) |
| 792 |
{ |
| 793 |
tmp->set_owner (pl); |
| 794 |
tmp->set_flag (FLAG_MONSTER); |
| 795 |
|
| 796 |
tmp->stats.exp = 0; |
| 797 |
|
| 798 |
add_friendly_object (tmp); |
| 799 |
tmp->attack_movement = PETMOVE; |
| 800 |
} |
| 801 |
break; |
| 802 |
|
| 803 |
case 6: // kill monsters |
| 804 |
if (!tmp->flag [FLAG_FRIENDLY]) |
| 805 |
break; |
| 806 |
|
| 807 |
// FALL THROUGH |
| 808 |
case 5: // kill all alives |
| 809 |
if (!tmp->flag [FLAG_PRECIOUS]) |
| 810 |
{ |
| 811 |
archetype::get (shstr_burnout)->insert_at (tmp, source); |
| 812 |
tmp->destroy (); |
| 813 |
} |
| 814 |
break; |
| 815 |
|
| 816 |
default: |
| 817 |
break; |
| 818 |
} |
| 819 |
} |
| 820 |
|
| 821 |
/* this function returns the object it matches, or NULL if non. |
| 822 |
* It will descend through containers to find the object. |
| 823 |
* slaying = match object slaying flag |
| 824 |
* race = match object archetype name flag |
| 825 |
* hp = match object type (excpt type '0'== PLAYER) |
| 826 |
*/ |
| 827 |
object * |
| 828 |
check_inv_recursive (object *op, const object *trig) |
| 829 |
{ |
| 830 |
object *tmp, *ret = NULL; |
| 831 |
|
| 832 |
/* First check the object itself. */ |
| 833 |
if ((trig->stats.hp && (op->type == trig->stats.hp)) |
| 834 |
|| (trig->slaying && (op->slaying == trig->slaying)) |
| 835 |
|| (trig->race && (op->arch->archname == trig->race))) |
| 836 |
return op; |
| 837 |
|
| 838 |
for (tmp = op->inv; tmp; tmp = tmp->below) |
| 839 |
{ |
| 840 |
if (tmp->inv) |
| 841 |
{ |
| 842 |
ret = check_inv_recursive (tmp, trig); |
| 843 |
if (ret) |
| 844 |
return ret; |
| 845 |
} |
| 846 |
else if ((trig->stats.hp && (tmp->type == trig->stats.hp)) |
| 847 |
|| (trig->slaying && (tmp->slaying == trig->slaying)) |
| 848 |
|| (trig->race && (tmp->arch->archname == trig->race))) |
| 849 |
return tmp; |
| 850 |
} |
| 851 |
return NULL; |
| 852 |
} |
| 853 |
|
| 854 |
/* check_inv(), a function to search the inventory, |
| 855 |
* of a player and then based on a set of conditions, |
| 856 |
* the square will activate connected items. |
| 857 |
* Monsters can't trigger this square (for now) |
| 858 |
* Values are: last_sp = 1/0 obj/no obj triggers |
| 859 |
* last_heal = 1/0 remove/dont remove obj if triggered |
| 860 |
* -b.t. (thomas@nomad.astro.psu.edu |
| 861 |
* |
| 862 |
* Tue Dec 19 15:34:00 CET 2006 elmex: changed the function to ignore op |
| 863 |
* because the check-inventory semantic essentially only applies when |
| 864 |
* something is above the inventory checker. |
| 865 |
* The semantic prior this change was: trigger if something has moved on or off |
| 866 |
* and has a matching item. Imagine what happens if someone steps on the inventory |
| 867 |
* checker with a matching item, has it, activates the connection, throws the item |
| 868 |
* away, and then leaves the inventory checker. That would've caused an always-enabled |
| 869 |
* state in the inventory checker. This won't happen anymore now. |
| 870 |
* |
| 871 |
* Wed Jan 10 11:34:26 CET 2007 elmex: fixed this function, we now check |
| 872 |
* whether op is on this mapspace or not, because the value (1|0) depends |
| 873 |
* on this information. also make sure to only push_button if op has |
| 874 |
* a matching item (because when we do a push_button with value=0 timed gates |
| 875 |
* will still open)! (i hope i got the semantics right this time) |
| 876 |
* |
| 877 |
*/ |
| 878 |
void |
| 879 |
check_inv (object *op, object *trig) |
| 880 |
{ |
| 881 |
trig->value = 0; // deactivate if none of the following conditions apply |
| 882 |
|
| 883 |
object *pl = trig->ms ().player (); |
| 884 |
object *match = check_inv_recursive (op, trig); |
| 885 |
|
| 886 |
// elmex: a note about (pl == op): |
| 887 |
// if pl == 0 then the player has left this space |
| 888 |
// if pl != 0 then a player is on this mapspace, but then |
| 889 |
// we still have to check whether it's the player that triggered |
| 890 |
// this inv-checker, because if not, then the op left this inv-checker |
| 891 |
// and we have to set the value to 0 |
| 892 |
|
| 893 |
if (match && trig->last_sp) // match == having |
| 894 |
{ |
| 895 |
if (trig->last_heal) |
| 896 |
match->decrease (); |
| 897 |
|
| 898 |
trig->value = (pl == op ? 1 : 0); // 1 if matching player entered, and 0 if he left |
| 899 |
push_button (trig, op); |
| 900 |
} |
| 901 |
else if (!match && !trig->last_sp) // match == not having |
| 902 |
{ |
| 903 |
trig->value = (pl == op ? 1 : 0); // 1 if matching player entered, and 0 if he left |
| 904 |
push_button (trig, op); |
| 905 |
} |
| 906 |
} |
| 907 |
|