File: gameclk.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 (244 lines) | stat: -rw-r--r-- 5,483 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
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
/*
 *	gameclk.cc - Keep track of time.
 *
 *  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

#include <iostream>			/* Debugging. */
#include "gameclk.h"
#include "gamewin.h"
#include "actors.h"
#include "cheat.h"
#include "game.h"

/*
 *	Set palette.
 */

void Game_clock::set_time_palette
	(
	)
	{
	Game_window *gwin = Game_window::get_game_window();
	Actor *main_actor = gwin->get_main_actor();
	int new_palette;
	if (main_actor && main_actor->get_flag(Obj_flags::invisible))
	{
		gwin->set_palette(PALETTE_INVISIBLE);
		return;
		}

	if (cheat.in_infravision()) {
		gwin->set_palette(PALETTE_DAY);
		return;
	}

	if (gwin->is_in_dungeon() || hour < 5)
		new_palette = PALETTE_NIGHT;
	else if (hour < 6)
		new_palette = PALETTE_DAWN;
	else if (hour < 19)
		new_palette = storm <= 0 ? PALETTE_DAY : PALETTE_DUSK;
	else if (hour < 21)
		new_palette = PALETTE_DUSK;
	else
		new_palette = PALETTE_NIGHT;
	if (light_source_level)
		{
		if (new_palette == PALETTE_NIGHT)
			new_palette = light_source_level == 1 ? PALETTE_DUSK
							: PALETTE_DAWN;
		else if (new_palette == PALETTE_DUSK)
			new_palette = PALETTE_DAWN;
		}
					// Gump mode, or light spell?
	if (gwin->is_special_light() && new_palette == PALETTE_NIGHT)
		new_palette = PALETTE_DAWN;
	gwin->set_palette(new_palette);
	}

/*
 *	Set palette.  Used for restoring a game.
 */

void Game_clock::set_palette
	(
	)
{
	// Update palette to new time.
	set_time_palette();
}

/*
 *	Set the palette for a changed light source level.
 */

void Game_clock::set_light_source_level
	(
	int lev
	)
{
	light_source_level = lev;
	set_time_palette();
}

/*
 *	Storm ending/starting.
 */

void Game_clock::set_storm
	(
	bool onoff
	)
{
	storm += (onoff ? 1 : -1);
	set_time_palette();		// Update palette.
}

/*
 *	Decrement food level and check hunger of the party members.
 */

void Game_clock::check_hunger
	(
	)
{
	Game_window *gwin = Game_window::get_game_window();
	Actor *party[9];		// Get party + Avatar.
	int cnt = gwin->get_party(party, 1);
	for (int i = 0; i < cnt; i++)
		party[i]->use_food();
}

static void Check_freezing
	(
	)
	{
	Game_window *gwin = Game_window::get_game_window();
					// Avatar's flag applies to party.
	bool freeze = gwin->get_main_actor()->get_flag(Obj_flags::freeze)!=false;
	Actor *party[9];		// Get party + Avatar.
	int cnt = gwin->get_party(party, 1);
	for (int i = 0; i < cnt; i++)
		party[i]->check_temperature(freeze);
	}

/*
 *	Increment clock.
 *
 *	FOR NOW, skipping call to mend_npcs().  Not sure...
 */

void Game_clock::increment
	(
	int num_minutes			// # of minutes to increment.
	)
{
	Game_window *gwin = Game_window::get_game_window();
	int new_3hour, old_3hour, delta_3hour;
	long new_min;
	
	old_3hour = hour/3;		// Remember current 3-hour period.
	num_minutes += time_factor/2;	// Round to nearest 15 minutes.
	num_minutes -= num_minutes%time_factor;
	new_min = minute + num_minutes;
	hour += new_min/60;		// Update hour.
	minute = new_min%60;
	day += hour/24;			// Update day.
	hour %= 24;
	
	// Update palette to new time.
	set_time_palette();
	new_3hour = hour/3;		// New 3-hour period.
	delta_3hour = new_3hour - old_3hour;
	if (delta_3hour != 0)		// In a new period?
	{			// Update NPC schedules.
		if (Game::get_game_type() == SERPENT_ISLE)
			delta_3hour = 8;
		gwin->schedule_npcs(new_3hour, (delta_3hour +7)%8);
	}
}

/*
 *	Advance clock.
 */

void Game_clock::handle_event
	(
	unsigned long curtime,		// Current time of day.
	long udata			// ->game window.
	)
{
	Game_window *gwin = (Game_window *) udata;

	int min_old = minute;
	int hour_old = hour;
				// Time stopped?  Don't advance.
	if (!gwin->is_time_stopped())
		{
		minute += time_rate;
		if (Game::get_game_type() == SERPENT_ISLE)
			Check_freezing();
		}

	while (minute >= 60)	// advance to the correct hour (and day)
	{
		minute -= 60;
		if (++hour >= 24)
		{
			hour -= 24;
			day++;
		}
		set_time_palette();
		gwin->mend_npcs();	// Restore HP's each hour.
		if (hour%3 == 0)	// New 3-hour period?
		{
			check_hunger();	// Use food, and print complaints.
					// Update NPC schedules.
			gwin->schedule_npcs(hour/3);
		}
	}
	if ((hour != hour_old) || (minute/15 != min_old/15))
		COUT("Clock updated to " << hour << ':' << minute);
	curtime += 60*1000/time_factor;		// 15 changes per minute
	tqueue->add(curtime, this, udata);
}

/*
 *	Fake an update to the next 3-hour period.
 */

void Game_clock::fake_next_period
	(
	)
{
	minute = 0;
	hour = ((hour/3 + 1)*3);
	day += hour/24;			// Update day.
	hour %= 24;
	Game_window *gwin = Game_window::get_game_window();
	set_time_palette();
	check_hunger();
	gwin->schedule_npcs(hour/3);
	gwin->mend_npcs();		// Just do it once, cheater.
	COUT("The hour is now " << hour);
}