2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2004, 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 the "LEGAL", "LICENSE", "CREDITS" and "README" files for all the
23 * related information and legal notices. It is expected that any future
24 * 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:
45 #include <sys/types.h>
50 update_policy_check(void)
52 if (update_policy < 0)
53 update_policy = UDP_DEFAULT;
54 if (update_policy > UDP_MAX)
55 update_policy = UDP_DEFAULT;
56 if (update_demandpolicy < 0)
57 update_demandpolicy = UDDEM_DEFAULT;
58 if (update_demandpolicy > UDDEM_MAX)
59 update_demandpolicy = UDDEM_DEFAULT;
60 if (update_wantmin < 0)
62 if (update_wantmin > MAXNOC)
63 update_wantmin = MAXNOC;
69 demand_update_time(time_t *now)
74 return is_daytime_allowed(60 * tm->tm_hour + tm->tm_min,
78 /* When is the next regularly scheduled update from now. */
80 regular_update_time(time_t *now, time_t *tim, time_t *delta)
85 tw = *now + adj_update;
86 secs_per_update = etu_per_update * s_p_etu;
87 *delta = secs_per_update - (tw % secs_per_update);
91 /* Is this a valid time for a scheduled update. */
93 scheduled_update_time(time_t *now)
98 return is_daytime_near(60 * tm->tm_hour + tm->tm_min,
99 update_times, hourslop);
103 next_scheduled_time(time_t *now, time_t *tim, time_t *delta)
109 d = min_to_next_daytime(60 * tm->tm_hour + tm->tm_min, update_times);
113 *tim = *now + *delta - tm->tm_sec;
118 demand_update_want(int *want, int *pop, int which)
126 whichwants = totpop = totwant = 0;
127 for (cn = 1; 0 != (natp = getnatp(cn)); cn++) {
128 /* Only countries which are normal. */
129 /* Should probably include sanctuaries ..... */
130 if (((natp->nat_stat & NORM) == NORM) &&
131 ((natp->nat_stat & GOD) != GOD)) {
133 if ((natp->nat_update & WUPD_WANT) == WUPD_WANT) {
149 int want, pop, cn, veto;
155 if (0 == update_wantmin) {
156 logerror("no demand update allowed, wantmin = 0");
160 demand_update_want(&want, &pop, 0);
161 if (want < update_wantmin) {
162 logerror("no demand update, want = %d, min = %d",
163 want, update_wantmin);
168 if (!demand_update_time(&now)) {
169 logerror("no demand update, not within hours allowed.");
175 for (cn = 1; 0 != (natp = getnatp(cn)); cn++) {
176 if (((natp->nat_stat & NORM) == NORM) &&
177 ((natp->nat_stat & GOD) != GOD)) {
178 if (natp->nat_missed >= update_missed)
184 logerror("no demand update, %d has missed more than %d updates",
185 veto - 1, update_missed);
192 /* Check if enough countries want an update,
193 * and if demand updates are allowed now.
196 demandupdatecheck(void)
198 if (UDDEM_COMSET != update_demandpolicy) {
199 logerror("no demand update, not policy.");
203 return (demand_check());
206 /* Is it time for a regular or scheduled update?
207 * As well, if none of the above, check to see if
208 * a demand update can occur.
211 updatetime(time_t *now)
213 if (opt_BLITZ && update_policy == UDP_BLITZ) {
214 logerror("BLITZ Update.");
218 if (UDP_NORMAL == update_policy) {
219 logerror("Regular update, etu type.");
223 if (UDP_TIMES == update_policy) {
224 if (scheduled_update_time(now)) {
225 logerror("Scheduled update.");
229 if (opt_DEMANDUPDATE) {
230 if (demand_check()) {
231 logerror("Demand update, at check time.");
238 /* Return the time, and delta seconds, of the next update.
239 * If the policy is no regular updates, return the time of
240 * the next possible check.
243 next_update_time(time_t *now, time_t *tim, time_t *delta)
245 /* Time of next update */
246 /* Seconds till next update */
250 switch (update_policy) {
252 regular_update_time(now, tim, delta);
255 if (!next_scheduled_time(now, tim, delta))
256 regular_update_time(now, tim, delta);
259 *delta = (blitz_time * 60) - (*now % (blitz_time * 60));
260 *tim = *now + *delta;
265 regular_update_time(now, tim, delta);
266 if (next_scheduled_time(now, &stim, &sdelta)) {
267 if (*delta > sdelta) {
277 next_update_check_time(time_t *now, time_t *tim, time_t *delta)
279 /* Time of next update */
280 /* Seconds till next update check */
284 switch (update_policy) {
286 regular_update_time(now, tim, delta);
289 *delta = (blitz_time * 60) - (*now % (blitz_time * 60));
290 *tim = *now + *delta;
295 regular_update_time(now, tim, delta);
296 if (next_scheduled_time(now, &stim, &sdelta)) {
297 if (*delta > sdelta) {
306 updates_disabled(void)
310 if ((fd = open(disablefil, O_RDONLY, 0)) < 0)