]> git.pond.sub.org Git - empserver/blob - src/lib/update/main.c
Fuse update wait thread and update thread:
[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 void
53 update_main(void)
54 {
55     int etu = etu_per_update;
56     int n;
57     int x;
58     struct bp *bp;
59     int cn, cn2, rel;
60     struct natstr *cnp;
61     struct natstr *np;
62
63     logerror("production update (%d etus)", etu);
64     journal_update(etu);
65
66     /* First, make sure all mobility is updated correctly. */
67     if (opt_MOB_ACCESS) {
68         mob_ship(etu);
69         mob_sect(etu);
70         mob_plane(etu);
71         mob_land(etu);
72     }
73
74     if (opt_AUTO_POWER)
75         update_power();
76
77     /*
78      * set up all the variables which get used in the
79      * sector production routine (for producing education,
80      * happiness, and printing out the state of the nation)
81      */
82     memset(pops, 0, sizeof(pops));
83     memset(air_money, 0, sizeof(air_money));
84     memset(sea_money, 0, sizeof(sea_money));
85     memset(lnd_money, 0, sizeof(lnd_money));
86     bp = bp_alloc();
87     for (n = 0; n < MAXNOC; n++) {
88         money[n] = 0;
89         if (!(np = getnatp(n)))
90             continue;
91         money[n] = np->nat_money;
92         tpops[n] = count_pop(n);
93     }
94
95     logerror("preparing sectors...");
96     prepare_sects(etu, bp);
97     logerror("done preparing sectors.");
98     logerror("producing for countries...");
99     for (x = 0; x < MAXNOC; x++) {
100         long p_sect[SCT_TYPE_MAX+2][2];
101
102         memset(p_sect, 0, sizeof(p_sect));
103         if (!(np = getnatp(x)))
104             continue;
105         if (np->nat_stat == STAT_SANCT) {
106 #ifdef DEBUG
107             logerror("Country %i is in sanctuary and did not update", x);
108 #endif
109             continue;
110         }
111         np->nat_money += (int)(np->nat_reserve * money_res * etu);
112
113         /* maintain units */
114         prod_ship(etu, x, bp, 0);
115         prod_plane(etu, x, bp, 0);
116         prod_land(etu, x, bp, 0);
117
118         /* produce all sects */
119         produce_sect(x, etu, bp, p_sect);
120
121         /* build units */
122         prod_ship(etu, x, bp, 1);
123         prod_plane(etu, x, bp, 1);
124         prod_land(etu, x, bp, 1);
125     }
126     logerror("done producing for countries.");
127
128     finish_sects(etu);
129     prod_nat(etu);
130     age_levels(etu);
131     free(bp);
132     /*flushwu(); */
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)) == 0)
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(etu);
157         mob_sect(etu);
158         mob_plane(etu);
159         mob_land(etu);
160     }
161     if (opt_DEMANDUPDATE)
162         update_removewants();
163     /* flush all mem file objects to disk */
164     ef_flush(EF_NATION);
165     ef_flush(EF_SECTOR);
166     ef_flush(EF_SHIP);
167     ef_flush(EF_PLANE);
168     ef_flush(EF_LAND);
169     delete_old_announcements();
170     delete_old_news();
171     /* Clear all the telegram flags */
172     for (cn = 0; cn < MAXNOC; cn++)
173         clear_telegram_is_new(cn);
174     logerror("End update");
175 }