2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2000, 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:
46 #include <sys/types.h>
51 update_policy_check(void)
53 if (update_policy < 0)
54 update_policy = UDP_DEFAULT;
55 if (update_policy > UDP_MAX)
56 update_policy = UDP_DEFAULT;
57 if (update_demandpolicy < 0)
58 update_demandpolicy = UDDEM_DEFAULT;
59 if (update_demandpolicy > UDDEM_MAX)
60 update_demandpolicy = UDDEM_DEFAULT;
61 if (update_wantmin < 0)
63 if (update_wantmin > MAXNOC)
64 update_wantmin = MAXNOC;
70 demand_update_time(time_t * now)
78 curtime = tm->tm_min + tm->tm_hour * 60;
79 p = update_demandtimes;
82 while (NULL != (p = kw_parse(CF_TIMERANGE, p, &hour[0]))) {
83 if (curtime >= hour[0] && curtime < hour[1])
89 /* When is the next regularly scheduled update from now. */
91 regular_update_time(time_t * now, time_t * tim, time_t * delta)
97 tw = *now + adj_update;
98 secs_per_update = etu_per_update * s_p_etu;
99 *delta = secs_per_update - (tw % secs_per_update);
100 *tim = *now + *delta;
103 /* Is this a valid time for a scheduled update. */
105 scheduled_update_time(time_t * now, int *which)
118 curtime = tm->tm_min + tm->tm_hour * 60;
119 while (NULL != (p = kw_parse(CF_TIME, p, &hour))) {
121 if (curtime >= hour && curtime < hour + hourslop)
129 next_scheduled_time(time_t * now, time_t * tim, time_t * delta)
142 curtime = tm->tm_min + tm->tm_hour * 60; /* now - in minutes */
143 mintime = curtime + 24 * 60 + 1; /* start with now + 1 day */
144 while (NULL != (p = kw_parse(CF_TIME, p, &hour))) {
146 hour += 24 * 60; /* this must be tomorrow */
148 mintime = hour; /* this is best bet so far */
150 *delta = 60 * (mintime - curtime);
151 *tim = *now + *delta - tm->tm_sec;
156 demand_update_want(int *want, int *pop, int which)
164 whichwants = totpop = totwant = 0;
165 for (cn = 1; 0 != (natp = getnatp(cn)); cn++) {
166 /* Only countries which are normal. */
167 /* Should probably include sanctuaries ..... */
168 if (((natp->nat_stat & NORM) == NORM) &&
169 ((natp->nat_stat & GOD) != GOD)) {
171 if ((natp->nat_update & WUPD_WANT) == WUPD_WANT) {
187 int want, pop, cn, veto;
194 if (last_demand_update == 0){
196 last_demand_update = natp->nat_reserve;
199 logerror("last_demand_update = %d\n",last_demand_update);
200 logerror("update_between = %d\n",update_between());
201 logerror("now = %d\n",cur);
202 diff = (cur-(last_demand_update + update_between()));
203 logerror("diff = %d\n",diff);
205 logerror("Forced update!\n");
206 last_demand_update = cur;
207 for (cn = 1; natp = getnatp(cn); cn++){
208 if (((natp->nat_stat & NORM) == NORM) &&
209 ((natp->nat_stat & GOD) != GOD)){
210 natp->nat_missed = 0;
216 logerror("No forced update!\n");
218 if (0 == update_wantmin) {
219 logerror("no demand update allowed, wantmin = 0");
223 demand_update_want(&want, &pop, 0);
224 if (want < update_wantmin) {
225 logerror("no demand update, want = %d, min = %d",
226 want, update_wantmin);
231 if (!demand_update_time(&now)) {
232 logerror("no demand update, not within hours allowed.");
238 for (cn = 1; 0 != (natp = getnatp(cn)); cn++) {
239 if (((natp->nat_stat & NORM) == NORM) &&
240 ((natp->nat_stat & GOD) != GOD)) {
241 if (natp->nat_missed >= update_missed)
247 logerror("no demand update, %d has missed more than %d updates",
248 veto - 1, update_missed);
252 last_demand_update = cur;
254 /* A dumb way to do it, but simple */
255 last_demand_update = natp->nat_reserve;
259 /* Check if enough countries want an update,
260 * and if demand updates are allowed now.
263 demandupdatecheck(void)
265 if (UDDEM_COMSET != update_demandpolicy) {
266 logerror("no demand update, not policy.");
270 return (demand_check());
273 /* Is it time for a regular or scheduled update?
274 * As well, if none of the above, check to see if
275 * a demand update can occur.
278 updatetime(time_t * now)
282 if (opt_BLITZ && update_policy == UDP_BLITZ) {
283 logerror("BLITZ Update.");
287 if (UDP_NORMAL == update_policy) {
288 logerror("Regular update, etu type.");
292 if (UDP_TIMES == update_policy) {
293 if (scheduled_update_time(now, &which)) {
294 logerror("Scheduled update, %d.", which);
298 if (opt_DEMANDUPDATE) {
299 if (demand_check()) {
300 logerror("Demand update, at check time.");
307 /* Return the time, and delta seconds, of the next update.
308 * If the policy is no regular updates, return the time of
309 * the next possible check.
312 next_update_time(time_t * now, time_t * tim, time_t * delta)
314 /* Time of next update */
315 /* Seconds till next update */
319 switch (update_policy) {
321 regular_update_time(now, tim, delta);
324 if (!next_scheduled_time(now, tim, delta))
325 regular_update_time(now, tim, delta);
328 *delta = (blitz_time * 60) - (*now % (blitz_time * 60));
329 *tim = *now + *delta;
334 regular_update_time(now, tim, delta);
335 if (next_scheduled_time(now, &stim, &sdelta)) {
336 if (*delta > sdelta) {
346 next_update_check_time(time_t * now, time_t * tim, time_t * delta)
348 /* Time of next update */
349 /* Seconds till next update check */
353 switch (update_policy) {
355 regular_update_time(now, tim, delta);
358 *delta = (blitz_time * 60) - (*now % (blitz_time * 60));
359 *tim = *now + *delta;
364 regular_update_time(now, tim, delta);
365 if (next_scheduled_time(now, &stim, &sdelta)) {
366 if (*delta > sdelta) {
375 updates_disabled(void)
379 if ((fd = open(disablefil, O_RDONLY, 0)) < 0)