]> git.pond.sub.org Git - empserver/blob - src/lib/common/game.c
Update copyright notice
[empserver] / src / lib / common / game.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2017, 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 "game.h"
47 #include "optlist.h"
48 #include "server.h"
49
50 /*
51  * Enable / disable updates
52  */
53 void
54 game_ctrl_update(int enable)
55 {
56     struct gamestr *game = getgamep();
57
58     game->game_upd_disable = !enable;
59     putgame();
60 }
61
62 /*
63  * Are updates disabled?
64  */
65 int
66 updates_disabled(void)
67 {
68     return getgamep()->game_upd_disable;
69 }
70
71 /*
72  * Enable / disable play
73  */
74 void
75 game_ctrl_play(int enable)
76 {
77     struct gamestr *game = getgamep();
78
79     game->game_down = !enable;
80     putgame();
81 }
82
83 /*
84  * Is playing enabled?
85  */
86 int
87 game_play_disabled(void)
88 {
89     return getgamep()->game_down;
90 }
91
92 /*
93  * Notice that a player broke sanctuary.
94  * This starts the Empire clock if it hasn't been started yet.
95  */
96 void
97 game_note_bsanct(void)
98 {
99     struct gamestr *game = getgamep();
100
101     if (game->game_rt == 0) {
102         game->game_rt = time(NULL);
103         putgame();
104     }
105 }
106
107 /*
108  * Record an update in the game file, the current time is @now.
109  * This starts the Empire clock if it hasn't been started yet.
110  */
111 void
112 game_record_update(time_t now)
113 {
114     struct gamestr *game = getgamep();
115
116     game->game_turn++;
117     game->game_tick = 0;
118     game->game_rt = now;
119     putgame();
120 }
121
122 /*
123  * Return current duration of an ETU in seconds.
124  * Note: may return HUGE_VAL when the Empire clock is not ticking.
125  */
126 static double
127 secs_per_etu(struct gamestr *game)
128 {
129     double secs;
130
131     if (!game->game_rt || !update_time[0])
132         return HUGE_VAL;        /* not started or no update scheduled */
133
134     secs = update_time[0] - game->game_rt;
135     if (secs < 0)
136         return HUGE_VAL;        /* update seems to be late */
137     return secs / (etu_per_update - game->game_tick);
138 }
139
140 /*
141  * Update the Empire clock according to the current real time.
142  * Return the game struct.
143  */
144 struct gamestr *
145 game_tick_tick(void)
146 {
147     struct gamestr *game = getgamep();
148     double dsecs, s_p_etu;
149     int detu;
150
151     dsecs = time(NULL) - game->game_rt;
152     if (CANT_HAPPEN(dsecs < 0))
153         dsecs = 0;
154     s_p_etu = secs_per_etu(game);
155     detu = (int)(dsecs / s_p_etu);
156     if (detu > 0) {
157         if (CANT_HAPPEN(game->game_tick + detu > etu_per_update))
158             detu = etu_per_update - game->game_tick;
159         game->game_tick += detu;
160         game->game_rt += detu * s_p_etu;
161         putgame();
162     }
163
164     return game;
165 }
166
167 /*
168  * Set ETU timestamp *@tick to the current ETU time.
169  * Return by how many ETUs it was increased.
170  */
171 int
172 game_tick_to_now(short *tick)
173 {
174     return game_step_a_tick(game_tick_tick(), tick);
175 }
176
177 /*
178  * Set ETU timestamp *@tick to the ETU time recorded in the game struct.
179  * The Empire clock is not updated.
180  * Return by how many ETUs it was increased.
181  */
182 int
183 game_step_a_tick(struct gamestr *game, short *tick)
184 {
185     int etu;
186
187     etu = game->game_tick - *tick;
188     if (CANT_HAPPEN(etu < 0))
189         etu = 0;
190     *tick = game->game_tick;
191     return etu;
192 }
193
194 /*
195  * Reset ETU timestamp *@tick to zero.
196  * Return how many ETUs it had left until etu_per_update.
197  */
198 int
199 game_reset_tick(short *tick)
200 {
201     int etu;
202
203     etu = etu_per_update - *tick;
204     if (CANT_HAPPEN(etu < 0))
205         etu = 0;
206     *tick = 0;
207     return etu;
208 }