]> git.pond.sub.org Git - empserver/blob - src/lib/update/main.c
budget: Fix military count (but not yet their pay)
[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 "empthread.h"
40 #include "file.h"
41 #include "game.h"
42 #include "journal.h"
43 #include "optlist.h"
44 #include "prototypes.h"
45 #include "server.h"
46 #include "unit.h"
47 #include "update.h"
48
49 struct budget nat_budget[MAXNOC];
50 int money[MAXNOC];
51 int pops[MAXNOC];
52 int sea_money[MAXNOC];
53 int lnd_money[MAXNOC];
54 int air_money[MAXNOC];
55 int tpops[MAXNOC];
56
57 void
58 update_main(void)
59 {
60     int etu = etu_per_update;
61     struct rusage rus1, rus2;
62     int n;
63     int i;
64     struct natstr *np;
65
66     logerror("production update (%d etus)", etu);
67     getrusage(RUSAGE_SELF, &rus1);
68     game_record_update(time(NULL));
69     journal_update(etu);
70     /* Ensure back-to-back production reports are separate: */
71     for (n = 0; n < MAXNOC; n++)
72         clear_telegram_is_new(n);
73
74     /* First, make sure all mobility is updated correctly. */
75     if (opt_MOB_ACCESS) {
76         mob_ship();
77         mob_sect();
78         mob_plane();
79         mob_land();
80     }
81
82     if (opt_AUTO_POWER)
83         update_power();
84
85     /*
86      * set up all the variables which get used in the
87      * sector production routine (for producing education,
88      * happiness, and printing out the state of the nation)
89      */
90     memset(nat_budget, 0, sizeof(nat_budget));
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     for (n = 0; n < MAXNOC; n++) {
96         money[n] = 0;
97         if (!(np = getnatp(n)))
98             continue;
99         money[n] = np->nat_money;
100         tpops[n] = count_pop(n);
101     }
102
103     logerror("preparing sectors...");
104     prepare_sects(etu);
105     logerror("done preparing sectors.");
106     logerror("producing for countries...");
107     for (i = 0; i < MAXNOC; i++) {
108         int p_sect[SCT_BUDG_MAX+1][2];
109
110         memset(p_sect, 0, sizeof(p_sect));
111         if (!(np = getnatp(i)))
112             continue;
113         if (np->nat_stat == STAT_SANCT) {
114             continue;
115         }
116
117         /* maintain units */
118         prod_ship(etu, i, NULL, 0);
119         prod_plane(etu, i, NULL, 0);
120         prod_land(etu, i, NULL, 0);
121
122         /* produce all sects */
123         produce_sect(np, etu, NULL, p_sect);
124
125         /* build units */
126         prod_ship(etu, i, NULL, 1);
127         prod_plane(etu, i, NULL, 1);
128         prod_land(etu, i, NULL, 1);
129     }
130     logerror("done producing for countries.");
131
132     finish_sects(etu);
133     prod_nat(etu);
134     age_levels(etu);
135
136     /* Only update mobility for non-MOB_ACCESS here, since it doesn't
137        get done for MOB_ACCESS anyway during the update */
138     if (!opt_MOB_ACCESS) {
139         mob_ship();
140         mob_sect();
141         mob_plane();
142         mob_land();
143     }
144     if (update_demand == UPD_DEMAND_SCHED
145         || update_demand == UPD_DEMAND_ASYNC)
146         update_removewants();
147     /* flush all mem file objects to disk */
148     ef_flush(EF_NATION);
149     ef_flush(EF_SECTOR);
150     ef_flush(EF_SHIP);
151     ef_flush(EF_PLANE);
152     ef_flush(EF_LAND);
153     unit_cargo_init();
154     delete_old_announcements();
155     delete_old_news();
156     delete_old_lostitems();
157     getrusage(RUSAGE_SELF, &rus2);
158     logerror("End update %g user %g system",
159              rus2.ru_utime.tv_sec + rus2.ru_utime.tv_usec / 1e6
160              - (rus1.ru_utime.tv_sec + rus1.ru_utime.tv_usec / 1e6),
161              rus2.ru_stime.tv_sec + rus2.ru_stime.tv_usec / 1e6
162              - (rus1.ru_stime.tv_sec + rus1.ru_stime.tv_usec / 1e6));
163 }