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