]> git.pond.sub.org Git - empserver/blob - src/lib/update/main.c
643b9d5117c697e0660420556ce491b45e57af47
[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 tpops[MAXNOC];
51
52 void
53 update_main(void)
54 {
55     int etu = etu_per_update;
56     struct rusage rus1, rus2;
57     int n;
58     int i;
59     struct natstr *np;
60
61     logerror("production update (%d etus)", etu);
62     getrusage(RUSAGE_SELF, &rus1);
63     game_record_update(time(NULL));
64     journal_update(etu);
65     /* Ensure back-to-back production reports are separate: */
66     for (n = 0; n < MAXNOC; n++)
67         clear_telegram_is_new(n);
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(nat_budget, 0, sizeof(nat_budget));
86     for (n = 0; n < MAXNOC; n++) {
87         if (!(np = getnatp(n)))
88             continue;
89         nat_budget[n].start_money = nat_budget[n].money = np->nat_money;
90         tpops[n] = count_pop(n);
91     }
92
93     logerror("preparing sectors...");
94     prepare_sects(etu);
95     logerror("done preparing sectors.");
96     logerror("producing for countries...");
97     for (i = 0; i < MAXNOC; i++) {
98         if (!(np = getnatp(i)))
99             continue;
100         if (np->nat_stat == STAT_SANCT) {
101             continue;
102         }
103
104         /* maintain units */
105         prod_ship(etu, i, NULL, 0);
106         prod_plane(etu, i, NULL, 0);
107         prod_land(etu, i, NULL, 0);
108
109         /* produce all sects */
110         produce_sect(np, etu, NULL);
111
112         /* build units */
113         prod_ship(etu, i, NULL, 1);
114         prod_plane(etu, i, NULL, 1);
115         prod_land(etu, i, NULL, 1);
116     }
117     logerror("done producing for countries.");
118
119     finish_sects(etu);
120     prod_nat(etu);
121     age_levels(etu);
122
123     /* Only update mobility for non-MOB_ACCESS here, since it doesn't
124        get done for MOB_ACCESS anyway during the update */
125     if (!opt_MOB_ACCESS) {
126         mob_ship();
127         mob_sect();
128         mob_plane();
129         mob_land();
130     }
131     if (update_demand == UPD_DEMAND_SCHED
132         || update_demand == UPD_DEMAND_ASYNC)
133         update_removewants();
134     /* flush all mem file objects to disk */
135     ef_flush(EF_NATION);
136     ef_flush(EF_SECTOR);
137     ef_flush(EF_SHIP);
138     ef_flush(EF_PLANE);
139     ef_flush(EF_LAND);
140     unit_cargo_init();
141     delete_old_announcements();
142     delete_old_news();
143     delete_old_lostitems();
144     getrusage(RUSAGE_SELF, &rus2);
145     logerror("End update %g user %g system",
146              rus2.ru_utime.tv_sec + rus2.ru_utime.tv_usec / 1e6
147              - (rus1.ru_utime.tv_sec + rus1.ru_utime.tv_usec / 1e6),
148              rus2.ru_stime.tv_sec + rus2.ru_stime.tv_usec / 1e6
149              - (rus1.ru_stime.tv_sec + rus1.ru_stime.tv_usec / 1e6));
150 }