File: npcnear.cc

package info (click to toggle)
exult 0.98rc1-2
  • links: PTS
  • area: contrib
  • in suites: woody
  • size: 6,924 kB
  • ctags: 8,928
  • sloc: cpp: 83,768; sh: 7,643; ansic: 4,328; makefile: 890; yacc: 618; lex: 255; xml: 19
file content (209 lines) | stat: -rw-r--r-- 5,422 bytes parent folder | download
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
/*
 *	npcnear.cc - At random times, run proximity usecode functions on nearby 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 Library 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

#ifndef ALPHA_LINUX_CXX
#  include <cstdlib>
#endif
#include "npcnear.h"
#include "gamewin.h"
#include "actors.h"
#include "ucmachine.h"
#include "schedule.h"
#include "items.h"
#include "game.h"
#include "cheat.h"

#include "SDL_timer.h"

using std::rand;

bool Bg_dont_wake(Game_window *gwin, Actor *npc);

/*
 *	Add an npc to the time queue.
 */

void Npc_proximity_handler::add
	(
	unsigned long curtime,		// Current time (msecs).
	Npc_actor *npc,
	int additional_secs		// More secs. to wait.
	)
	{
	int msecs;			// Hostile?  Wait 0-4 secs.
	if (npc->get_alignment() >= Npc_actor::hostile)
		msecs = rand() % 4000;
	else				// Wait between 2 & 6 secs.
		msecs = (rand() % 4000) + 2000;
	unsigned long newtime = curtime + msecs;
	newtime += 1000*additional_secs;
	gwin->get_tqueue()->add(newtime, this, reinterpret_cast<long>(npc));
	}

/*
 *	Remove entry for an npc.
 */

void Npc_proximity_handler::remove
	(
	Npc_actor *npc
	)
	{
	npc->clear_nearby();
	gwin->get_tqueue()->remove(this, reinterpret_cast<long>(npc)); 
	}

/*
 *	Is this a Black Gate (Skara Brae) ghost, or Penumbra?
 */

bool Bg_dont_wake
	(
	Game_window *gwin,
	Actor *npc
	)
	{
	int num;
	return (Game::get_game_type() == BLACK_GATE &&
		(gwin->get_info(npc).has_translucency() ||
					// Horace or Penumbra?
		 (num = npc->Actor::get_npc_num()) == 141 || num == 150));
	}

/*
 *	Run proximity usecode function for the NPC now.
 */

void Npc_proximity_handler::handle_event
	(
	unsigned long curtime,
	long udata
	)
	{
	Npc_actor *npc = (Npc_actor *) udata;
	int extra_delay = 5;		// For next time.
					// See if still on visible screen.
	Rectangle tiles = gwin->get_win_tile_rect().enlarge(10);
	Tile_coord t = npc->get_tile();
	if (!tiles.has_point(t.tx, t.ty) ||	// No longer visible?
	    npc->is_dead())		// Or no longer living?
		{
		npc->clear_nearby();
		return;
		}
	Schedule::Schedule_types sched = (Schedule::Schedule_types)
						npc->get_schedule_type();
					// Sleep schedule?
	if (npc->get_schedule() &&
	    sched == Schedule::sleep &&
					// But not under a sleep spell?
	    !npc->get_flag(Obj_flags::asleep) &&
	    gwin->is_main_actor_inside() &&
	    !Bg_dont_wake(gwin, npc) &&
	    npc->distance(gwin->get_main_actor()) < 6 && rand()%3 != 0)
		{
					// Trick:  Stand, but stay in
					//   sleep_schedule.
		npc->get_schedule()->ending(Schedule::stand);
					// Hostile monster?
		if (npc->get_alignment() == Npc_actor::hostile &&
		    npc != gwin->get_camera_actor() && npc->is_monster())
			{
			npc->say("Aaargggh!!");
			npc->set_schedule_type(Schedule::combat);
			}
		else
			{
			npc->say(first_awakened, last_awakened);
					// In 10 seconds, go back to sleep.
			npc->start(0, 10000);
			extra_delay = 11;// And don't run Usecode while up.
			}
		}
					// Hostile monster?  ATTACK!
	else if (npc->get_alignment() == Npc_actor::hostile &&
		npc != gwin->get_camera_actor() &&
		npc->is_monster() &&
		sched != Schedule::combat && sched != Schedule::sleep &&
					// jsf-Trying to fix mage in
					// Test of Courage:
		sched != Schedule::wait)
		{
		npc->set_schedule_type(Schedule::combat);
		}
			
	else if (!(curtime < wait_until) && !cheat.in_map_editor() && 
					// Do it 50% of the time OR if
					//   a rabbit (SI start).
		 (rand()%2 == 1 || npc->get_shapenum() == 811)  &&
					// And not for party members.
			npc->get_party_id() < 0 &&
					// And not for patrollers/monsters
					//  in SI. !!Guessing.
		 (Game::get_game_type() != SERPENT_ISLE ||
			(sched != Schedule::patrol &&
			 sched != Schedule::wait &&
			 sched != Schedule::horiz_pace &&
			 sched != Schedule::vert_pace &&
			 !npc->is_monster())))
				
		{
		int ucfun = npc->get_usecode();
		gwin->get_usecode()->call_usecode(ucfun, npc,
					Usecode_machine::npc_proximity);
		extra_delay += 3;
		curtime = Game::get_ticks();// Time may have passed.
		}
	add(curtime, npc, extra_delay);	// Add back for next time.
	}

/*
 *	Set a time to wait for before running any usecodes.  This is to
 *	skip all the text events that would happen at the end of a conver-
 *	sation.
 */

void Npc_proximity_handler::wait
	(
	int secs			// # of seconds.
	)
	{
	wait_until = Game::get_ticks() + 1000*secs;
	}

/*
 *	Fill a list with all the nearby NPC's.
 */

void Npc_proximity_handler::get_all
	(
	Actor_queue& list			// They're appended to this.
	)
	{
	Time_queue_iterator next(gwin->get_tqueue(), this);
	Time_sensitive *obj;
	long data;			// NPC is the data.
	while (next(obj, data))
		list.push((Npc_actor *) data);
	}