]> git.pond.sub.org Git - empserver/blob - src/lib/update/main.c
update: Don't use materials and work destroyed by che or plague
[empserver] / src / lib / update / main.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2016, Dave Pare, Jeff Bailey, Thomas Ruschak,
4  *                Ken Stevens, Steve McClure, Markus Armbruster
5  *
6  *  Empire 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 3 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, see <http://www.gnu.org/licenses/>.
18  *
19  *  ---
20  *
21  *  See files README, COPYING and CREDITS in the root of the source
22  *  tree for related information and legal notices.  It is expected
23  *  that future projects/authors will amend these files as needed.
24  *
25  *  ---
26  *
27  *  main.c: World update main function
28  *
29  *  Known contributors to this file:
30  *     Dave Pare, 1994
31  *     Steve McClure, 1996
32  *     Doug Hay, 1998
33  *     Markus Armbruster, 2006-2016
34  */
35
36 #include <config.h>
37
38 #include <sys/resource.h>
39 #include "budg.h"
40 #include "empthread.h"
41 #include "game.h"
42 #include "journal.h"
43 #include "server.h"
44 #include "unit.h"
45 #include "update.h"
46
47 int money[MAXNOC];
48 int pops[MAXNOC];
49 int sea_money[MAXNOC];
50 int lnd_money[MAXNOC];
51 int air_money[MAXNOC];
52 int tpops[MAXNOC];
53
54 void
55 update_main(void)
56 {
57     int etu = etu_per_update;
58     struct rusage rus1, rus2;
59     int n;
60     int i;
61     struct natstr *np;
62
63     logerror("production update (%d etus)", etu);
64     getrusage(RUSAGE_SELF, &rus1);
65     game_record_update(time(NULL));
66     journal_update(etu);
67     /* Ensure back-to-back production reports are separate: */
68     for (n = 0; n < MAXNOC; n++)
69         clear_telegram_is_new(n);
70
71     /* First, make sure all mobility is updated correctly. */
72     if (opt_MOB_ACCESS) {
73         mob_ship();
74         mob_sect();
75         mob_plane();
76         mob_land();
77     }
78
79     if (opt_AUTO_POWER)
80         update_power();
81
82     /*
83      * set up all the variables which get used in the
84      * sector production routine (for producing education,
85      * happiness, and printing out the state of the nation)
86      */
87     memset(pops, 0, sizeof(pops));
88     memset(air_money, 0, sizeof(air_money));
89     memset(sea_money, 0, sizeof(sea_money));
90     memset(lnd_money, 0, sizeof(lnd_money));
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);
101     logerror("done preparing sectors.");
102     logerror("producing for countries...");
103     for (i = 0; i < MAXNOC; i++) {
104         int 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, NULL, 0);
116         prod_plane(etu, i, NULL, 0);
117         prod_land(etu, i, NULL, 0);
118
119         /* produce all sects */
120         produce_sect(np, etu, NULL, p_sect);
121
122         /* build units */
123         prod_ship(etu, i, NULL, 1);
124         prod_plane(etu, i, NULL, 1);
125         prod_land(etu, i, NULL, 1);
126     }
127     logerror("done producing for countries.");
128
129     finish_sects(etu);
130     prod_nat(etu);
131     age_levels(etu);
132
133     /* Only update mobility for non-MOB_ACCESS here, since it doesn't
134        get done for MOB_ACCESS anyway during the update */
135     if (!opt_MOB_ACCESS) {
136         mob_ship();
137         mob_sect();
138         mob_plane();
139         mob_land();
140     }
141     if (update_demand == UPD_DEMAND_SCHED
142         || update_demand == UPD_DEMAND_ASYNC)
143         update_removewants();
144     /* flush all mem file objects to disk */
145     ef_flush(EF_NATION);
146     ef_flush(EF_SECTOR);
147     ef_flush(EF_SHIP);
148     ef_flush(EF_PLANE);
149     ef_flush(EF_LAND);
150     unit_cargo_init();
151     delete_old_announcements();
152     delete_old_news();
153     delete_old_lostitems();
154     getrusage(RUSAGE_SELF, &rus2);
155     logerror("End update %g user %g system",
156              rus2.ru_utime.tv_sec + rus2.ru_utime.tv_usec / 1e6
157              - (rus1.ru_utime.tv_sec + rus1.ru_utime.tv_usec / 1e6),
158              rus2.ru_stime.tv_sec + rus2.ru_stime.tv_usec / 1e6
159              - (rus1.ru_stime.tv_sec + rus1.ru_stime.tv_usec / 1e6));
160 }