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>
48 #if defined(Rel4) || defined(_WIN32)
55 update_policy_check(void)
57 extern int update_policy, update_demandpolicy;
58 extern int update_wantmin, blitz_time;
60 if (update_policy < 0)
61 update_policy = UDP_DEFAULT;
62 if (update_policy > UDP_MAX)
63 update_policy = UDP_DEFAULT;
64 if (update_demandpolicy < 0)
65 update_demandpolicy = UDDEM_DEFAULT;
66 if (update_demandpolicy > UDDEM_MAX)
67 update_demandpolicy = UDDEM_DEFAULT;
68 if (update_wantmin < 0)
70 if (update_wantmin > MAXNOC)
71 update_wantmin = MAXNOC;
77 demand_update_time(time_t * now)
83 extern s_char *update_demandtimes;
86 curtime = tm->tm_min + tm->tm_hour * 60;
87 p = update_demandtimes;
90 while (NULL != (p = kw_parse(CF_TIMERANGE, p, &hour[0]))) {
91 if (curtime >= hour[0] && curtime < hour[1])
97 /* When is the next regularly scheduled update from now. */
99 regular_update_time(time_t * now, time_t * tim, time_t * delta)
102 extern int etu_per_update;
103 extern int adj_update;
108 tw = *now + adj_update;
109 secs_per_update = etu_per_update * s_p_etu;
110 *delta = secs_per_update - (tw % secs_per_update);
111 *tim = *now + *delta;
114 /* Is this a valid time for a scheduled update. */
116 scheduled_update_time(time_t * now, int *which)
123 extern s_char *update_times;
131 curtime = tm->tm_min + tm->tm_hour * 60;
132 while (NULL != (p = kw_parse(CF_TIME, p, &hour))) {
134 if (curtime >= hour && curtime < hour + hourslop)
142 next_scheduled_time(time_t * now, time_t * tim, time_t * delta)
149 extern s_char *update_times;
156 curtime = tm->tm_min + tm->tm_hour * 60; /* now - in minutes */
157 mintime = curtime + 24 * 60 + 1; /* start with now + 1 day */
158 while (NULL != (p = kw_parse(CF_TIME, p, &hour))) {
160 hour += 24 * 60; /* this must be tomorrow */
162 mintime = hour; /* this is best bet so far */
164 *delta = 60 * (mintime - curtime);
165 *tim = *now + *delta - tm->tm_sec;
170 demand_update_want(int *want, int *pop, int which)
178 whichwants = totpop = totwant = 0;
179 for (cn = 1; 0 != (natp = getnatp(cn)); cn++) {
180 /* Only countries which are normal. */
181 /* Should probably include sanctuaries ..... */
182 if (((natp->nat_stat & NORM) == NORM) &&
183 ((natp->nat_stat & GOD) != GOD)) {
185 if ((natp->nat_update & WUPD_WANT) == WUPD_WANT) {
200 extern int update_wantmin, update_missed;
202 int want, pop, cn, veto;
205 extern long last_demand_update;
210 if (last_demand_update == 0){
212 last_demand_update = natp->nat_reserve;
215 logerror("last_demand_update = %d\n",last_demand_update);
216 logerror("update_between = %d\n",update_between());
217 logerror("now = %d\n",cur);
218 diff = (cur-(last_demand_update + update_between()));
219 logerror("diff = %d\n",diff);
221 logerror("Forced update!\n");
222 last_demand_update = cur;
223 for (cn = 1; natp = getnatp(cn); cn++){
224 if (((natp->nat_stat & NORM) == NORM) &&
225 ((natp->nat_stat & GOD) != GOD)){
226 natp->nat_missed = 0;
232 logerror("No forced update!\n");
234 if (0 == update_wantmin) {
235 logerror("no demand update allowed, wantmin = 0");
239 demand_update_want(&want, &pop, 0);
240 if (want < update_wantmin) {
241 logerror("no demand update, want = %d, min = %d",
242 want, update_wantmin);
247 if (!demand_update_time(&now)) {
248 logerror("no demand update, not within hours allowed.");
254 for (cn = 1; 0 != (natp = getnatp(cn)); cn++) {
255 if (((natp->nat_stat & NORM) == NORM) &&
256 ((natp->nat_stat & GOD) != GOD)) {
257 if (natp->nat_missed >= update_missed)
263 logerror("no demand update, %d has missed more than %d updates",
264 veto - 1, update_missed);
268 last_demand_update = cur;
270 /* A dumb way to do it, but simple */
271 last_demand_update = natp->nat_reserve;
275 /* Check if enough countries want an update,
276 * and if demand updates are allowed now.
279 demandupdatecheck(void)
281 extern int update_demandpolicy;
283 if (UDDEM_COMSET != update_demandpolicy) {
284 logerror("no demand update, not policy.");
288 return (demand_check());
291 /* Is it time for a regular or scheduled update?
292 * As well, if none of the above, check to see if
293 * a demand update can occur.
296 updatetime(time_t * now)
298 extern int update_policy;
301 if (opt_BLITZ && update_policy == UDP_BLITZ) {
302 logerror("BLITZ Update.");
306 if (UDP_NORMAL == update_policy) {
307 logerror("Regular update, etu type.");
311 if (UDP_TIMES == update_policy) {
312 if (scheduled_update_time(now, &which)) {
313 logerror("Scheduled update, %d.", which);
317 if (opt_DEMANDUPDATE) {
318 if (demand_check()) {
319 logerror("Demand update, at check time.");
326 /* Return the time, and delta seconds, of the next update.
327 * If the policy is no regular updates, return the time of
328 * the next possible check.
331 next_update_time(time_t * now, time_t * tim, time_t * delta)
333 /* Time of next update */
334 /* Seconds till next update */
336 extern int update_policy;
338 extern int blitz_time;
340 switch (update_policy) {
342 regular_update_time(now, tim, delta);
345 if (!next_scheduled_time(now, tim, delta))
346 regular_update_time(now, tim, delta);
349 *delta = (blitz_time * 60) - (*now % (blitz_time * 60));
350 *tim = *now + *delta;
355 regular_update_time(now, tim, delta);
356 if (next_scheduled_time(now, &stim, &sdelta)) {
357 if (*delta > sdelta) {
367 next_update_check_time(time_t * now, time_t * tim, time_t * delta)
369 /* Time of next update */
370 /* Seconds till next update check */
372 extern int update_policy;
374 extern int blitz_time;
376 switch (update_policy) {
378 regular_update_time(now, tim, delta);
381 *delta = (blitz_time * 60) - (*now % (blitz_time * 60));
382 *tim = *now + *delta;
387 regular_update_time(now, tim, delta);
388 if (next_scheduled_time(now, &stim, &sdelta)) {
389 if (*delta > sdelta) {
398 updates_disabled(void)
400 extern s_char *disablefil;
403 if ((fd = open(disablefil, O_RDONLY, 0)) < 0)