]> git.pond.sub.org Git - empserver/blob - src/lib/update/main.c
Provide an Empire clock with ETU resolution that is properly
[empserver] / src / lib / update / main.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2007, 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 files README, COPYING and CREDITS in the root of the source
23  *  tree for related information and legal notices.  It is expected
24  *  that future projects/authors will amend these files as needed.
25  *
26  *  ---
27  *
28  *  main.c: World update main function
29  * 
30  *  Known contributors to this file:
31  *     Dave Pare, 1994
32  *     Steve McClure, 1996
33  *     Doug Hay, 1998
34  */
35
36 #include <config.h>
37
38 #include "budg.h"
39 #include "empthread.h"
40 #include "game.h"
41 #include "journal.h"
42 #include "player.h"
43 #include "server.h"
44 #include "update.h"
45
46 long money[MAXNOC];
47 long pops[MAXNOC];
48 long sea_money[MAXNOC];
49 long lnd_money[MAXNOC];
50 long air_money[MAXNOC];
51 long tpops[MAXNOC];
52
53 void
54 update_main(void)
55 {
56     int etu = etu_per_update;
57     int n;
58     int x;
59     struct bp *bp;
60     int cn, cn2, rel;
61     struct natstr *cnp;
62     struct natstr *np;
63
64     logerror("production update (%d etus)", etu);
65     game_record_update(time(NULL));
66     journal_update(etu);
67
68     /* First, make sure all mobility is updated correctly. */
69     if (opt_MOB_ACCESS) {
70         mob_ship(etu);
71         mob_sect(etu);
72         mob_plane(etu);
73         mob_land(etu);
74     }
75
76     if (opt_AUTO_POWER)
77         update_power();
78
79     /*
80      * set up all the variables which get used in the
81      * sector production routine (for producing education,
82      * happiness, and printing out the state of the nation)
83      */
84     memset(pops, 0, sizeof(pops));
85     memset(air_money, 0, sizeof(air_money));
86     memset(sea_money, 0, sizeof(sea_money));
87     memset(lnd_money, 0, sizeof(lnd_money));
88     bp = bp_alloc();
89     for (n = 0; n < MAXNOC; n++) {
90         money[n] = 0;
91         if (!(np = getnatp(n)))
92             continue;
93         money[n] = np->nat_money;
94         tpops[n] = count_pop(n);
95     }
96
97     logerror("preparing sectors...");
98     prepare_sects(etu, bp);
99     logerror("done preparing sectors.");
100     logerror("producing for countries...");
101     for (x = 0; x < MAXNOC; x++) {
102         long p_sect[SCT_TYPE_MAX+2][2];
103
104         memset(p_sect, 0, sizeof(p_sect));
105         if (!(np = getnatp(x)))
106             continue;
107         if (np->nat_stat == STAT_SANCT) {
108 #ifdef DEBUG
109             logerror("Country %i is in sanctuary and did not update", x);
110 #endif
111             continue;
112         }
113         np->nat_money += (int)(np->nat_reserve * money_res * etu);
114
115         /* maintain units */
116         prod_ship(etu, x, bp, 0);
117         prod_plane(etu, x, bp, 0);
118         prod_land(etu, x, bp, 0);
119
120         /* produce all sects */
121         produce_sect(x, etu, bp, p_sect);
122
123         /* build units */
124         prod_ship(etu, x, bp, 1);
125         prod_plane(etu, x, bp, 1);
126         prod_land(etu, x, bp, 1);
127     }
128     logerror("done producing for countries.");
129
130     finish_sects(etu);
131     prod_nat(etu);
132     age_levels(etu);
133     free(bp);
134     /*flushwu(); */
135     if (opt_SLOW_WAR) {
136         /* Update war declarations */
137         /* MOBILIZATION->SITZKRIEG->AT_WAR */
138         for (cn = 1; cn < MAXNOC; cn++) {
139             if ((cnp = getnatp(cn)) == 0)
140                 break;
141             for (cn2 = 1; cn2 < MAXNOC; cn2++) {
142                 if (cn2 == cn)
143                     continue;
144                 rel = getrel(cnp, cn2);
145                 if (rel == MOBILIZATION) {
146                     rel = SITZKRIEG;
147                     setrel(cn, cn2, rel);
148                 } else if (rel == SITZKRIEG) {
149                     rel = AT_WAR;
150                     setrel(cn, cn2, rel);
151                 }
152             }
153         }
154     }
155     /* Only update mobility for non-MOB_ACCESS here, since it doesn't
156        get done for MOB_ACCESS anyway during the update */
157     if (!opt_MOB_ACCESS) {
158         mob_ship(etu);
159         mob_sect(etu);
160         mob_plane(etu);
161         mob_land(etu);
162     }
163     if (update_demand == UPD_DEMAND_SCHED
164         || update_demand == UPD_DEMAND_ASYNC)
165         update_removewants();
166     /* flush all mem file objects to disk */
167     ef_flush(EF_NATION);
168     ef_flush(EF_SECTOR);
169     ef_flush(EF_SHIP);
170     ef_flush(EF_PLANE);
171     ef_flush(EF_LAND);
172     delete_old_announcements();
173     delete_old_news();
174     /* Clear all the telegram flags */
175     for (cn = 0; cn < MAXNOC; cn++)
176         clear_telegram_is_new(cn);
177     logerror("End update");
178 }