]> git.pond.sub.org Git - empserver/blob - src/lib/update/main.c
(update_main): Remove unused local variables.
[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 "misc.h"
39 #include "nat.h"
40 #include "file.h"
41 #include "sect.h"
42 #include "player.h"
43 #include "empthread.h"
44 #include "budg.h"
45 #include "player.h"
46 #include "update.h"
47 #include "common.h"
48 #include "optlist.h"
49 #include "journal.h"
50 #include "server.h"
51 #include <stdlib.h>
52 #if !defined(_WIN32)
53 #include <sys/time.h>
54 #endif
55
56 long money[MAXNOC];
57 long pops[MAXNOC];
58 long sea_money[MAXNOC];
59 long lnd_money[MAXNOC];
60 long air_money[MAXNOC];
61 long tpops[MAXNOC];
62
63 int update_pending = 0;
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     if (CANT_HAPPEN(!update_pending))
78         update_pending = 1;
79
80     /* First, make sure all mobility is updated correctly. */
81     if (opt_MOB_ACCESS) {
82         mob_ship(etu);
83         mob_sect(etu);
84         mob_plane(etu);
85         mob_land(etu);
86     }
87     player->proc = empth_self();
88     player->cnum = 0;
89     player->god = 1;
90     /*
91      * set up all the variables which get used in the
92      * sector production routine (for producing education,
93      * happiness, and printing out the state of the nation)
94      */
95     logerror("production update (%d etus)", etu);
96     journal_update(etu);
97     memset(pops, 0, sizeof(pops));
98     memset(air_money, 0, sizeof(air_money));
99     memset(sea_money, 0, sizeof(sea_money));
100     memset(lnd_money, 0, sizeof(lnd_money));
101     bp = calloc(WORLD_X * WORLD_Y * 7, sizeof(int));
102     for (n = 0; n < MAXNOC; n++) {
103         money[n] = 0;
104         if (!(np = getnatp(n)))
105             continue;
106         money[n] = np->nat_money;
107         tpops[n] = count_pop(n);
108     }
109
110     logerror("preparing sectors...");
111     prepare_sects(etu, bp);
112     logerror("done preparing sectors.");
113     logerror("producing for countries...");
114     for (x = 0; x < MAXNOC; x++) {
115         long p_sect[SCT_MAXDEF+1][2];
116
117         memset(p_sect, 0, sizeof(p_sect));
118         mil_dbl_pay = 0;
119         if (!(np = getnatp(x)))
120             continue;
121         if (np->nat_stat == STAT_SANCT) {
122 #ifdef DEBUG
123             logerror("Country %i is in sanctuary and did not update", x);
124 #endif
125             continue;
126         }
127         np->nat_money += (int)(np->nat_reserve * money_res * etu);
128
129         /* maintain units */
130         prod_ship(etu, x, bp, 0);
131         prod_plane(etu, x, bp, 0);
132         prod_land(etu, x, bp, 0);
133
134         /* produce all sects */
135         produce_sect(x, etu, bp, p_sect);
136         np->nat_money -= p_sect[SCT_CAPIT][1];
137
138         /* build units */
139         prod_ship(etu, x, bp, 1);
140         prod_plane(etu, x, bp, 1);
141         prod_land(etu, x, bp, 1);
142     }
143     logerror("done producing for countries.");
144
145     finish_sects(etu);
146     prod_nat(etu);
147     age_levels(etu);
148     free(bp);
149     /*flushwu(); */
150     if (opt_SLOW_WAR) {
151         /* Update war declarations */
152         /* MOBILIZATION->SITZKRIEG->AT_WAR */
153         for (cn = 1; cn < MAXNOC; cn++) {
154             if ((cnp = getnatp(cn)) == 0)
155                 break;
156             for (cn2 = 1; cn2 < MAXNOC; cn2++) {
157                 if (cn2 == cn)
158                     continue;
159                 rel = getrel(cnp, cn2);
160                 if (rel == MOBILIZATION) {
161                     rel = SITZKRIEG;
162                     setrel(cn, cn2, rel);
163                 } else if (rel == SITZKRIEG) {
164                     rel = AT_WAR;
165                     setrel(cn, cn2, rel);
166                 }
167             }
168         }
169     }
170     /* Only update mobility for non-MOB_ACCESS here, since it doesn't
171        get done for MOB_ACCESS anyway during the update */
172     if (!opt_MOB_ACCESS) {
173         mob_ship(etu);
174         mob_sect(etu);
175         mob_plane(etu);
176         mob_land(etu);
177     }
178     if (opt_DEMANDUPDATE)
179         update_removewants();
180     /* flush all mem file objects to disk */
181     ef_flush(EF_NATION);
182     ef_flush(EF_SECTOR);
183     ef_flush(EF_SHIP);
184     ef_flush(EF_PLANE);
185     ef_flush(EF_LAND);
186     delete_old_announcements();
187     delete_old_news();
188     /* Clear all the telegram flags */
189     for (cn = 0; cn < MAXNOC; cn++)
190         clear_telegram_is_new(cn);
191     update_pending = 0;
192     logerror("End update");
193     player_delete(player);
194     empth_exit();
195     /*NOTREACHED*/
196 }