]> git.pond.sub.org Git - empserver/blob - src/lib/update/mobility.c
Update copyright notice
[empserver] / src / lib / update / mobility.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2014, 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  *  mobility.c: Add mobility to each of the items which accumulate mobility.
28  *
29  *  Known contributors to this file:
30  *     Dave Pare, 1986
31  *     Steve McClure, 1998-1999
32  *     Markus Armbruster, 2004-2008
33  */
34
35 #include <config.h>
36
37 #include "game.h"
38 #include "land.h"
39 #include "plane.h"
40 #include "server.h"
41 #include "ship.h"
42 #include "update.h"
43
44 static int do_upd_checking;
45
46 static void do_mob_land(struct lndstr *, int);
47 static void do_mob_plane(struct plnstr *, int);
48 static void do_mob_sect(struct sctstr *sp, int etus);
49 static void do_mob_ship(struct shpstr *, int);
50
51 void
52 sct_do_upd_mob(struct sctstr *sp)
53 {
54     int etus;
55
56     if (do_upd_checking || update_running)
57         return;
58     if (sp->sct_own == 0)
59         return;
60     if (sp->sct_type == SCT_SANCT)
61         return;
62     etus = game_tick_to_now(&sp->sct_access);
63     if (etus == 0)
64         return;
65
66     do_upd_checking = 1;        /* avoid recursion */
67     do_mob_sect(sp, etus);
68     do_upd_checking = 0;
69 }
70
71 void
72 shp_do_upd_mob(struct shpstr *sp)
73 {
74     int etus;
75
76     if (do_upd_checking || update_running)
77         return;
78     if (sp->shp_own == 0)
79         return;
80     etus = game_tick_to_now(&sp->shp_access);
81     if (etus == 0)
82         return;
83
84     do_upd_checking = 1;        /* avoid recursion */
85     do_mob_ship(sp, etus);
86     do_upd_checking = 0;
87 }
88
89 void
90 lnd_do_upd_mob(struct lndstr *lp)
91 {
92     int etus;
93
94     if (do_upd_checking || update_running)
95         return;
96     if (lp->lnd_own == 0)
97         return;
98     etus = game_tick_to_now(&lp->lnd_access);
99     if (etus == 0)
100         return;
101
102     do_upd_checking = 1;        /* avoid recursion */
103     do_mob_land(lp, etus);
104     do_upd_checking = 0;
105 }
106
107 void
108 pln_do_upd_mob(struct plnstr *pp)
109 {
110     int etus;
111
112     if (do_upd_checking || update_running)
113         return;
114     if (pp->pln_own == 0)
115         return;
116     etus = game_tick_to_now(&pp->pln_access);
117     if (etus == 0)
118         return;
119
120     do_upd_checking = 1;        /* avoid recursion */
121     do_mob_plane(pp, etus);
122     do_upd_checking = 0;
123 }
124
125 void
126 mob_sect(void)
127 {
128     struct sctstr *sp;
129     int n, etus;
130     time_t now;
131
132     time(&now);
133     for (n = 0; NULL != (sp = getsectid(n)); n++) {
134         sp->sct_timestamp = now;
135         if (opt_MOB_ACCESS)
136             etus = game_reset_tick(&sp->sct_access);
137         else
138             etus = etu_per_update;
139         do_mob_sect(sp, etus);
140     }
141 }
142
143 static void
144 do_mob_sect(struct sctstr *sp, int etus)
145 {
146     int value;
147
148     if (CANT_HAPPEN(etus < 0))
149         etus = 0;
150
151     if (sp->sct_own == 0)
152         return;
153     if (sp->sct_type == SCT_SANCT)
154         return;
155
156     value = sp->sct_mobil + ((float)etus * sect_mob_scale);
157     if (value > sect_mob_max)
158         value = sect_mob_max;
159     sp->sct_mobil = value;
160 }
161
162 void
163 mob_ship(void)
164 {
165     struct shpstr *sp;
166     int n, etus;
167     time_t now;
168
169     time(&now);
170     for (n = 0; NULL != (sp = getshipp(n)); n++) {
171         sp->shp_timestamp = now;
172         if (opt_MOB_ACCESS)
173             etus = game_reset_tick(&sp->shp_access);
174         else
175             etus = etu_per_update;
176         do_mob_ship(sp, etus);
177     }
178 }
179
180 static void
181 do_mob_ship(struct shpstr *sp, int etus)
182 {
183     int value;
184
185     if (CANT_HAPPEN(etus < 0))
186         etus = 0;
187
188     if (sp->shp_own == 0)
189         return;
190
191     value = sp->shp_mobil + (float)etus * ship_mob_scale;
192     if (value > ship_mob_max)
193         value = ship_mob_max;
194     sp->shp_mobil = (signed char)value;
195 }
196
197 void
198 mob_land(void)
199 {
200     struct lndstr *lp;
201     int n, etus;
202     time_t now;
203
204     time(&now);
205     for (n = 0; NULL != (lp = getlandp(n)); n++) {
206         lp->lnd_timestamp = now;
207         if (opt_MOB_ACCESS)
208             etus = game_reset_tick(&lp->lnd_access);
209         else
210             etus = etu_per_update;
211         do_mob_land(lp, etus);
212     }
213 }
214
215 static void
216 do_mob_land(struct lndstr *lp, int etus)
217 {
218     int value;
219
220     if (CANT_HAPPEN(etus < 0))
221         etus = 0;
222
223     if (lp->lnd_own == 0)
224         return;
225
226     value = lp->lnd_mobil + ((float)etus * land_mob_scale);
227     if (value > land_mob_max) {
228         if (lp->lnd_harden < land_mob_max && !opt_MOB_ACCESS) {
229             /*
230              * Automatic fortification on excess mobility.
231              * Disabled for MOB_ACCESS, because it leads to
232              * excessively deep recursion and thus miserable
233              * performance as the number of land units grows.
234              *
235              * Provide mobility to be used in lnd_fortify()
236              * without overflowing lnd_mobil.
237              */
238             lp->lnd_mobil = land_mob_max;
239             lnd_fortify(lp, value - land_mob_max);
240         }
241         value = land_mob_max;
242     }
243     lp->lnd_mobil = value;
244 }
245
246 void
247 mob_plane(void)
248 {
249     struct plnstr *pp;
250     int n, etus;
251     time_t now;
252
253     time(&now);
254     for (n = 0; NULL != (pp = getplanep(n)); n++) {
255         pp->pln_timestamp = now;
256         if (opt_MOB_ACCESS)
257             etus = game_reset_tick(&pp->pln_access);
258         else
259             etus = etu_per_update;
260         do_mob_plane(pp, etus);
261     }
262 }
263
264 static void
265 do_mob_plane(struct plnstr *pp, int etus)
266 {
267     int value;
268
269     if (CANT_HAPPEN(etus < 0))
270         etus = 0;
271
272     if (pp->pln_own == 0)
273         return;
274
275     value = pp->pln_mobil + ((float)etus * plane_mob_scale);
276     if (value > plane_mob_max)
277         value = plane_mob_max;
278     pp->pln_mobil = value;
279 }