]> git.pond.sub.org Git - empserver/blob - src/lib/update/plane.c
Declare all configuration variables in optlist.h. Include that
[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 long air_money[MAXNOC];
63     register struct plnstr *pp;
64     register struct plchrstr *plp;
65     struct natstr *np;
66     float leftp, buildp;
67     int left, build;
68     int lcm_needed, hcm_needed;
69     int mil_needed;
70     int svec[I_MAX + 1];
71     int mvec[I_MAX + 1];
72     int n, k = 0;
73     struct shpstr *shp;
74     struct plchrstr *desc;
75     struct sctstr *sp;
76     int delta;
77     int mult;
78     int cost;
79     int eff;
80     int avail;
81     int w_p_eff;
82     int used;
83     int start_money, onship = 0;
84
85     for (n = 0; NULL != (pp = getplanep(n)); n++) {
86         if (pp->pln_own == 0)
87             continue;
88         if (pp->pln_own != natnum)
89             continue;
90         if (pp->pln_effic < PLANE_MINEFF) {
91             makelost(EF_PLANE, pp->pln_own, pp->pln_uid,
92                      pp->pln_x, pp->pln_y);
93             pp->pln_own = 0;
94             continue;
95         }
96
97         plp = &plchr[(int)pp->pln_type];
98         if (pp->pln_flags & PLN_LAUNCHED) {
99             if (opt_ORBIT && (buildem == 0)) {
100                 if ((!player->simulation) &&
101                     (plp->pl_flags & P_O) &&
102                     (pp->pln_flags & PLN_LAUNCHED) &&
103                     !(plp->pl_flags & P_M) &&
104                     !(pp->pln_flags & PLN_SYNCHRONOUS))
105                     move_sat(pp);
106             }
107             continue;
108         }
109
110         onship = 0;
111         shp = (struct shpstr *)0;
112         if (pp->pln_ship >= 0 && (buildem == 1)) {
113             if (pp->pln_effic >= 80)
114                 continue;
115             onship = 1;
116             shp = getshipp(pp->pln_ship);
117             if (shp == 0 || shp->shp_own != pp->pln_own) {
118                 /* nplane is unsigned... */
119                 if (shp->shp_nplane > 0)
120                     shp->shp_nplane--;
121                 makelost(EF_PLANE, pp->pln_own, pp->pln_uid, pp->pln_x,
122                          pp->pln_y);
123                 pp->pln_own = 0;
124                 continue;
125             }
126         }
127         np = getnatp(pp->pln_own);
128         desc = &plchr[(int)pp->pln_type];
129         sp = getsectp(pp->pln_x, pp->pln_y);
130         getvec(VT_ITEM, svec, (s_char *)sp, EF_SECTOR);
131         mult = 1;
132         if (np->nat_level[NAT_TLEV] < pp->pln_tech * 0.85)
133             mult = 2;
134
135         if (buildem == 0) {
136             /* flight pay is 5x the pay received by other military */
137             start_money = np->nat_money;
138             cost = -(mult * etus * dmin(0.0, desc->pl_cost * money_plane));
139             if ((np->nat_priorities[PRI_PMAINT] == 0 ||
140                  np->nat_money < cost) && !player->simulation) {
141                 if ((eff = pp->pln_effic - etus / 5) < PLANE_MINEFF) {
142                     wu(0, pp->pln_own,
143                        "%s lost to lack of maintenance\n", prplane(pp));
144                     makelost(EF_PLANE, pp->pln_own, pp->pln_uid, pp->pln_x,
145                              pp->pln_y);
146                     pp->pln_own = 0;
147                     continue;
148                 }
149                 wu(0, pp->pln_own,
150                    "%s lost %d%% to lack of maintenance\n",
151                    prplane(pp), pp->pln_effic - eff);
152                 pp->pln_effic = eff;
153             } else {
154                 np->nat_money -= cost;
155             }
156
157             np->nat_money += (etus * plp->pl_crew * money_mil * 5);
158
159             air_money[pp->pln_own] += np->nat_money - start_money;
160             k++;
161             if (player->simulation)
162                 np->nat_money = start_money;
163             if ((pp->pln_flags & PLN_LAUNCHED) == PLN_LAUNCHED)
164                 continue;
165         } else {
166             if (sp->sct_off)
167                 continue;
168             if (np->nat_priorities[PRI_PBUILD] == 0 || np->nat_money < 0)
169                 continue;
170
171             start_money = np->nat_money;
172             left = 100 - pp->pln_effic;
173             if (left <= 0)
174                 continue;
175
176             if (!player->simulation)
177                 avail = sp->sct_avail * 100;
178             else
179                 avail = gt_bg_nmbr(bp, sp, I_MAX + 1) * 100;
180
181             if (pp->pln_ship >= 0) {
182                 int vec[I_MAX + 1];
183                 shp = getshipp(pp->pln_ship);
184                 getvec(VT_ITEM, vec, (s_char *)shp, EF_SHIP);
185                 avail += (etus * vec[I_MILIT] / 2);
186             }
187             w_p_eff = 20 + (desc->pl_lcm + 2 * desc->pl_hcm);
188             delta = roundavg((double)avail / w_p_eff);
189             if (delta <= 0)
190                 continue;
191             if (delta > etus * plane_grow_scale)
192                 delta = etus * plane_grow_scale;
193             if (delta > left)
194                 delta = left;
195
196             /* delta is the max amount we can grow */
197
198             left = 100 - pp->pln_effic;
199             if (left > delta)
200                 left = delta;
201
202             leftp = ((float)left / 100.0);
203             memset(mvec, 0, sizeof(mvec));
204             mvec[I_MILIT] = mil_needed =
205                 ldround((double)(plp->pl_crew * leftp), 1);
206             mvec[I_LCM] = lcm_needed =
207                 ldround((double)(plp->pl_lcm * leftp), 1);
208             mvec[I_HCM] = hcm_needed =
209                 ldround((double)(plp->pl_hcm * leftp), 1);
210
211             get_materials(sp, bp, mvec, 0);
212
213             if (mvec[I_MILIT] >= mil_needed)
214                 buildp = leftp;
215             else
216                 buildp = ((float)mvec[I_MILIT] / (float)plp->pl_crew);
217
218             if (mvec[I_LCM] < lcm_needed)
219                 buildp = MIN(buildp, ((float)mvec[I_LCM] /
220                                       (float)plp->pl_lcm));
221
222             if (mvec[I_HCM] < hcm_needed)
223                 buildp = MIN(buildp, ((float)mvec[I_HCM] /
224                                       (float)plp->pl_hcm));
225
226             build = ldround((double)(buildp * 100.0), 1);
227             memset(mvec, 0, sizeof(mvec));
228             mvec[I_MILIT] = mil_needed =
229                 roundavg((double)(plp->pl_crew * buildp));
230             mvec[I_LCM] = lcm_needed =
231                 roundavg((double)(plp->pl_lcm * buildp));
232             mvec[I_HCM] = hcm_needed =
233                 roundavg((double)(plp->pl_hcm * buildp));
234
235             get_materials(sp, bp, mvec, 1);
236
237             if (onship)
238                 build = delta;
239             used = build * w_p_eff;
240
241             /*
242              * I didn't use roundavg here, because I want to
243              * penalize the player with a large number of planes.
244              */
245             if (!player->simulation)
246                 avail = (sp->sct_avail * 100 - used) / 100;
247             else
248                 avail = (gt_bg_nmbr(bp, sp, I_MAX + 1) * 100 - used) / 100;
249
250             if (avail < 0)
251                 avail = 0;
252             if (!player->simulation)
253                 sp->sct_avail = avail;
254             else
255                 pt_bg_nmbr(bp, sp, I_MAX + 1, avail);
256
257             if (sp->sct_type != SCT_AIRPT)
258                 build /= 3;
259             if (onship) {
260                 if ((pp->pln_effic + build) > 80)
261                     build = 80 - pp->pln_effic;
262             }
263             np->nat_money -= roundavg(mult * build *
264                                       desc->pl_cost / 100.0);
265             air_money[pp->pln_own] += np->nat_money - start_money;
266
267             if (!player->simulation)
268                 pp->pln_effic += (s_char)build;
269             else
270                 np->nat_money = start_money;
271             k++;
272         }
273     }
274     return k;
275 }