1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547
|
/*
* Npctime.cc - Timed-even handlers for NPC's.
*
* Copyright (C) 2000-2001 The Exult Team
*
* This program 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 2 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, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include "npctime.h"
#include "gamewin.h"
#include "gameclk.h"
#include "actors.h"
#include "items.h"
#include "schedule.h"
#include "game.h"
#ifndef UNDER_CE
using std::rand;
#endif
extern bool god_mode;
/*
* Base class for keeping track of things like poison, protection, hunger.
*/
class Npc_timer : public Time_sensitive, public Game_singletons
{
protected:
Npc_timer_list *list; // Where NPC stores ->this.
uint32 get_minute(); // Get game minutes.
public:
Npc_timer(Npc_timer_list *l, int start_delay = 0);
virtual ~Npc_timer();
};
/*
* Handle starvation.
*/
class Npc_hunger_timer : public Npc_timer
{
uint32 last_time; // Last game minute when penalized.
public:
Npc_hunger_timer(Npc_timer_list *l) : Npc_timer(l, 5000)
{ last_time = get_minute(); }
virtual ~Npc_hunger_timer();
// Handle events:
void handle_event(unsigned long curtime, long udata);
};
/*
* Handle poison.
*/
class Npc_poison_timer : public Npc_timer
{
uint32 end_time; // Time when it wears off.
public:
Npc_poison_timer(Npc_timer_list *l);
virtual ~Npc_poison_timer();
// Handle events:
void handle_event(unsigned long curtime, long udata);
};
/*
* Handle sleep.
*/
class Npc_sleep_timer : public Npc_timer
{
uint32 end_time; // Time when it wears off.
public:
Npc_sleep_timer(Npc_timer_list *l) : Npc_timer(l)
{ // Lasts 5-10 seconds..
end_time = Game::get_ticks() + 5000 + rand()%5000;
}
virtual ~Npc_sleep_timer()
{ list->sleep = 0; }
// Handle events:
void handle_event(unsigned long curtime, long udata);
};
/*
* Invisibility timer.
*/
class Npc_invisibility_timer : public Npc_timer
{
uint32 end_time; // Time when it wears off.
public:
Npc_invisibility_timer(Npc_timer_list *l) : Npc_timer(l)
{ // Lasts 60-80 seconds..
end_time = Game::get_ticks() + 60000 + rand()%20000;
}
virtual ~Npc_invisibility_timer()
{ list->invisibility = 0; }
// Handle events:
void handle_event(unsigned long curtime, long udata);
};
/*
* Protection timer.
*/
class Npc_protection_timer : public Npc_timer
{
uint32 end_time; // Time when it wears off.
public:
Npc_protection_timer(Npc_timer_list *l) : Npc_timer(l)
{ // Lasts 60-80 seconds..
end_time = Game::get_ticks() + 60000 + rand()%20000;
}
virtual ~Npc_protection_timer()
{ list->protection = 0; }
// Handle events:
void handle_event(unsigned long curtime, long udata);
};
/*
* Timer for flags that don't need any other checks.
*/
class Npc_flag_timer : public Npc_timer
{
int flag; // Flag # in Obj_flags.
uint32 end_time; // Time when it wears off.
Npc_flag_timer **listloc; // Where it's stored in Npc_timer_list.
public:
Npc_flag_timer(Npc_timer_list *l, int f, Npc_flag_timer **loc)
: Npc_timer(l), flag(f), listloc(loc)
{ // Lasts 60-120 seconds..
end_time = Game::get_ticks() + 60000 + rand()%60000;
}
virtual ~Npc_flag_timer()
{ *listloc = 0; }
// Handle events:
void handle_event(unsigned long curtime, long udata);
};
/*
* Delete list.
*/
Npc_timer_list::~Npc_timer_list
(
)
{
delete hunger;
delete poison;
delete sleep;
delete invisibility;
delete protection;
delete might;
delete curse;
delete paralyze;
}
/*
* Start hunger (if not already there).
*/
void Npc_timer_list::start_hunger
(
)
{
if (!hunger) // Not already there?
hunger = new Npc_hunger_timer(this);
}
/*
* Start poison.
*/
void Npc_timer_list::start_poison
(
)
{
if (poison) // Remove old one.
delete poison;
poison = new Npc_poison_timer(this);
}
/*
* Start sleep.
*/
void Npc_timer_list::start_sleep
(
)
{
if (sleep) // Remove old one.
delete sleep;
sleep = new Npc_sleep_timer(this);
}
/*
* Start invisibility.
*/
void Npc_timer_list::start_invisibility
(
)
{
if (invisibility) // Remove old one.
delete invisibility;
invisibility = new Npc_invisibility_timer(this);
}
/*
* Start protection.
*/
void Npc_timer_list::start_protection
(
)
{
if (protection) // Remove old one.
delete protection;
protection = new Npc_protection_timer(this);
}
/*
* Start might.
*/
void Npc_timer_list::start_might
(
)
{
if (might) // Remove old one.
delete might;
might = new Npc_flag_timer(this, Obj_flags::might, &might);
}
/*
* Start curse.
*/
void Npc_timer_list::start_curse
(
)
{
if (curse) // Remove old one.
delete curse;
curse = new Npc_flag_timer(this, Obj_flags::cursed, &curse);
}
/*
* Start paralyze.
*/
void Npc_timer_list::start_paralyze
(
)
{
if (paralyze) // Remove old one.
delete paralyze;
paralyze = new Npc_flag_timer(this, Obj_flags::paralyzed, ¶lyze);
}
/*
* Get game minute from start.
*/
uint32 Npc_timer::get_minute
(
)
{
Game_window *gwin = Game_window::get_instance();
return 60*gclock->get_total_hours() + gclock->get_minute();
}
/*
* Start timer.
*/
Npc_timer::Npc_timer
(
Npc_timer_list *l,
int start_delay // Time in msecs. before starting.
) : list(l)
{
gwin->get_tqueue()->add(Game::get_ticks() + start_delay, this, 0L);
}
/*
* Be sure we're no longer in the time queue.
*/
Npc_timer::~Npc_timer
(
)
{
if (in_queue())
{
Time_queue *tq = Game_window::get_instance()->get_tqueue();
tq->remove(this);
}
}
/*
* Done with hunger timer.
*/
Npc_hunger_timer::~Npc_hunger_timer
(
)
{
list->hunger = 0;
}
/*
* Time to penalize for hunger.
*/
void Npc_hunger_timer::handle_event
(
unsigned long curtime,
long udata
)
{
Actor *npc = list->npc;
// No longer a party member?
if (!npc->is_in_party() ||
// or no longer hungry?
npc->get_property(static_cast<int>(Actor::food_level)) >= 0 ||
npc->is_dead()) // Obviously.
{
delete this;
return;
}
uint32 minute = get_minute();
// Once/hour.
if (minute >= last_time + 60)
{
int hp = rand()%3;
if (rand()%4)
npc->say(first_starving, first_starving + 2);
npc->reduce_health(hp);
// npc->set_property(static_cast<int>(Actor::health),
// npc->get_property(static_cast<int>(Actor::health)) - hp);
last_time = minute;
}
gwin->get_tqueue()->add(curtime + 30000, this, 0L);
}
/*
* Initialize poison timer.
*/
Npc_poison_timer::Npc_poison_timer
(
Npc_timer_list *l
) : Npc_timer(l, 5000)
{
// Lasts 1-3 minutes.
end_time = Game::get_ticks() + 60000 + rand()%120000;
}
/*
* Done with poison timer.
*/
Npc_poison_timer::~Npc_poison_timer
(
)
{
list->poison = 0;
}
/*
* Time to penalize for poison, or see if it's worn off.
*/
void Npc_poison_timer::handle_event
(
unsigned long curtime,
long udata
)
{
Actor *npc = list->npc;
if (curtime >= end_time || // Long enough? Or cured?
npc->get_flag(Obj_flags::poisoned) == 0 ||
npc->is_dead()) // Obviously.
{
npc->clear_flag(Obj_flags::poisoned);
delete this;
return;
}
int penalty = rand()%3;
npc->reduce_health(penalty);
// npc->set_property(static_cast<int>(Actor::health),
// npc->get_property(static_cast<int>(Actor::health)) - penalty);
// Check again in 10-20 secs.
gwin->get_tqueue()->add(curtime + 10000 + rand()%10000, this, 0L);
}
/*
* Time to see if we should wake up.
*/
void Npc_sleep_timer::handle_event
(
unsigned long curtime,
long udata
)
{
Actor *npc = list->npc;
if (curtime >= end_time || // Long enough? Or cured?
npc->get_flag(Obj_flags::asleep) == 0)
{
// Avoid waking Penumbra.
if (npc->get_schedule_type() != Schedule::sleep &&
!npc->is_dead()) // Don't wake the dead.
{
npc->clear_flag(Obj_flags::asleep);
int frnum = npc->get_framenum();
if ((frnum&0xf) == Actor::sleep_frame &&
// Slimes don't change.
!npc->get_info().has_strange_movement())
// Stand up.
npc->change_frame(
Actor::standing | (frnum&0x30));
}
delete this;
return;
}
// Check again in 2 secs.
gwin->get_tqueue()->add(curtime + 2000, this, 0L);
}
/*
* Check for a given ring.
*/
inline int Wearing_ring
(
Actor *actor,
int shnum, // Ring shape to look for.
int frnum
)
{
// See if wearing ring.
Game_object *ring = actor->get_readied(Actor::lfinger);
if (ring && ring->get_shapenum() == shnum &&
ring->get_framenum() == frnum)
return 1;
ring = actor->get_readied(Actor::rfinger);
if (ring && ring->get_shapenum() == shnum &&
ring->get_framenum() == frnum)
return 1;
return 0;
}
/*
* See if invisibility wore off.
*/
void Npc_invisibility_timer::handle_event
(
unsigned long curtime,
long udata
)
{
Actor *npc = list->npc;
if (Wearing_ring(npc, 296, 0)) // (Works for SI and BG.)
{ // Wearing invisibility ring.
delete this; // Don't need timer.
return;
}
if (curtime >= end_time || // Long enough? Or cleared.
npc->get_flag(Obj_flags::invisible) == 0)
{
npc->clear_flag(Obj_flags::invisible);
if (!npc->is_dead())
gwin->add_dirty(npc);
delete this;
return;
}
// Check again in 2 secs.
gwin->get_tqueue()->add(curtime + 2000, this, 0L);
}
/*
* See if protection wore off.
*/
void Npc_protection_timer::handle_event
(
unsigned long curtime,
long udata
)
{
Actor *npc = list->npc;
if (Wearing_ring(npc, 297, 0)) // ++++SI has an Amulet.
{ // Wearing protection ring.
delete this; // Don't need timer.
return;
}
if (curtime >= end_time || // Long enough? Or cleared.
npc->get_flag(Obj_flags::protection) == 0)
{
npc->clear_flag(Obj_flags::protection);
if (!npc->is_dead())
gwin->add_dirty(npc);
delete this;
return;
}
// Check again in 2 secs.
gwin->get_tqueue()->add(curtime + 2000, this, 0L);
}
/*
* Might/curse/paralyze wore off.
*/
void Npc_flag_timer::handle_event
(
unsigned long curtime,
long udata
)
{
Actor *npc = list->npc;
if (curtime >= end_time || // Long enough? Or cleared.
npc->get_flag(flag) == 0)
{
npc->clear_flag(flag);
delete this;
}
else // Check again in 10 secs.
gwin->get_tqueue()->add(curtime + 10000, this, 0L);
}
|