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