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