2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2006, 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 * wantupd.c: Check to se if an update is wanted and/or allowed.
30 * Known contributors to this file:
38 #if defined(_WIN32) && !defined(__GNUC__)
49 #include "prototypes.h"
53 update_policy_check(void)
55 if (update_policy < 0)
56 update_policy = UDP_DEFAULT;
57 if (update_policy > UDP_MAX)
58 update_policy = UDP_DEFAULT;
59 if (update_demandpolicy < 0)
60 update_demandpolicy = UDDEM_DEFAULT;
61 if (update_demandpolicy > UDDEM_MAX)
62 update_demandpolicy = UDDEM_DEFAULT;
63 if (update_wantmin < 1)
65 if (update_wantmin > MAXNOC)
66 update_wantmin = MAXNOC;
72 demand_update_time(time_t *now)
77 return is_daytime_allowed(60 * tm->tm_hour + tm->tm_min,
81 /* When is the next regularly scheduled update from now. */
83 regular_update_time(time_t *now, time_t *tim, time_t *delta)
88 tw = *now + adj_update;
89 secs_per_update = etu_per_update * s_p_etu;
90 *delta = secs_per_update - (tw % secs_per_update);
94 /* Is this a valid time for a scheduled update. */
96 scheduled_update_time(time_t *now)
101 return is_daytime_near(60 * tm->tm_hour + tm->tm_min,
102 update_times, hourslop);
106 next_scheduled_time(time_t *now, time_t *tim, time_t *delta)
112 d = min_to_next_daytime(60 * tm->tm_hour + tm->tm_min, update_times);
116 *tim = *now + *delta - tm->tm_sec;
121 demand_update_want(int *want, int *pop, int which)
129 whichwants = totpop = totwant = 0;
130 for (cn = 1; 0 != (natp = getnatp(cn)); cn++) {
131 /* Only countries which are normal. */
132 /* Should probably include sanctuaries ..... */
133 if (natp->nat_stat == STAT_ACTIVE) {
135 if ((natp->nat_update & WUPD_WANT) == WUPD_WANT) {
151 int want, pop, cn, veto;
157 demand_update_want(&want, &pop, 0);
158 if (want < update_wantmin) {
159 logerror("no demand update, want = %d, min = %d",
160 want, update_wantmin);
165 if (!demand_update_time(&now)) {
166 logerror("no demand update, not within hours allowed.");
172 for (cn = 1; 0 != (natp = getnatp(cn)); cn++) {
173 if (natp->nat_stat == STAT_ACTIVE) {
174 if (natp->nat_missed >= update_missed)
180 logerror("no demand update, %d has missed more than %d updates",
181 veto, update_missed);
189 * Check if enough countries want an update,
190 * and if demand updates are allowed now.
193 demandupdatecheck(void)
195 if (UDDEM_COMSET != update_demandpolicy) {
196 logerror("no demand update, not policy.");
200 return demand_check();
204 * Is it time for a regular or scheduled update?
205 * As well, if none of the above, check to see if
206 * a demand update can occur.
209 updatetime(time_t *now)
211 if (opt_BLITZ && update_policy == UDP_BLITZ) {
212 logerror("BLITZ Update.");
216 if (UDP_NORMAL == update_policy) {
217 logerror("Regular update, etu type.");
221 if (UDP_TIMES == update_policy) {
222 if (scheduled_update_time(now)) {
223 logerror("Scheduled update.");
227 if (opt_DEMANDUPDATE) {
228 if (demand_check()) {
229 logerror("Demand update, at check time.");
237 * Return the time, and delta seconds, of the next update.
238 * If the policy is no regular updates, return the time of
239 * the next possible check.
242 next_update_time(time_t *now, time_t *tim, time_t *delta)
244 /* Time of next update */
245 /* Seconds till next update */
249 switch (update_policy) {
251 regular_update_time(now, tim, delta);
254 if (!next_scheduled_time(now, tim, delta))
255 regular_update_time(now, tim, delta);
258 *delta = (blitz_time * 60) - (*now % (blitz_time * 60));
259 *tim = *now + *delta;
263 regular_update_time(now, tim, delta);
264 if (next_scheduled_time(now, &stim, &sdelta)) {
265 if (*delta > sdelta) {
275 next_update_check_time(time_t *now, time_t *tim, time_t *delta)
277 /* Time of next update */
278 /* Seconds till next update check */
282 switch (update_policy) {
284 regular_update_time(now, tim, delta);
287 *delta = (blitz_time * 60) - (*now % (blitz_time * 60));
288 *tim = *now + *delta;
293 regular_update_time(now, tim, delta);
294 if (next_scheduled_time(now, &stim, &sdelta)) {
295 if (*delta > sdelta) {
304 updates_disabled(void)
308 if ((fd = open(disablefil, O_RDONLY, 0)) < 0)