]> git.pond.sub.org Git - empserver/blob - src/lib/update/plane.c
update: Saner rounding of unit building money and work
[empserver] / src / lib / update / plane.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  *  plane.c: Do production for planes
28  *
29  *  Known contributors to this file:
30  *     Dave Pare, 1986
31  *     Steve McClure, 1998
32  *     Markus Armbruster, 2006-2016
33  */
34
35 #include <config.h>
36
37 #include "budg.h"
38 #include "chance.h"
39 #include "lost.h"
40 #include "plane.h"
41 #include "player.h"
42 #include "ship.h"
43 #include "update.h"
44
45 static void planerepair(struct plnstr *, struct natstr *, struct bp *, int);
46 static void upd_plane(struct plnstr *, int, struct natstr *, struct bp *, int);
47
48 int
49 prod_plane(int etus, int natnum, struct bp *bp, int buildem)
50                  /* Build = 1, maintain =0 */
51 {
52     struct plnstr *pp;
53     struct natstr *np;
54     int n, k = 0;
55     int start_money;
56
57     for (n = 0; NULL != (pp = getplanep(n)); n++) {
58         if (pp->pln_own == 0)
59             continue;
60         if (pp->pln_own != natnum)
61             continue;
62         if (pp->pln_effic < PLANE_MINEFF) {
63             makelost(EF_PLANE, pp->pln_own, pp->pln_uid,
64                      pp->pln_x, pp->pln_y);
65             pp->pln_own = 0;
66             continue;
67         }
68
69         if (pln_is_in_orbit(pp)) {
70             if (!player->simulation && buildem == 0
71                 && !(pp->pln_flags & PLN_SYNCHRONOUS))
72                 move_sat(pp);
73             continue;
74         }
75
76         np = getnatp(pp->pln_own);
77         start_money = np->nat_money;
78         upd_plane(pp, etus, np, bp, buildem);
79         air_money[pp->pln_own] += np->nat_money - start_money;
80         if (buildem == 0 || np->nat_money != start_money)
81             k++;
82         if (player->simulation)
83             np->nat_money = start_money;
84     }
85
86     return k;
87 }
88
89 static void
90 upd_plane(struct plnstr *pp, int etus,
91           struct natstr *np, struct bp *bp, int build)
92 {
93     struct plchrstr *pcp = &plchr[(int)pp->pln_type];
94     int mult, cost, eff_lost;
95
96     if (build == 1) {
97         if (!pp->pln_off && np->nat_money >= 0)
98             planerepair(pp, np, bp, etus);
99         if (!player->simulation)
100             pp->pln_off = 0;
101     } else {
102         mult = 1;
103         if (np->nat_level[NAT_TLEV] < pp->pln_tech * 0.85)
104             mult = 2;
105         cost = -(mult * etus * MIN(0.0, pcp->pl_cost * money_plane));
106         if (np->nat_money < cost && !player->simulation) {
107             eff_lost = etus / 5;
108             if (pp->pln_effic - eff_lost < PLANE_MINEFF)
109                 eff_lost = pp->pln_effic - PLANE_MINEFF;
110             if (eff_lost > 0) {
111                 wu(0, pp->pln_own, "%s lost %d%% to lack of maintenance\n",
112                    prplane(pp), eff_lost);
113                 pp->pln_effic -= eff_lost;
114             }
115         } else {
116             np->nat_money -= cost;
117         }
118         /* flight pay is 5x the pay received by other military */
119         np->nat_money += etus * pcp->pl_mat[I_MILIT] * money_mil * 5;
120     }
121 }
122
123 static void
124 planerepair(struct plnstr *pp, struct natstr *np, struct bp *bp, int etus)
125 {
126     struct plchrstr *pcp = &plchr[(int)pp->pln_type];
127     int build;
128     struct shpstr *carrier;
129     struct sctstr *sp, scratch_sect;
130     int delta;
131     int mult;
132     int avail;
133     int used;
134
135     if (pp->pln_effic == 100)
136         return;
137
138     sp = getsectp(pp->pln_x, pp->pln_y);
139     if (sp->sct_off)
140         return;
141
142     carrier = NULL;
143     if (pp->pln_ship >= 0) {
144         if (pp->pln_effic >= 80)
145             return;
146         carrier = getshipp(pp->pln_ship);
147         if (CANT_HAPPEN(!carrier))
148             return;
149         if (carrier->shp_off)
150             return;
151         if (relations_with(carrier->shp_own, pp->pln_own) != ALLIED)
152             return;
153     } else {
154         if (relations_with(sp->sct_own, pp->pln_own) != ALLIED)
155             return;
156     }
157
158     if (player->simulation) {
159         scratch_sect = *sp;
160         bp_to_sect(bp, &scratch_sect);
161         sp = &scratch_sect;
162     }
163
164     mult = 1;
165     if (np->nat_level[NAT_TLEV] < pp->pln_tech * 0.85)
166         mult = 2;
167
168     avail = sp->sct_avail * 100;
169     if (carrier)
170         avail += etus * carrier->shp_item[I_MILIT] / 2;
171
172     delta = avail / pcp->pl_bwork;
173     if (delta <= 0)
174         return;
175     if (delta > (int)((float)etus * plane_grow_scale))
176         delta = (int)((float)etus * plane_grow_scale);
177     if (delta > 100 - pp->pln_effic)
178         delta = 100 - pp->pln_effic;
179
180     build = get_materials(sp, pcp->pl_mat, delta);
181
182     if (carrier)
183         build = delta;
184
185     used = build * pcp->pl_bwork;
186     avail = roundavg((sp->sct_avail * 100 - used) / 100.0);
187     if (avail < 0)
188         avail = 0;
189     sp->sct_avail = avail;
190
191     if (sp->sct_type != SCT_AIRPT)
192         build /= 3;
193     if (carrier) {
194         if ((pp->pln_effic + build) > 80)
195             build = 80 - pp->pln_effic;
196     }
197
198     bp_set_from_sect(bp, sp);
199     np->nat_money -= roundavg(mult * build * pcp->pl_cost / 100.0);
200
201     if (!player->simulation)
202         pp->pln_effic += (signed char)build;
203 }