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) update_policy = UDP_DEFAULT;
61 if (update_policy > UDP_MAX) update_policy = UDP_DEFAULT;
62 if (update_demandpolicy < 0) update_demandpolicy = UDDEM_DEFAULT;
63 if (update_demandpolicy > UDDEM_MAX)
64 update_demandpolicy = UDDEM_DEFAULT;
65 if (update_wantmin < 0) update_wantmin = 0;
66 if (update_wantmin > MAXNOC) update_wantmin = MAXNOC;
67 if (blitz_time < 0) blitz_time = 0;
71 demand_update_time(time_t *now)
77 extern s_char *update_demandtimes;
80 curtime = tm->tm_min + tm->tm_hour * 60;
82 if (*p == 0) return(1);
83 while (NULL != (p = kw_parse(CF_TIMERANGE, p, &hour[0]))) {
84 if (curtime >= hour[0] && curtime < hour[1])
90 /* When is the next regularly scheduled update from now. */
92 regular_update_time(time_t *now, time_t *tim, time_t *delta)
95 extern int etu_per_update;
96 extern int adj_update;
101 tw = *now + adj_update;
102 secs_per_update = etu_per_update * s_p_etu;
103 *delta = secs_per_update - (tw % secs_per_update);
104 *tim = *now + *delta;
107 /* Is this a valid time for a scheduled update. */
109 scheduled_update_time(time_t *now, int *which)
116 extern s_char *update_times;
120 if (*p == 0) return(0);
123 curtime = tm->tm_min + tm->tm_hour * 60;
124 while (NULL != (p = kw_parse(CF_TIME, p, &hour))) {
126 if (curtime >= hour &&
127 curtime < hour+hourslop)
135 next_scheduled_time(time_t *now, time_t *tim, time_t *delta)
142 extern s_char *update_times;
145 if (*p == 0) return(0);
148 curtime = tm->tm_min + tm->tm_hour * 60; /* now - in minutes */
149 mintime = curtime + 24*60+1; /* start with now + 1 day */
150 while (NULL != (p = kw_parse(CF_TIME, p, &hour))) {
152 hour += 24 * 60; /* this must be tomorrow */
154 mintime = hour; /* this is best bet so far */
156 *delta = 60*(mintime - curtime);
157 *tim = *now + *delta - tm->tm_sec;
162 demand_update_want(int *want, int *pop, int which)
170 whichwants = totpop = totwant = 0;
171 for (cn = 1; 0 != (natp = getnatp(cn)); cn++) {
172 /* Only countries which are normal. */
173 /* Should probably include sanctuaries ..... */
174 if (((natp->nat_stat & NORM) == NORM) &&
175 ((natp->nat_stat & GOD) != GOD)) {
177 if ((natp->nat_update & WUPD_WANT) == WUPD_WANT) {
192 extern int update_wantmin,update_missed;
194 int want, pop, cn, veto;
197 extern long last_demand_update;
202 if (last_demand_update == 0){
204 last_demand_update = natp->nat_reserve;
207 logerror("last_demand_update = %d\n",last_demand_update);
208 logerror("update_between = %d\n",update_between());
209 logerror("now = %d\n",cur);
210 diff = (cur-(last_demand_update + update_between()));
211 logerror("diff = %d\n",diff);
213 logerror("Forced update!\n");
214 last_demand_update = cur;
215 for (cn = 1; natp = getnatp(cn); cn++){
216 if (((natp->nat_stat & NORM) == NORM) &&
217 ((natp->nat_stat & GOD) != GOD)){
218 natp->nat_missed = 0;
224 logerror("No forced update!\n");
226 if (0 == update_wantmin) {
227 logerror("no demand update allowed, wantmin = 0");
231 demand_update_want(&want, &pop, 0);
232 if (want < update_wantmin) {
233 logerror("no demand update, want = %d, min = %d",
234 want, update_wantmin);
239 if (!demand_update_time(&now)) {
240 logerror("no demand update, not within hours allowed.");
246 for (cn = 1; 0 != (natp = getnatp(cn)); cn++){
247 if (((natp->nat_stat & NORM) == NORM) &&
248 ((natp->nat_stat & GOD) != GOD)){
249 if (natp->nat_missed >= update_missed)
255 logerror("no demand update, %d has missed more than %d updates",
256 veto-1, update_missed);
260 last_demand_update = cur;
262 /* A dumb way to do it, but simple */
263 last_demand_update = natp->nat_reserve;
267 /* Check if enough countries want an update,
268 * and if demand updates are allowed now.
271 demandupdatecheck(void)
273 extern int update_demandpolicy;
275 if (UDDEM_COMSET != update_demandpolicy) {
276 logerror("no demand update, not policy.");
280 return(demand_check());
283 /* Is it time for a regular or scheduled update?
284 * As well, if none of the above, check to see if
285 * a demand update can occur.
288 updatetime(time_t *now)
290 extern int update_policy;
293 if (opt_BLITZ && update_policy == UDP_BLITZ) {
294 logerror("BLITZ Update.");
298 if (UDP_NORMAL == update_policy) {
299 logerror("Regular update, etu type.");
303 if (UDP_TIMES == update_policy) {
304 if (scheduled_update_time(now, &which)) {
305 logerror("Scheduled update, %d.", which);
309 if (opt_DEMANDUPDATE) {
310 if (demand_check()) {
311 logerror("Demand update, at check time.");
318 /* Return the time, and delta seconds, of the next update.
319 * If the policy is no regular updates, return the time of
320 * the next possible check.
323 next_update_time(time_t *now, time_t *tim, time_t *delta)
325 /* Time of next update */
326 /* Seconds till next update */
328 extern int update_policy;
330 extern int blitz_time;
332 switch (update_policy) {
334 regular_update_time(now, tim, delta);
337 if (!next_scheduled_time(now, tim, delta))
338 regular_update_time(now, tim, delta);
341 *delta = (blitz_time * 60) - (*now % (blitz_time * 60));
342 *tim = *now + *delta;
347 regular_update_time(now, tim, delta);
348 if (next_scheduled_time(now, &stim, &sdelta)) {
349 if (*delta > sdelta) {
359 next_update_check_time(time_t *now, time_t *tim, time_t *delta)
361 /* Time of next update */
362 /* Seconds till next update check */
364 extern int update_policy;
366 extern int blitz_time;
368 switch (update_policy) {
370 regular_update_time(now, tim, delta);
373 *delta = (blitz_time * 60) - (*now % (blitz_time * 60));
374 *tim = *now + *delta;
379 regular_update_time(now, tim, delta);
380 if (next_scheduled_time(now, &stim, &sdelta)) {
381 if (*delta > sdelta) {
390 updates_disabled(void)
392 extern s_char *disablefil;
395 if ((fd = open(disablefil, O_RDONLY, 0)) < 0)