]> git.pond.sub.org Git - empserver/blob - src/lib/update/main.c
(update_main): Change update sequence to repair ships, planes and land
[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 "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 /*ARGSUSED*/
65 void
66 update_main(void *unused)
67 {
68     int etu = etu_per_update;
69     int n;
70     int x;
71     int *bp;
72     int cn, cn2, rel;
73     struct natstr *cnp;
74     struct natstr *np;
75
76     if (CANT_HAPPEN(!update_pending))
77         update_pending = 1;
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     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     memset(pops, 0, sizeof(pops));
96     memset(air_money, 0, sizeof(air_money));
97     memset(sea_money, 0, sizeof(sea_money));
98     memset(lnd_money, 0, sizeof(lnd_money));
99     bp = calloc(WORLD_X * WORLD_Y * 7, sizeof(int));
100     for (n = 0; n < MAXNOC; n++) {
101         money[n] = 0;
102         if (!(np = getnatp(n)))
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;
114         long p_sect[SCT_MAXDEF+1][2];
115
116         memset(p_sect, 0, sizeof(p_sect));
117         mil_dbl_pay = 0;
118         if (!(np = getnatp(x)))
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         /* maintain units */
129         prod_ship(etu, x, bp, 0);
130         prod_plane(etu, x, bp, 0);
131         prod_land(etu, x, bp, 0);
132
133         /* produce all sects */
134         produce_sect(x, etu, bp, p_sect);
135         np->nat_money -= p_sect[SCT_CAPIT][1];
136
137         /* build units */
138         prod_ship(etu, x, bp, 1);
139         prod_plane(etu, x, bp, 1);
140         prod_land(etu, x, bp, 1);
141     }
142     logerror("done producing for countries.");
143
144     finish_sects(etu);
145     prod_nat(etu);
146     age_levels(etu);
147     free(bp);
148     /*flushwu(); */
149     if (opt_SLOW_WAR) {
150         /* Update war declarations */
151         /* MOBILIZATION->SITZKRIEG->AT_WAR */
152         for (cn = 1; cn < MAXNOC; cn++) {
153             if ((cnp = getnatp(cn)) == 0)
154                 break;
155             for (cn2 = 1; cn2 < MAXNOC; cn2++) {
156                 if (cn2 == cn)
157                     continue;
158                 rel = getrel(cnp, cn2);
159                 if (rel == MOBILIZATION) {
160                     rel = SITZKRIEG;
161                     setrel(cn, cn2, rel);
162                 } else if (rel == SITZKRIEG) {
163                     rel = AT_WAR;
164                     setrel(cn, cn2, rel);
165                 }
166             }
167         }
168     }
169     /* Only update mobility for non-MOB_ACCESS here, since it doesn't
170        get done for MOB_ACCESS anyway during the update */
171     if (!opt_MOB_ACCESS) {
172         mob_ship(etu);
173         mob_sect(etu);
174         mob_plane(etu);
175         mob_land(etu);
176     }
177     if (opt_DEMANDUPDATE)
178         update_removewants();
179     /* flush all mem file objects to disk */
180     ef_flush(EF_NATION);
181     ef_flush(EF_SECTOR);
182     ef_flush(EF_SHIP);
183     ef_flush(EF_PLANE);
184     ef_flush(EF_LAND);
185     delete_old_announcements();
186     delete_old_news();
187     /* Clear all the telegram flags */
188     for (cn = 0; cn < MAXNOC; cn++)
189         clear_telegram_is_new(cn);
190     update_pending = 0;
191     logerror("End update");
192     player_delete(player);
193     empth_exit();
194     /*NOTREACHED*/
195 }