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