2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2007, Dave Pare, Jeff Bailey, Thomas Ruschak,
4 * Ken Stevens, Steve McClure
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.
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.
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
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.
28 * game.c: Game file access
30 * Known contributors to this file:
31 * Markus Armbruster, 2007
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
50 #include "prototypes.h"
57 game_ctrl_update(int enable)
59 struct gamestr *game = getgamep();
61 game->game_upd_disable = !enable;
66 * Are updates disabled?
69 updates_disabled(void)
71 return getgamep()->game_upd_disable;
75 * Record an update in the game file, the current time is NOW.
78 game_record_update(time_t now)
80 struct gamestr *game = getgamep();
89 * Return current duration of an ETU in seconds.
90 * Note: may return HUGE_VAL when the Empire clock is not ticking.
93 secs_per_etu(struct gamestr *game)
98 return HUGE_VAL; /* no update scheduled */
100 secs = update_time[0] - game->game_rt;
102 return HUGE_VAL; /* update seems to be late */
103 return secs / (etu_per_update - game->game_tick);
107 * Update the Empire clock according to the current real time.
108 * Return the game struct.
113 struct gamestr *game = getgamep();
114 double dsecs, s_p_etu;
117 dsecs = time(NULL) - game->game_rt;
118 if (CANT_HAPPEN(dsecs < 0))
120 s_p_etu = secs_per_etu(game);
121 detu = (int)(dsecs / s_p_etu);
123 if (CANT_HAPPEN(game->game_tick + detu > etu_per_update))
124 detu = etu_per_update - game->game_tick;
125 game->game_tick += detu;
126 game->game_rt += detu * s_p_etu;
134 * Set ETU timestamp *TICK to the current ETU time.
135 * Return by how many ETUs it was increased.
138 game_tick_to_now(short *tick)
140 return game_step_a_tick(game_tick_tick(), tick);
144 * Set ETU timestamp *TICK to the ETU time recorded in the game struct.
145 * The Empire clock is not updated.
146 * Return by how many ETUs it was increased.
149 game_step_a_tick(struct gamestr *game, short *tick)
153 etu = game->game_tick - *tick;
154 if (CANT_HAPPEN(etu < 0))
156 *tick = game->game_tick;
161 * Reset ETU timestamp *TICK to zero.
162 * Return how many ETUs it had left until etu_per_update.
165 game_reset_tick(short *tick)
169 etu = etu_per_update - *tick;
170 if (CANT_HAPPEN(etu < 0))