]> git.pond.sub.org Git - empserver/blob - src/lib/update/main.c
Remove a bunch of redundant casts.
[empserver] / src / lib / update / main.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2005, 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  *  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 "misc.h"
37 #include "nat.h"
38 #include "file.h"
39 #include "sect.h"
40 #include "player.h"
41 #include "empthread.h"
42 #include "budg.h"
43 #include "player.h"
44 #include "update.h"
45 #include "common.h"
46 #include "optlist.h"
47 #include "server.h"
48 #include <stdlib.h>
49 #if !defined(_WIN32)
50 #include <sys/time.h>
51 #endif
52
53 long money[MAXNOC];
54 long pops[MAXNOC];
55 long sea_money[MAXNOC];
56 long lnd_money[MAXNOC];
57 long air_money[MAXNOC];
58 long tpops[MAXNOC];
59
60 int update_pending = 0;
61
62 static void do_prod(int, int, int, int *, long int (*)[2], int *, int *,
63                     int *, int *, int *, int *);
64
65 /*ARGSUSED*/
66 void
67 update_main(void *unused)
68 {
69     int etu = etu_per_update;
70     int n;
71     int x;
72     int *bp;
73     int cn, cn2, rel;
74     struct natstr *cnp;
75     struct natstr *np;
76
77     /* First, make sure all mobility is updated correctly. */
78     if (opt_MOB_ACCESS) {
79         mob_ship(etu);
80         mob_sect(etu);
81         mob_plane(etu);
82         mob_land(etu);
83     }
84     update_pending = 1;
85     player->proc = empth_self();
86     player->cnum = 0;
87     player->god = 1;
88     /*
89      * set up all the variables which get used in the
90      * sector production routine (for producing education,
91      * happiness, and printing out the state of the nation)
92      */
93     logerror("production update (%d etus)", etu);
94     memset(pops, 0, sizeof(pops));
95     memset(air_money, 0, sizeof(air_money));
96     memset(sea_money, 0, sizeof(sea_money));
97     memset(lnd_money, 0, sizeof(lnd_money));
98     bp = calloc(WORLD_X * WORLD_Y * 7, sizeof(int));
99     for (n = 0; n < MAXNOC; n++) {
100         money[n] = 0;
101         if ((np = getnatp(n)) == (struct natstr *)0)
102             continue;
103         money[n] = np->nat_money;
104         tpops[n] = count_pop(n);
105     }
106
107     logerror("preparing sectors...");
108     prepare_sects(etu, bp);
109     logerror("done preparing sectors.");
110     logerror("producing for countries...");
111     for (x = 0; x < MAXNOC; x++) {
112         int y, z, sb = 0, sm = 0, pb = 0, pm = 0, lm = 0, lb = 0;
113         long p_sect[SCT_MAXDEF+1][2];
114
115         memset(p_sect, 0, sizeof(p_sect));
116         mil_dbl_pay = 0;
117         if ((np = getnatp(x)) == (struct natstr *)0)
118             continue;
119         if (np->nat_stat & STAT_SANCT) {
120 #ifdef DEBUG
121             logerror("Country %i is in sanctuary and did not update", x);
122 #endif
123             continue;
124         }
125         np->nat_money += (int)(np->nat_reserve * money_res * etu);
126
127         for (y = 1; y <= PRI_MAX; y++) {
128             for (z = 0; z <= PRI_MAX; z++) {
129                 if (np->nat_priorities[z] == y) {
130                     do_prod(z, etu, x, bp, p_sect,
131                             &sb, &sm, &pb, &pm, &lb, &lm);
132                 }
133             }
134         }
135         /* 0 is maintain, 1 is build */
136         if (!sm)
137             prod_ship(etu, x, bp, 0);
138         if (!sb)
139             prod_ship(etu, x, bp, 1);
140         if (!pm)
141             prod_plane(etu, x, bp, 0);
142         if (!pb)
143             prod_plane(etu, x, bp, 1);
144         if (!lm)
145             prod_land(etu, x, bp, 0);
146         if (!lb)
147             prod_land(etu, x, bp, 1);
148
149         /* produce all sects that haven't produced yet */
150         produce_sect(x, etu, bp, p_sect, -1);
151         np->nat_money -= p_sect[SCT_CAPIT][1];
152     }
153     logerror("done producing for countries.");
154
155     finish_sects(etu);
156     prod_nat(etu);
157     age_levels(etu);
158     free(bp);
159     /*flushwu(); */
160     if (opt_SLOW_WAR) {
161         /* Update war declarations */
162         /* MOBILIZATION->SITZKRIEG->AT_WAR */
163         for (cn = 1; cn < MAXNOC; cn++) {
164             if ((cnp = getnatp(cn)) == 0)
165                 break;
166             for (cn2 = 1; cn2 < MAXNOC; cn2++) {
167                 if (cn2 == cn)
168                     continue;
169                 rel = getrel(cnp, cn2);
170                 if (rel == MOBILIZATION) {
171                     rel = SITZKRIEG;
172                     setrel(cn, cn2, rel);
173                 } else if (rel == SITZKRIEG) {
174                     rel = AT_WAR;
175                     setrel(cn, cn2, rel);
176                 }
177             }
178         }
179     }
180     /* Age contact */
181     if (opt_LOSE_CONTACT) {
182         for (cn = 1; cn < MAXNOC; cn++) {
183             if ((cnp = getnatp(cn)) != NULL)
184                 agecontact(cnp);
185         }
186     }
187     /* Only update mobility for non-MOB_ACCESS here, since it doesn't
188        get done for MOB_ACCESS anyway during the update */
189     if (!opt_MOB_ACCESS) {
190         mob_ship(etu);
191         mob_sect(etu);
192         mob_plane(etu);
193         mob_land(etu);
194     }
195     if (opt_DEMANDUPDATE)
196         update_removewants();
197     /* flush all mem file objects to disk */
198     ef_flush(EF_NATION);
199     ef_flush(EF_SECTOR);
200     ef_flush(EF_SHIP);
201     ef_flush(EF_PLANE);
202     ef_flush(EF_LAND);
203     delete_old_announcements();
204     delete_old_news();
205     /* Clear all the telegram flags */
206     for (cn = 0; cn < MAXNOC; cn++)
207         clear_telegram_is_new(cn);
208     update_pending = 0;
209     logerror("End update");
210     player_delete(player);
211     empth_exit();
212     /*NOTREACHED*/
213 }
214
215 static void
216 do_prod(int sector_type, int etu, int n, int *bp, long int (*p_sect)[2],
217         int *ship_build, int *ship_maint, int *plane_build,
218         int *plane_maint, int *land_build, int *land_maint)
219 {
220     struct natstr *np;
221
222     np = getnatp(n);
223
224     if (sector_type == PRI_SMAINT) {
225         prod_ship(etu, n, bp, 0);
226         *ship_maint = 1;
227     } else if (sector_type == PRI_SBUILD) {
228         prod_ship(etu, n, bp, 1);
229         *ship_build = 1;
230     } else if (sector_type == PRI_PMAINT) {
231         prod_plane(etu, n, bp, 0);
232         *plane_maint = 1;
233     } else if (sector_type == PRI_PBUILD) {
234         prod_plane(etu, n, bp, 1);
235         *plane_build = 1;
236     } else if (sector_type == PRI_LMAINT) {
237         if (*land_build)
238             np->nat_money -= (int)(money_mil * etu * mil_dbl_pay);
239         prod_land(etu, n, bp, 0);
240         *land_maint = 1;
241     } else if (sector_type == PRI_LBUILD) {
242         prod_land(etu, n, bp, 1);
243         *land_build = 1;
244     } else {
245         produce_sect(n, etu, bp, p_sect, sector_type);
246     }
247 }