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