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