]> git.pond.sub.org Git - empserver/blob - src/lib/update/main.c
Break inclusion cycle: prototypes.h and commands.h included each
[empserver] / src / lib / update / main.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2006, 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 "journal.h"
41 #include "player.h"
42 #include "server.h"
43 #include "update.h"
44
45 long money[MAXNOC];
46 long pops[MAXNOC];
47 long sea_money[MAXNOC];
48 long lnd_money[MAXNOC];
49 long air_money[MAXNOC];
50 long tpops[MAXNOC];
51
52 int update_pending = 0;
53
54 /*ARGSUSED*/
55 void
56 update_main(void *unused)
57 {
58     int etu = etu_per_update;
59     int n;
60     int x;
61     int *bp;
62     int cn, cn2, rel;
63     struct natstr *cnp;
64     struct natstr *np;
65
66     if (CANT_HAPPEN(!update_pending))
67         update_pending = 1;
68
69     /* First, make sure all mobility is updated correctly. */
70     if (opt_MOB_ACCESS) {
71         mob_ship(etu);
72         mob_sect(etu);
73         mob_plane(etu);
74         mob_land(etu);
75     }
76     player->proc = empth_self();
77     player->cnum = 0;
78     player->god = 1;
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     logerror("production update (%d etus)", etu);
85     journal_update(etu);
86     memset(pops, 0, sizeof(pops));
87     memset(air_money, 0, sizeof(air_money));
88     memset(sea_money, 0, sizeof(sea_money));
89     memset(lnd_money, 0, sizeof(lnd_money));
90     bp = calloc(WORLD_X * WORLD_Y * 7, sizeof(int));
91     for (n = 0; n < MAXNOC; n++) {
92         money[n] = 0;
93         if (!(np = getnatp(n)))
94             continue;
95         money[n] = np->nat_money;
96         tpops[n] = count_pop(n);
97     }
98
99     logerror("preparing sectors...");
100     prepare_sects(etu, bp);
101     logerror("done preparing sectors.");
102     logerror("producing for countries...");
103     for (x = 0; x < MAXNOC; x++) {
104         long p_sect[SCT_TYPE_MAX+2][2];
105
106         memset(p_sect, 0, sizeof(p_sect));
107         if (!(np = getnatp(x)))
108             continue;
109         if (np->nat_stat == STAT_SANCT) {
110 #ifdef DEBUG
111             logerror("Country %i is in sanctuary and did not update", x);
112 #endif
113             continue;
114         }
115         np->nat_money += (int)(np->nat_reserve * money_res * etu);
116
117         /* maintain units */
118         prod_ship(etu, x, bp, 0);
119         prod_plane(etu, x, bp, 0);
120         prod_land(etu, x, bp, 0);
121
122         /* produce all sects */
123         produce_sect(x, etu, bp, p_sect);
124
125         /* build units */
126         prod_ship(etu, x, bp, 1);
127         prod_plane(etu, x, bp, 1);
128         prod_land(etu, x, bp, 1);
129     }
130     logerror("done producing for countries.");
131
132     finish_sects(etu);
133     prod_nat(etu);
134     age_levels(etu);
135     free(bp);
136     /*flushwu(); */
137     if (opt_SLOW_WAR) {
138         /* Update war declarations */
139         /* MOBILIZATION->SITZKRIEG->AT_WAR */
140         for (cn = 1; cn < MAXNOC; cn++) {
141             if ((cnp = getnatp(cn)) == 0)
142                 break;
143             for (cn2 = 1; cn2 < MAXNOC; cn2++) {
144                 if (cn2 == cn)
145                     continue;
146                 rel = getrel(cnp, cn2);
147                 if (rel == MOBILIZATION) {
148                     rel = SITZKRIEG;
149                     setrel(cn, cn2, rel);
150                 } else if (rel == SITZKRIEG) {
151                     rel = AT_WAR;
152                     setrel(cn, cn2, rel);
153                 }
154             }
155         }
156     }
157     /* Only update mobility for non-MOB_ACCESS here, since it doesn't
158        get done for MOB_ACCESS anyway during the update */
159     if (!opt_MOB_ACCESS) {
160         mob_ship(etu);
161         mob_sect(etu);
162         mob_plane(etu);
163         mob_land(etu);
164     }
165     if (opt_DEMANDUPDATE)
166         update_removewants();
167     /* flush all mem file objects to disk */
168     ef_flush(EF_NATION);
169     ef_flush(EF_SECTOR);
170     ef_flush(EF_SHIP);
171     ef_flush(EF_PLANE);
172     ef_flush(EF_LAND);
173     delete_old_announcements();
174     delete_old_news();
175     /* Clear all the telegram flags */
176     for (cn = 0; cn < MAXNOC; cn++)
177         clear_telegram_is_new(cn);
178     update_pending = 0;
179     logerror("End update");
180     player_delete(player);
181     empth_exit();
182     /*NOTREACHED*/
183 }