]> git.pond.sub.org Git - empserver/blob - src/server/timestamp.c
Declare all configuration parameters in optlist.h. Remove some
[empserver] / src / server / timestamp.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  *  timestamp.c: Timestamp writer/maintainer thread
29  * 
30  *  Known contributors to this file:
31  *     Steve McClure, 1996
32  *     Doug Hay, 1998
33  */
34
35 #include "options.h"
36 #include "misc.h"
37 #include "player.h"
38 #include "keyword.h"
39 #include "empthread.h"
40 #include <stdio.h>
41 #include "prototypes.h"
42 #include "optlist.h"
43
44 /*ARGSUSED*/
45 void
46 mobility_check(void *argv)
47 {
48     extern int updating_mob;
49     struct mob_acc_globals timestamps;
50     time_t now;
51     FILE *fp;
52     int hour[2];
53
54     while (1) {
55         time(&now);
56 /*              logerror("Updating timestamp file at %s", ctime(&now));*/
57 #if !defined(_WIN32)
58         if ((fp = fopen(timestampfil, "r+")) == NULL) {
59 #else
60         if ((fp = fopen(timestampfil, "r+b")) == NULL) {
61 #endif
62             logerror("Unable to edit timestamp file.");
63             continue;
64         }
65         rewind(fp);
66         fread(&timestamps, sizeof(timestamps), 1, fp);
67         timestamps.timestamp = now;
68         rewind(fp);
69         fwrite(&timestamps, sizeof(timestamps), 1, fp);
70         fclose(fp);
71         if (!gamehours(now, hour)) {
72             if (updating_mob == 1) {
73                 update_all_mob();
74                 logerror("Turning off mobility updating (gamehours).");
75                 updating_mob = 0;
76             }
77         } else if (updating_mob == 1 && now < timestamps.starttime) {
78             logerror("Turning off mobility updating at %s", ctime(&now));
79             update_all_mob();
80             updating_mob = 0;
81         } else if (updating_mob == 0 && now >= timestamps.starttime) {
82             logerror("Turning on mobility updating at %s", ctime(&now));
83             update_all_mob();
84             updating_mob = 1;
85         }
86         now = now + 180;        /* Every 3 minutes */
87         empth_sleep(now);
88
89     }
90     /*NOTREACHED*/
91 }
92
93 void
94 mobility_init(void)
95 {
96     extern int updating_mob;
97     struct mob_acc_globals timestamps;
98     time_t now;
99     time_t lastsavedtime;
100     FILE *fp;
101     int hour[2];
102
103     /* During downtime, we don't want mobility to accrue.  So, we look
104        at the timestamp file, and determine how far forward to push
105        mobility */
106
107     time(&now);
108 #if !defined(_WIN32)
109     if ((fp = fopen(timestampfil, "r+")) == NULL) {
110 #else
111     if ((fp = fopen(timestampfil, "r+b")) == NULL) {
112 #endif
113         logerror("Unable to edit timestamp file.");
114     } else {
115         rewind(fp);
116         fread(&timestamps, sizeof(timestamps), 1, fp);
117         lastsavedtime = timestamps.timestamp;
118         timestamps.timestamp = now;
119         rewind(fp);
120         fwrite(&timestamps, sizeof(timestamps), 1, fp);
121         fclose(fp);
122     }
123     time(&now);
124     logerror("Adjusting timestamps at %s", ctime(&now));
125     logerror("(was %s)", ctime(&lastsavedtime));
126     /* Update the timestamps to this point in time */
127     update_timestamps(lastsavedtime);
128     time(&now);
129     logerror("Done at %s", ctime(&now));
130
131     if (now >= timestamps.starttime && gamehours(now, hour)) {
132         logerror("Turning on mobility updating.");
133         updating_mob = 1;
134     } else {
135         logerror("Turning off mobility updating.");
136         updating_mob = 0;
137     }
138 }