]> git.pond.sub.org Git - empserver/blob - src/server/update.c
Supply prototypes where possible. This uncovered type errors with
[empserver] / src / server / update.c
1 /*
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
5  *
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.
10  *
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.
15  *
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
19  *
20  *  ---
21  *
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.
25  *
26  *  ---
27  *
28  *  update.c: Update scheduler
29  * 
30  *  Known contributors to this file:
31  *     Dave Pare, 1994
32  *     Steve McClure, 1996
33  */
34
35 #include "misc.h"
36 #include "player.h"
37 #include "keyword.h"
38 #include "empthread.h"
39 #include "prototypes.h"
40 #include "optlist.h"
41
42 empth_sem_t *update_sem;
43
44 extern void update_main(void *);
45 extern void update_wait(void *argv);
46 time_t update_time;
47
48 /*ARGSUSED*/
49 void
50 update_sched(void *argv)
51 {
52     extern int s_p_etu;
53     extern int etu_per_update;
54     extern int adj_update;
55     extern int update_window;
56     s_char *kw;
57     int hour[2];
58     int wind;
59     time_t now, delta;
60
61     update_sem = empth_sem_create("Update", 0);
62     empth_create(PP_SCHED, update_wait, (50 * 1024), 0, "UpdateWait",
63                  "Waits until players idle", 0);
64     time(&now);
65     (void)gamehours(now, hour);
66     if (NULL != (kw = kw_find("s_p_etu")))
67         kw_parse(CF_VALUE, kw, &s_p_etu);
68     if (NULL != (kw = kw_find("etu_per_update")))
69         kw_parse(CF_VALUE, kw, &etu_per_update);
70     if (NULL != (kw = kw_find("adj_update")))
71         kw_parse(CF_VALUE, kw, &adj_update);
72     if (NULL != (kw = kw_find("update_window")))
73         kw_parse(CF_VALUE, kw, &update_window);
74     if (s_p_etu <= 0) {
75         logerror("bad value for s_p_etu (%d)", s_p_etu);
76         s_p_etu = 2 * 60;
77         logerror("setting s_p_etu to %d", s_p_etu);
78     }
79     while (1) {
80         time(&now);
81         next_update_time(&now, &update_time, &delta);
82         if (update_window > 0) {
83             wind = (random() % update_window);
84             update_time += wind;
85             delta += wind;
86         }
87         logerror("Next update at %s", ctime(&update_time));
88         logerror("Next update in %d seconds", delta);
89         /* sleep until update is scheduled to go off */
90         empth_sleep(update_time);
91         time(&now);
92         now += adj_update;
93         if (!gamehours(now, hour)) {
94             logerror("No update permitted (hours restriction)");
95             continue;
96         }
97         if (!updatetime(&now)) {
98             logerror("No update wanted");
99             continue;
100         }
101         if (updates_disabled()) {
102             logerror("Updates disabled...skipping update");
103             continue;
104         }
105         empth_sem_signal(update_sem);
106     }
107     /*NOTREACHED*/
108 }
109
110 /*ARGSUSED*/
111 void
112 update_wait(void *argv)
113 {
114     struct player *p;
115     int running;
116     time_t now;
117     int stacksize;
118     struct player *dp;
119
120     while (1) {
121         empth_sem_wait(update_sem);
122         running = 0;
123         for (p = player_next(0); p != 0; p = player_next(p)) {
124             if (p->state != PS_PLAYING)
125                 continue;
126             if (p->command) {
127                 pr_flash(p, "Update aborting command\n");
128                 p->aborted = 1;
129                 empth_wakeup(p->proc);
130                 running++;
131             }
132         }
133         time(&now);
134         if (running) {
135             /* sleep a few, wait for aborts to take effect */
136             empth_sleep(now + 2);
137         }
138         /* 
139          * we rely on the fact that update's priority is the highest
140          * in the land so it can finish before it yields.
141          */
142         dp = player_new(0, 0);
143         if (!dp) {
144             logerror("can't create dummy player for update");
145             continue;
146         }
147         stacksize = 100000 +
148 /* finish_sects */ WORLD_X * WORLD_Y * (2 * sizeof(double) +
149                                         sizeof(s_char *));
150
151         empth_create(PP_UPDATE, update_main, stacksize, 0,
152                      "UpdateRun", "Updates the world", dp);
153     }
154     /*NOTREACHED*/
155 }