]> git.pond.sub.org Git - empserver/blob - src/lib/update/plane.c
Import of Empire 4.2.12
[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, pp->pln_y);
125                                 pp->pln_own = 0;
126                                 continue;
127                         }
128                 }
129                 np = getnatp(pp->pln_own);
130                 desc = &plchr[(int)pp->pln_type];
131                 sp = getsectp(pp->pln_x, pp->pln_y);
132                 getvec(VT_ITEM, svec, (s_char *)sp, EF_SECTOR);
133                 mult=1;
134                 if (np->nat_level[NAT_TLEV] < pp->pln_tech * 0.85)
135                         mult = 2;
136
137                 if (buildem == 0) {
138                 /* flight pay is 5x the pay received by other military */
139                         start_money = np->nat_money;
140                         cost = -(mult * etus *
141                                  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",
147                                            prplane(pp));
148                                         makelost(EF_PLANE, pp->pln_own, pp->pln_uid, pp->pln_x, 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 ||
172                             np->nat_money < 0)
173                                 continue;
174
175                         start_money = np->nat_money;
176                         left = 100 - pp->pln_effic;
177                         if (left <= 0)
178                                 continue;
179
180                         if (!player->simulation)
181                           avail = sp->sct_avail * 100;
182                         else
183                           avail = gt_bg_nmbr(bp, sp, I_MAX+1) * 100;
184
185                         if (pp->pln_ship >= 0) {
186                                 int     vec[I_MAX+1];
187                                 shp = getshipp(pp->pln_ship);
188                                 getvec(VT_ITEM, vec, (s_char *)shp, EF_SHIP);
189                                 avail += (etus * vec[I_MILIT]/2);
190                         }
191                         w_p_eff = 20 + (desc->pl_lcm + 2 * desc->pl_hcm);
192                         delta = roundavg((double)avail/w_p_eff);
193                         if (delta <= 0)
194                                 continue;
195                         if (delta > etus*plane_grow_scale)
196                                 delta = etus*plane_grow_scale;
197                         if (delta > left)
198                                 delta = left;
199
200                         /* delta is the max amount we can grow */
201
202                         left = 100 - pp->pln_effic;
203                         if (left > delta)
204                                 left = delta;
205
206                         leftp = ((float)left/100.0);
207                         bzero((s_char *)mvec, sizeof(mvec));
208                         mvec[I_MILIT] = mil_needed = 
209                                      ldround((double)(plp->pl_crew * leftp),1);
210                         mvec[I_LCM] = lcm_needed = 
211                                      ldround((double)(plp->pl_lcm * leftp),1);
212                         mvec[I_HCM] = hcm_needed = 
213                                      ldround((double)(plp->pl_hcm * leftp),1);
214
215                         get_materials(sp, bp, mvec, 0);
216  
217                         if (mvec[I_MILIT]>=mil_needed)
218                                 buildp=leftp;
219                         else
220                                 buildp=((float)mvec[I_MILIT]/(float)plp->pl_crew);
221         
222                         if (mvec[I_LCM] < lcm_needed)
223                                 buildp = MIN(buildp,((float)mvec[I_LCM]/
224                                         (float)plp->pl_lcm));
225
226                         if (mvec[I_HCM] < hcm_needed)
227                                 buildp = MIN(buildp,((float)mvec[I_HCM]/
228                                         (float)plp->pl_hcm));
229
230                         build=ldround((double)(buildp*100.0),1);
231                         bzero((s_char *)mvec, sizeof(mvec));
232                         mvec[I_MILIT] = mil_needed = 
233                                      roundavg((double)(plp->pl_crew * buildp));
234                         mvec[I_LCM] = lcm_needed = 
235                                      roundavg((double)(plp->pl_lcm * buildp));
236                         mvec[I_HCM] = hcm_needed = 
237                                      roundavg((double)(plp->pl_hcm * buildp));
238
239                         get_materials(sp, bp, mvec, 1);
240
241                         if (onship) 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 }
279
280
281
282