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