]> git.pond.sub.org Git - empserver/blob - src/server/timestamp.c
Update copyright notice.
[empserver] / src / server / timestamp.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2004, 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 "empthread.h"
39 #include <stdio.h>
40 #include "prototypes.h"
41 #include "optlist.h"
42 #include "server.h"
43
44 /*ARGSUSED*/
45 void
46 mobility_check(void *unused)
47 {
48     struct mob_acc_globals timestamps;
49     time_t now;
50     FILE *fp;
51
52     while (1) {
53         time(&now);
54 /*              logerror("Updating timestamp file at %s", ctime(&now));*/
55 #if !defined(_WIN32)
56         if ((fp = fopen(timestampfil, "r+")) == NULL) {
57 #else
58         if ((fp = fopen(timestampfil, "r+b")) == NULL) {
59 #endif
60             logerror("Unable to edit timestamp file.");
61             continue;
62         }
63         rewind(fp);
64         fread(&timestamps, sizeof(timestamps), 1, fp);
65         timestamps.timestamp = now;
66         rewind(fp);
67         fwrite(&timestamps, sizeof(timestamps), 1, fp);
68         fclose(fp);
69         if (!gamehours(now)) {
70             if (updating_mob == 1) {
71                 update_all_mob();
72                 logerror("Turning off mobility updating (gamehours).");
73                 updating_mob = 0;
74             }
75         } else if (updating_mob == 1 && now < timestamps.starttime) {
76             logerror("Turning off mobility updating at %s", ctime(&now));
77             update_all_mob();
78             updating_mob = 0;
79         } else if (updating_mob == 0 && now >= timestamps.starttime) {
80             logerror("Turning on mobility updating at %s", ctime(&now));
81             update_all_mob();
82             updating_mob = 1;
83         }
84         now = now + 180;        /* Every 3 minutes */
85         empth_sleep(now);
86
87     }
88     /*NOTREACHED*/
89 }
90
91 void
92 mobility_init(void)
93 {
94     struct mob_acc_globals timestamps;
95     time_t now;
96     time_t lastsavedtime;
97     FILE *fp;
98
99     /* During downtime, we don't want mobility to accrue.  So, we look
100        at the timestamp file, and determine how far forward to push
101        mobility */
102
103     time(&now);
104 #if !defined(_WIN32)
105     if ((fp = fopen(timestampfil, "r+")) == NULL) {
106 #else
107     if ((fp = fopen(timestampfil, "r+b")) == NULL) {
108 #endif
109         logerror("Unable to edit timestamp file.");
110         /* FIXME safe to continue? */
111     } else {
112         rewind(fp);
113         fread(&timestamps, sizeof(timestamps), 1, fp);
114         lastsavedtime = timestamps.timestamp;
115         timestamps.timestamp = now;
116         rewind(fp);
117         fwrite(&timestamps, sizeof(timestamps), 1, fp);
118         fclose(fp);
119     }
120     time(&now);
121     logerror("Adjusting timestamps at %s", ctime(&now));
122     logerror("(was %s)", ctime(&lastsavedtime));
123     /* Update the timestamps to this point in time */
124     update_timestamps(lastsavedtime);
125     time(&now);
126     logerror("Done at %s", ctime(&now));
127
128     if (now >= timestamps.starttime && gamehours(now)) {
129         logerror("Turning on mobility updating.");
130         updating_mob = 1;
131     } else {
132         logerror("Turning off mobility updating.");
133         updating_mob = 0;
134     }
135 }