summaryrefslogtreecommitdiff
path: root/src/uqm/clock.h
blob: 5bd428c1b8fd0bc16ea1df1d9aa6350cb05f2b01 (plain)
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
//Copyright Paul Reiche, Fred Ford. 1992-2002

/*
 *  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.
 */

#ifndef UQM_CLOCK_H_
#define UQM_CLOCK_H_

#include "libs/tasklib.h"
#include "displist.h"

#if defined(__cplusplus)
extern "C" {
#endif


#define START_YEAR 2155

#define UPDATE_DAY (1 << 0)
#define UPDATE_MONTH (1 << 1)
#define UPDATE_YEAR (1 << 2)

typedef struct
{
	BYTE day_index, month_index;
	COUNT year_index;
	SIZE tick_count, day_in_ticks;

	QUEUE event_q;
			/* Queue element is EVENT */
} CLOCK_STATE;

typedef HLINK HEVENT;

typedef struct event
{
	// LINK elements; must be first
	HEVENT pred, succ;

	BYTE day_index, month_index;
	COUNT year_index;
	BYTE func_index;
} EVENT;

typedef enum
{
	ABSOLUTE_EVENT = 0,
	RELATIVE_EVENT
} EVENT_TYPE;

#define AllocEvent() AllocLink (&GLOBAL (GameClock.event_q))
#define PutEvent(h) PutQueue (&GLOBAL (GameClock.event_q), (h))
#define InsertEvent(h,i) InsertQueue (&GLOBAL (GameClock.event_q), (h), (i))
#define GetHeadEvent() GetHeadLink (&GLOBAL (GameClock.event_q))
#define GetTailEvent() GetTailLink (&GLOBAL (GameClock.event_q))
#define LockEvent(h,ppe) (*(ppe) = (EVENT*)LockLink (&GLOBAL (GameClock.event_q), h))
#define UnlockEvent(h) UnlockLink (&GLOBAL (GameClock.event_q), (h))
#define RemoveEvent(h) RemoveQueue (&GLOBAL (GameClock.event_q), (h))
#define FreeEvent(h) FreeLink (&GLOBAL (GameClock.event_q), (h))
#define GetPredEvent(l) _GetPredLink (l)
#define GetSuccEvent(l) _GetSuccLink (l)
#define ForAllEvents(callback, arg) ForAllLinks(&GLOBAL (GameClock.event_q), \
		(void (*)(LINK *, void *)) (callback), (arg))

// Rates are in seconds per game day
#define HYPERSPACE_CLOCK_RATE 5
// XXX: the IP rate is based on 24 ticks/second (see SetGameClockRate),
//   however, IP runs at 30 fps right now. So in reality, the IP clock
//   rate is closer to 23 seconds per game day. The clock is faster, but
//   the flagship also moves faster.
#define INTERPLANETARY_CLOCK_RATE 30

extern BOOLEAN InitGameClock (void);
extern BOOLEAN UninitGameClock (void);

extern void SetGameClockRate (COUNT seconds_per_day);
extern BOOLEAN ValidateEvent (EVENT_TYPE type, COUNT *pmonth_index,
		COUNT *pday_index, COUNT *pyear_index);
extern HEVENT AddEvent (EVENT_TYPE type, COUNT month_index, COUNT
		day_index, COUNT year_index, BYTE func_index);
extern void EventHandler (BYTE selector);
extern void GameClockTick (void);
extern void MoveGameClockDays (COUNT days);

// The lock/unlock/running functions are for debugging use only
// Locking will block the GameClockTick() function and thus
// the thread moving the clock.
extern void LockGameClock (void);
extern void UnlockGameClock (void);
// A weak indicator of the clock moving. Suitable for debugging,
// but not much else
extern BOOLEAN GameClockRunning (void);

#if defined(__cplusplus)
}
#endif

#endif /* UQM_CLOCK_H_ */