]> git.pond.sub.org Git - empserver/blob - src/lib/update/main.c
Log distribution path assembly's CPU use (user and system time)
[empserver] / src / lib / update / main.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2010, 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  *     Markus Armbruster, 2006-2011
35  */
36
37 #include <config.h>
38
39 #include <sys/resource.h>
40 #include "budg.h"
41 #include "empthread.h"
42 #include "game.h"
43 #include "journal.h"
44 #include "player.h"
45 #include "server.h"
46 #include "unit.h"
47 #include "update.h"
48
49 long money[MAXNOC];
50 long pops[MAXNOC];
51 long sea_money[MAXNOC];
52 long lnd_money[MAXNOC];
53 long air_money[MAXNOC];
54 long tpops[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 bp *bp;
64     int cn;
65     struct natstr *np;
66
67     logerror("production update (%d etus)", etu);
68     getrusage(RUSAGE_SELF, &rus1);
69     game_record_update(time(NULL));
70     journal_update(etu);
71
72     /* First, make sure all mobility is updated correctly. */
73     if (opt_MOB_ACCESS) {
74         mob_ship();
75         mob_sect();
76         mob_plane();
77         mob_land();
78     }
79
80     if (opt_AUTO_POWER)
81         update_power();
82
83     /*
84      * set up all the variables which get used in the
85      * sector production routine (for producing education,
86      * happiness, and printing out the state of the nation)
87      */
88     memset(pops, 0, sizeof(pops));
89     memset(air_money, 0, sizeof(air_money));
90     memset(sea_money, 0, sizeof(sea_money));
91     memset(lnd_money, 0, sizeof(lnd_money));
92     bp = bp_alloc();
93     for (n = 0; n < MAXNOC; n++) {
94         money[n] = 0;
95         if (!(np = getnatp(n)))
96             continue;
97         money[n] = np->nat_money;
98         tpops[n] = count_pop(n);
99     }
100
101     logerror("preparing sectors...");
102     prepare_sects(etu, bp);
103     logerror("done preparing sectors.");
104     logerror("producing for countries...");
105     for (i = 0; i < MAXNOC; i++) {
106         long p_sect[SCT_BUDG_MAX+1][2];
107
108         memset(p_sect, 0, sizeof(p_sect));
109         if (!(np = getnatp(i)))
110             continue;
111         if (np->nat_stat == STAT_SANCT) {
112             continue;
113         }
114         np->nat_money += (int)(np->nat_reserve * money_res * etu);
115
116         /* maintain units */
117         prod_ship(etu, i, bp, 0);
118         prod_plane(etu, i, bp, 0);
119         prod_land(etu, i, bp, 0);
120
121         /* produce all sects */
122         produce_sect(i, etu, bp, p_sect);
123
124         /* build units */
125         prod_ship(etu, i, bp, 1);
126         prod_plane(etu, i, bp, 1);
127         prod_land(etu, i, bp, 1);
128     }
129     logerror("done producing for countries.");
130
131     finish_sects(etu);
132     prod_nat(etu);
133     age_levels(etu);
134     free(bp);
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     /* Clear all the telegram flags */
158     for (cn = 0; cn < MAXNOC; cn++)
159         clear_telegram_is_new(cn);
160     getrusage(RUSAGE_SELF, &rus2);
161     logerror("End update %g user %g system",
162              rus2.ru_utime.tv_sec + rus2.ru_utime.tv_usec / 1e6
163              - (rus1.ru_utime.tv_sec + rus1.ru_utime.tv_usec / 1e6),
164              rus2.ru_stime.tv_sec + rus2.ru_stime.tv_usec / 1e6
165              - (rus1.ru_stime.tv_sec + rus1.ru_stime.tv_usec / 1e6));
166 }