]> git.pond.sub.org Git - empserver/blob - src/lib/update/plane.c
Indented with src/scripts/indent-emp.
[empserver] / src / lib / update / plane.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2000, 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 the "LEGAL", "LICENSE", "CREDITS" and "README" files for all the
23  *  related information and legal notices. It is expected that any future
24  *  projects/authors will amend these files as needed.
25  *
26  *  ---
27  *
28  *  plane.c: Do production for planes
29  * 
30  *  Known contributors to this file:
31  *     Dave Pare, 1986
32  *     Steve McClure, 1998
33  */
34
35 #include "misc.h"
36 #include "var.h"
37 #include "sect.h"
38 #include "plane.h"
39 #include "ship.h"
40 #include "nat.h"
41 #include "file.h"
42 #include "optlist.h"
43 #include "budg.h"
44 #include "player.h"
45 #include "update.h"
46 #include "lost.h"
47 #include "subs.h"
48 #include "common.h"
49 #include "gen.h"
50
51 #ifndef MIN
52 #define MIN(x,y)        ((x) > (y) ? (y) : (x))
53 #endif
54
55 int
56 prod_plane(int etus, int natnum, int *bp, int buildem)
57
58
59
60                  /* Build = 1, maintain =0 */
61 {
62     extern double money_mil;
63     extern double money_plane;
64     extern int plane_grow_scale;
65     extern long air_money[MAXNOC];
66     register struct plnstr *pp;
67     register struct plchrstr *plp;
68     struct natstr *np;
69     float leftp, buildp;
70     int left, build;
71     int lcm_needed, hcm_needed;
72     int mil_needed;
73     int svec[I_MAX + 1];
74     int mvec[I_MAX + 1];
75     int n, k = 0;
76     struct shpstr *shp;
77     struct plchrstr *desc;
78     struct sctstr *sp;
79     int delta;
80     int mult;
81     int cost;
82     int eff;
83     int avail;
84     int w_p_eff;
85     int used;
86     int start_money, onship = 0;
87
88     for (n = 0; NULL != (pp = getplanep(n)); n++) {
89         if (pp->pln_own == 0)
90             continue;
91         if (pp->pln_own != natnum)
92             continue;
93         if (pp->pln_effic < PLANE_MINEFF) {
94             makelost(EF_PLANE, pp->pln_own, pp->pln_uid,
95                      pp->pln_x, pp->pln_y);
96             pp->pln_own = 0;
97             continue;
98         }
99
100         plp = &plchr[(int)pp->pln_type];
101         if (pp->pln_flags & PLN_LAUNCHED) {
102             if (opt_ORBIT && (buildem == 0)) {
103                 if ((!player->simulation) &&
104                     (plp->pl_flags & P_O) &&
105                     (pp->pln_flags & PLN_LAUNCHED) &&
106                     !(plp->pl_flags & P_M) &&
107                     !(pp->pln_flags & PLN_SYNCHRONOUS))
108                     move_sat(pp);
109             }
110             continue;
111         }
112
113         onship = 0;
114         shp = (struct shpstr *)0;
115         if (pp->pln_ship >= 0 && (buildem == 1)) {
116             if (pp->pln_effic >= 80)
117                 continue;
118             onship = 1;
119             shp = getshipp(pp->pln_ship);
120             if (shp == 0 || shp->shp_own != pp->pln_own) {
121                 /* nplane is unsigned... */
122                 if (shp->shp_nplane > 0)
123                     shp->shp_nplane--;
124                 makelost(EF_PLANE, pp->pln_own, pp->pln_uid, pp->pln_x,
125                          pp->pln_y);
126                 pp->pln_own = 0;
127                 continue;
128             }
129         }
130         np = getnatp(pp->pln_own);
131         desc = &plchr[(int)pp->pln_type];
132         sp = getsectp(pp->pln_x, pp->pln_y);
133         getvec(VT_ITEM, svec, (s_char *)sp, EF_SECTOR);
134         mult = 1;
135         if (np->nat_level[NAT_TLEV] < pp->pln_tech * 0.85)
136             mult = 2;
137
138         if (buildem == 0) {
139             /* flight pay is 5x the pay received by other military */
140             start_money = np->nat_money;
141             cost = -(mult * etus * dmin(0.0, desc->pl_cost * money_plane));
142             if ((np->nat_priorities[PRI_PMAINT] == 0 ||
143                  np->nat_money < cost) && !player->simulation) {
144                 if ((eff = pp->pln_effic - etus / 5) < PLANE_MINEFF) {
145                     wu(0, pp->pln_own,
146                        "%s lost to lack of maintenance\n", prplane(pp));
147                     makelost(EF_PLANE, pp->pln_own, pp->pln_uid, pp->pln_x,
148                              pp->pln_y);
149                     pp->pln_own = 0;
150                     continue;
151                 }
152                 wu(0, pp->pln_own,
153                    "%s lost %d%% to lack of maintenance\n",
154                    prplane(pp), pp->pln_effic - eff);
155                 pp->pln_effic = eff;
156             } else {
157                 np->nat_money -= cost;
158             }
159
160             np->nat_money += (etus * plp->pl_crew * money_mil * 5);
161
162             air_money[pp->pln_own] += np->nat_money - start_money;
163             k++;
164             if (player->simulation)
165                 np->nat_money = start_money;
166             if ((pp->pln_flags & PLN_LAUNCHED) == PLN_LAUNCHED)
167                 continue;
168         } else {
169             if (sp->sct_off)
170                 continue;
171             if (np->nat_priorities[PRI_PBUILD] == 0 || np->nat_money < 0)
172                 continue;
173
174             start_money = np->nat_money;
175             left = 100 - pp->pln_effic;
176             if (left <= 0)
177                 continue;
178
179             if (!player->simulation)
180                 avail = sp->sct_avail * 100;
181             else
182                 avail = gt_bg_nmbr(bp, sp, I_MAX + 1) * 100;
183
184             if (pp->pln_ship >= 0) {
185                 int vec[I_MAX + 1];
186                 shp = getshipp(pp->pln_ship);
187                 getvec(VT_ITEM, vec, (s_char *)shp, EF_SHIP);
188                 avail += (etus * vec[I_MILIT] / 2);
189             }
190             w_p_eff = 20 + (desc->pl_lcm + 2 * desc->pl_hcm);
191             delta = roundavg((double)avail / w_p_eff);
192             if (delta <= 0)
193                 continue;
194             if (delta > etus * plane_grow_scale)
195                 delta = etus * plane_grow_scale;
196             if (delta > left)
197                 delta = left;
198
199             /* delta is the max amount we can grow */
200
201             left = 100 - pp->pln_effic;
202             if (left > delta)
203                 left = delta;
204
205             leftp = ((float)left / 100.0);
206             bzero((s_char *)mvec, sizeof(mvec));
207             mvec[I_MILIT] = mil_needed =
208                 ldround((double)(plp->pl_crew * leftp), 1);
209             mvec[I_LCM] = lcm_needed =
210                 ldround((double)(plp->pl_lcm * leftp), 1);
211             mvec[I_HCM] = hcm_needed =
212                 ldround((double)(plp->pl_hcm * leftp), 1);
213
214             get_materials(sp, bp, mvec, 0);
215
216             if (mvec[I_MILIT] >= mil_needed)
217                 buildp = leftp;
218             else
219                 buildp = ((float)mvec[I_MILIT] / (float)plp->pl_crew);
220
221             if (mvec[I_LCM] < lcm_needed)
222                 buildp = MIN(buildp, ((float)mvec[I_LCM] /
223                                       (float)plp->pl_lcm));
224
225             if (mvec[I_HCM] < hcm_needed)
226                 buildp = MIN(buildp, ((float)mvec[I_HCM] /
227                                       (float)plp->pl_hcm));
228
229             build = ldround((double)(buildp * 100.0), 1);
230             bzero((s_char *)mvec, sizeof(mvec));
231             mvec[I_MILIT] = mil_needed =
232                 roundavg((double)(plp->pl_crew * buildp));
233             mvec[I_LCM] = lcm_needed =
234                 roundavg((double)(plp->pl_lcm * buildp));
235             mvec[I_HCM] = hcm_needed =
236                 roundavg((double)(plp->pl_hcm * buildp));
237
238             get_materials(sp, bp, mvec, 1);
239
240             if (onship)
241                 build = delta;
242             used = build * w_p_eff;
243
244             /*
245              * I didn't use roundavg here, because I want to
246              * penalize the player with a large number of planes.
247              */
248             if (!player->simulation)
249                 avail = (sp->sct_avail * 100 - used) / 100;
250             else
251                 avail = (gt_bg_nmbr(bp, sp, I_MAX + 1) * 100 - used) / 100;
252
253             if (avail < 0)
254                 avail = 0;
255             if (!player->simulation)
256                 sp->sct_avail = avail;
257             else
258                 pt_bg_nmbr(bp, sp, I_MAX + 1, avail);
259
260             if (sp->sct_type != SCT_AIRPT)
261                 build /= 3;
262             if (onship) {
263                 if ((pp->pln_effic + build) > 80)
264                     build = 80 - pp->pln_effic;
265             }
266             np->nat_money -= roundavg(mult * build *
267                                       desc->pl_cost / 100.0);
268             air_money[pp->pln_own] += np->nat_money - start_money;
269
270             if (!player->simulation)
271                 pp->pln_effic += (s_char)build;
272             else
273                 np->nat_money = start_money;
274             k++;
275         }
276     }
277     return k;
278 }