1 /*
2  *  gameclk.h - Keep track of time.
3  *
4  *  Copyright (C) 2000-2013  The Exult Team
5  *
6  *  This program is free software; you can redistribute it and/or modify
7  *  it under the terms of the GNU General Public License as published by
8  *  the Free Software Foundation; either version 2 of the License, or
9  *  (at your option) any later version.
10  *
11  *  This program is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *  GNU General Public License for more details.
15  *
16  *  You should have received a copy of the GNU General Public License
17  *  along with this program; if not, write to the Free Software
18  *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19  */
20 
21 #ifndef GAMECLK_H
22 #define GAMECLK_H 1
23 
24 #include "tqueue.h"
25 #include "palette.h"
26 
27 #include <memory>
28 
29 /*
30  *  The number of times that the game clock ticks in one game minute.
31  */
32 const int ticks_per_minute = 25;
33 
34 /*
35  *  Keep track of time, and of the palette for a given time.
36  */
37 class Game_clock : public Time_sensitive {
38 	Time_queue *tqueue;     // The time queue.
39 	short hour, minute, ticks;     // Time (0-23, 0-59).
40 	int day;            // Keep track of days played.
41 	int light_source_level;     // Last set light source level.
42 	int old_light_level;        // Last set light source level.
43 	bool old_special_light;     // Last set light source level.
44 	bool old_infravision;       // If infravision was on last time.
45 	bool old_invisible;     // If invisibility was on last time.
46 	int dungeon;        // Last set 'in_dungeon' value.
47 	int overcast;           // >0 if day is overcast (e.g., from a storm).
48 	bool was_overcast;
49 	int fog;            // >0 if there is fog.
50 	bool was_foggy;
51 	std::unique_ptr<Palette_transition> transition; // For smooth palette transitions.
52 	unsigned short time_rate;
53 	void set_time_palette(bool force);
54 	void set_light_source_level(int lev);
55 	void check_hunger() const;
56 public:
Game_clock(Time_queue * tq)57 	Game_clock(Time_queue *tq) : tqueue(tq), hour(6), minute(0), ticks(0), day(0),
58 		light_source_level(0), old_light_level(0), old_special_light(false),
59 		old_infravision(false), old_invisible(false), dungeon(255),
60 		overcast(0), was_overcast(false), fog(0), was_foggy(false), time_rate(1)
61 	{ }
get_hour()62 	int get_hour() const {
63 		return hour;
64 	}
set_hour(int h)65 	void set_hour(int h) {
66 		hour = h;
67 	}
get_minute()68 	int get_minute() const {
69 		return minute;
70 	}
set_minute(int m)71 	void set_minute(int m) {
72 		minute = m;
73 	}
get_ticks()74 	int get_ticks() const {
75 		return ticks;
76 	}
set_ticks(int m)77 	void set_ticks(int m) {
78 		ticks = m;
79 	}
get_day()80 	int get_day() const {
81 		return day;
82 	}
set_day(int d)83 	void set_day(int d) {
84 		day = d;
85 	}
get_total_hours()86 	unsigned long get_total_hours() const { // Get total # hours.
87 		return day * 24 + hour;
88 	}
get_total_minutes()89 	unsigned long get_total_minutes() const {
90 		return get_total_hours() * 60 + minute;
91 	}
92 	void set_palette();     // Set palette for current hour.
93 	void reset_palette();
94 	// Set light source.  MUST be fast,
95 	//   since it's called during paint().
set_light_source(int lev,int dun)96 	void set_light_source(int lev, int dun) {
97 		if (lev != light_source_level || dun != dungeon)
98 			set_light_source_level(lev);
99 	}
reset()100 	void reset() {
101 		overcast = fog = 0;
102 		was_overcast = was_foggy = false;
103 		old_special_light = false;
104 		old_infravision = false;
105 		old_invisible = false;
106 		dungeon = 255;
107 		transition.reset();
108 	}
109 	void set_overcast(bool onoff);  // Start/end cloud cover.
110 	void set_fog(bool onoff);   // Start/end cloud cover.
111 	void increment(int num_minutes);// Increment clock.
112 	void handle_event(unsigned long curtime, uintptr udata) override;
113 	void fake_next_period();    // For debugging.
get_time_rate()114 	int get_time_rate() const {
115 		return time_rate;
116 	}
set_time_rate(int i)117 	void set_time_rate(int i) {
118 		time_rate = i > 0 ? i : 1;
119 	}
120 };
121 
122 #endif  /* INCL_GAMECLK */
123