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