]> git.pond.sub.org Git - empserver/blob - src/lib/update/main.c
Update copyright notice
[empserver] / src / lib / update / main.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2018, 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 "game.h"
41 #include "journal.h"
42 #include "optlist.h"
43 #include "prototypes.h"
44 #include "server.h"
45 #include "unit.h"
46 #include "update.h"
47
48 /*
49  * Update is running.
50  * Can be used to suppress messages, or direct them to bulletins.
51  */
52 int update_running;
53
54 struct budget nat_budget[MAXNOC];
55
56 void
57 update_main(void)
58 {
59     int etu = etu_per_update;
60     struct rusage rus1, rus2;
61     int n;
62     int i;
63     struct natstr *np;
64
65     update_running = 1;
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     /* Credit the turn's remaining MOB_ACCESS mobility */
75     if (opt_MOB_ACCESS)
76         mob_access_all();
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(nat_budget, 0, sizeof(nat_budget));
87     for (n = 0; n < MAXNOC; n++) {
88         if (!(np = getnatp(n)))
89             continue;
90         nat_budget[n].start_money = nat_budget[n].money = np->nat_money;
91     }
92
93     prep_ships(etu, NULL);
94     prep_planes(etu, NULL);
95     prep_lands(etu, NULL);
96     logerror("preparing sectors...");
97     prepare_sects(etu, NULL);
98     logerror("done preparing sectors.");
99     for (i = 0; i < MAXNOC; i++)
100         pay_reserve(getnatp(i), etu);
101
102     logerror("producing for countries...");
103     /* maintain units */
104     prod_ship(etu, NULL, 0);
105     prod_plane(etu, NULL, 0);
106     prod_land(etu, NULL, 0);
107
108     /* produce all sects */
109     produce_sect(etu, NULL);
110
111     /* build units */
112     prod_ship(etu, NULL, 1);
113     prod_plane(etu, NULL, 1);
114     prod_land(etu, NULL, 1);
115     logerror("done producing for countries.");
116
117     finish_sects(etu);
118     prod_nat(etu);
119     age_levels(etu);
120     mob_inc_all(etu);
121
122     if (update_demand == UPD_DEMAND_SCHED
123         || update_demand == UPD_DEMAND_ASYNC)
124         update_removewants();
125     /* flush all mem file objects to disk */
126     ef_flush(EF_NATION);
127     ef_flush(EF_SECTOR);
128     ef_flush(EF_SHIP);
129     ef_flush(EF_PLANE);
130     ef_flush(EF_LAND);
131     unit_cargo_init();
132     delete_old_announcements();
133     delete_old_news();
134     delete_old_lostitems();
135     getrusage(RUSAGE_SELF, &rus2);
136     logerror("End update %g user %g system",
137              rus2.ru_utime.tv_sec + rus2.ru_utime.tv_usec / 1e6
138              - (rus1.ru_utime.tv_sec + rus1.ru_utime.tv_usec / 1e6),
139              rus2.ru_stime.tv_sec + rus2.ru_stime.tv_usec / 1e6
140              - (rus1.ru_stime.tv_sec + rus1.ru_stime.tv_usec / 1e6));
141     update_running = 0;
142 }