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