]> git.pond.sub.org Git - empserver/blob - src/lib/common/game.c
License upgrade to GPL version 3 or later
[empserver] / src / lib / common / game.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2011, Dave Pare, Jeff Bailey, Thomas Ruschak,
4  *                Ken Stevens, Steve McClure, Markus Armbruster
5  *
6  *  Empire 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 3 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, see <http://www.gnu.org/licenses/>.
18  *
19  *  ---
20  *
21  *  See files README, COPYING and CREDITS in the root of the source
22  *  tree for related information and legal notices.  It is expected
23  *  that future projects/authors will amend these files as needed.
24  *
25  *  ---
26  *
27  *  game.c: Game file access
28  *
29  *  Known contributors to this file:
30  *     Markus Armbruster, 2007-2009
31  */
32
33 /*
34  * On Empire Time:
35  *
36  * An Empire turn is terminated by an update.  The Empire clock counts
37  * turns and ETUs, i.e. it ticks etu_per_update times per turn.  When
38  * updates move around in real time (schedule change, downtime, etc.),
39  * the Empire clock automatically adapts the duration of an ETU
40  * accordingly.
41  */
42
43 #include <config.h>
44
45 #include <math.h>
46 #include "file.h"
47 #include "game.h"
48 #include "optlist.h"
49 #include "prototypes.h"
50 #include "server.h"
51
52 /*
53  * Enable / disable updates
54  */
55 void
56 game_ctrl_update(int enable)
57 {
58     struct gamestr *game = getgamep();
59
60     game->game_upd_disable = !enable;
61     putgame();
62 }
63
64 /*
65  * Are updates disabled?
66  */
67 int
68 updates_disabled(void)
69 {
70     return getgamep()->game_upd_disable;
71 }
72
73 /*
74  * Enable / disable play
75  */
76 void
77 game_ctrl_play(int enable)
78 {
79     struct gamestr *game = getgamep();
80
81     game->game_down = !enable;
82     putgame();
83 }
84
85 /*
86  * Is playing enabled?
87  */
88 int
89 game_play_disabled(void)
90 {
91     return getgamep()->game_down;
92 }
93
94 /*
95  * Notice that a player broke sanctuary.
96  * This starts the Empire clock if it hasn't been started yet.
97  */
98 void
99 game_note_bsanct(void)
100 {
101     struct gamestr *game = getgamep();
102
103     if (game->game_rt == 0) {
104         game->game_rt = time(NULL);
105         putgame();
106     }
107 }
108
109 /*
110  * Record an update in the game file, the current time is NOW.
111  * This starts the Empire clock if it hasn't been started yet.
112  */
113 void
114 game_record_update(time_t now)
115 {
116     struct gamestr *game = getgamep();
117
118     game->game_turn++;
119     game->game_tick = 0;
120     game->game_rt = now;
121     putgame();
122 }
123
124 /*
125  * Return current duration of an ETU in seconds.
126  * Note: may return HUGE_VAL when the Empire clock is not ticking.
127  */
128 static double
129 secs_per_etu(struct gamestr *game)
130 {
131     double secs;
132
133     if (!game->game_rt || !update_time[0])
134         return HUGE_VAL;        /* not started or no update scheduled */
135
136     secs = update_time[0] - game->game_rt;
137     if (secs < 0)
138         return HUGE_VAL;        /* update seems to be late */
139     return secs / (etu_per_update - game->game_tick);
140 }
141
142 /*
143  * Update the Empire clock according to the current real time.
144  * Return the game struct.
145  */
146 struct gamestr *
147 game_tick_tick(void)
148 {
149     struct gamestr *game = getgamep();
150     double dsecs, s_p_etu;
151     int detu;
152
153     dsecs = time(NULL) - game->game_rt;
154     if (CANT_HAPPEN(dsecs < 0))
155         dsecs = 0;
156     s_p_etu = secs_per_etu(game);
157     detu = (int)(dsecs / s_p_etu);
158     if (detu > 0) {
159         if (CANT_HAPPEN(game->game_tick + detu > etu_per_update))
160             detu = etu_per_update - game->game_tick;
161         game->game_tick += detu;
162         game->game_rt += detu * s_p_etu;
163         putgame();
164     }
165
166     return game;
167 }
168
169 /*
170  * Set ETU timestamp *TICK to the current ETU time.
171  * Return by how many ETUs it was increased.
172  */
173 int
174 game_tick_to_now(short *tick)
175 {
176     return game_step_a_tick(game_tick_tick(), tick);
177 }
178
179 /*
180  * Set ETU timestamp *TICK to the ETU time recorded in the game struct.
181  * The Empire clock is not updated.
182  * Return by how many ETUs it was increased.
183  */
184 int
185 game_step_a_tick(struct gamestr *game, short *tick)
186 {
187     int etu;
188
189     etu = game->game_tick - *tick;
190     if (CANT_HAPPEN(etu < 0))
191         etu = 0;
192     *tick = game->game_tick;
193     return etu;
194 }
195
196 /*
197  * Reset ETU timestamp *TICK to zero.
198  * Return how many ETUs it had left until etu_per_update.
199  */
200 int
201 game_reset_tick(short *tick)
202 {
203     int etu;
204
205     etu = etu_per_update - *tick;
206     if (CANT_HAPPEN(etu < 0))
207         etu = 0;
208     *tick = 0;
209     return etu;
210 }