]> git.pond.sub.org Git - empserver/blob - src/lib/commands/buil.c
Supply prototypes where possible. This uncovered type errors with
[empserver] / src / lib / commands / buil.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  *  buil.c: Build ships, nukes, bridges, planes, land units, bridge towers
29  * 
30  *  Known contributors to this file:
31  *      Steve McClure, 1998-2000
32  */
33
34 #include <math.h>
35 #ifdef Rel4
36 #include <string.h>
37 #endif /* Rel4 */
38 #include "misc.h"
39 #include "player.h"
40 #include "var.h"
41 #include "sect.h"
42 #include "nat.h"
43 #include "retreat.h"
44 #include "ship.h"
45 #include "land.h"
46 #include "nuke.h"
47 #include "plane.h"
48 #include "xy.h"
49 #include "nsc.h"
50 #include "treaty.h"
51 #include "deity.h"
52 #include "file.h"
53 #include "path.h"
54 #include "optlist.h"
55 #include "commands.h"
56
57 static int build_nuke(register struct sctstr *sp,
58                       register struct nchrstr *np, register int *vec);
59 static int build_ship(register struct sctstr *sp,
60                       register struct mchrstr *mp, register int *vec,
61                       int tlev);
62 static int build_land(register struct sctstr *sp,
63                       register struct lchrstr *lp, register int *vec,
64                       int tlev);
65 static int build_bridge(register struct sctstr *sp, register int *vec);
66 static int build_tower(register struct sctstr *sp, register int *vec);
67 static int build_plane(register struct sctstr *sp,
68                        register struct plchrstr *pp, register int *vec,
69                        int tlev);
70
71 static int cash;                /* static ok */
72
73 extern int morale_base;
74 extern int sect_mob_neg_factor;
75 extern int etu_per_update;
76
77 /*
78  * build <WHAT> <SECTS> <TYPE|DIR|MEG> [NUMBER]
79  */
80 int
81 buil(void)
82 {
83     extern double buil_bt;
84     extern double buil_tower_bt;
85     struct sctstr sect;
86     struct nstr_sect nstr;
87     struct natstr *natp;
88     int rqtech;
89     int tlev;
90     int rlev;
91     int n;
92     int type;
93     int what;
94     struct lchrstr *lp;
95     struct mchrstr *mp;
96     struct plchrstr *pp;
97     struct nchrstr *np;
98     s_char *p;
99     int vec[I_MAX + 1];
100     int gotsect = 0;
101     int built;
102     int hold, found, number = 1, x;
103     extern float drnuke_const;
104     int asked = 0;
105     s_char buf[1024];
106
107     natp = getnatp(player->cnum);
108     if ((p =
109          getstarg(player->argp[1],
110                   "Build (ship, nuke, bridge, plane, land unit, tower)? ",
111                   buf)) == 0)
112         return RET_SYN;
113     what = *p;
114
115     for (x = 0; x < number; x++) {
116         if (!snxtsct(&nstr, player->argp[2])) {
117             pr("Bad sector specification.\n");
118             return RET_SYN;
119         }
120       ask_again:
121         tlev = (int)natp->nat_level[NAT_TLEV];
122         rlev = (int)natp->nat_level[NAT_RLEV];
123
124         switch (what) {
125         case 'p':
126             p = getstarg(player->argp[3], "Plane type? ", buf);
127             if (p == 0 || *p == 0)
128                 return RET_SYN;
129             n = strlen(p);
130             while (n && iscntrl(p[n - 1]))
131                 n--;
132             if (!n)
133                 return RET_SYN;
134             for (found = 0, type = 0, pp = plchr; type <= pln_maxno;
135                  type++, pp++) {
136                 if (pp->pl_tech > tlev)
137                     continue;
138                 if (pp->pl_name && strncmp(p, pp->pl_name, n) == 0) {
139                     found++;
140                     hold = type;
141                     break;
142                 }
143             }
144             if (found != 1) {
145                 pr("Illegal plane type: \"%s\"\n", p);
146                 if (confirm("List plane types? "))
147                     show_plane_build(tlev);
148                 player->argp[3] = 0;
149                 goto ask_again;
150             }
151             type = hold;
152             pp = &(plchr[type]);
153             rqtech = pp->pl_tech;
154             break;
155         case 's':
156             p = getstarg(player->argp[3], "Ship type? ", buf);
157             if (p == 0 || *p == 0)
158                 return RET_SYN;
159             n = strlen(p);
160             while (n && iscntrl(p[n - 1]))
161                 n--;
162             if (!n)
163                 return RET_SYN;
164             for (found = 0, mp = mchr, type = 0; type <= shp_maxno;
165                  type++, mp++) {
166                 if (mp->m_tech > tlev)
167                     continue;
168                 /* Can't build trade ships unless it's turned on */
169                 if ((mp->m_flags & M_TRADE) && !opt_TRADESHIPS)
170                     continue;
171                 if (mp->m_name && strncmp(p, mp->m_name, n) == 0) {
172                     found++;
173                     hold = type;
174                     break;
175                 }
176             }
177             if (found != 1) {
178                 pr("Illegal ship type: \"%s\"\n", p);
179                 if (confirm("List ship types? "))
180                     show_ship_build(tlev);
181                 player->argp[3] = 0;
182                 goto ask_again;
183             }
184             type = hold;
185             mp = &(mchr[type]);
186             rqtech = mp->m_tech;
187             break;
188         case 'l':
189             p = getstarg(player->argp[3], "Land unit type? ", buf);
190             if (p == 0 || *p == 0)
191                 return RET_SYN;
192             n = strlen(p);
193             while (n && iscntrl(p[n - 1]))
194                 n--;
195             if (!n)
196                 return RET_SYN;
197             for (found = 0, lp = lchr, type = 0; type <= lnd_maxno;
198                  type++, lp++) {
199                 if (lp->l_tech > tlev)
200                     continue;
201                 if ((lp->l_flags & L_SPY) && !opt_LANDSPIES)
202                     continue;
203                 if (lp->l_name && strncmp(p, lp->l_name, n) == 0) {
204                     found++;
205                     hold = type;
206                     break;
207                 }
208             }
209             if (found != 1) {
210                 pr("Illegal land unit type: \"%s\"\n", p);
211                 if (confirm("List unit types? "))
212                     show_land_build(tlev);
213                 player->argp[3] = 0;
214                 goto ask_again;
215             }
216             type = hold;
217             lp = &(lchr[type]);
218             rqtech = lp->l_tech;
219             break;
220         case 'b':
221             if (natp->nat_level[NAT_TLEV] + 0.005 < buil_bt) {
222                 pr("Building a span requires a tech of %.0f\n", buil_bt);
223                 return 2;
224             }
225             break;
226         case 't':
227             if (!opt_BRIDGETOWERS) {
228                 pr("Bridge tower building is disabled.\n");
229                 return RET_FAIL;
230             }
231             if (natp->nat_level[NAT_TLEV] + 0.005 < buil_tower_bt) {
232                 pr("Building a tower requires a tech of %.0f\n",
233                    buil_tower_bt);
234                 return 2;
235             }
236             break;
237         case 'n':
238             if (opt_NONUKES) {
239                 pr("There are no nukes in this game.\n");
240                 return RET_FAIL;
241             }
242             p = getstarg(player->argp[3], "Nuke type? ", buf);
243             if (p == 0 || *p == 0)
244                 return RET_SYN;
245             n = strlen(p);
246             while (n && iscntrl(p[n - 1]))
247                 n--;
248             if (!n)
249                 return RET_SYN;
250             for (found = 0, np = nchr, type = 0; type < nuk_maxno;
251                  type++, np++) {
252                 if ((np->n_tech > tlev)
253                     || (opt_DRNUKE
254                         && ((np->n_tech * drnuke_const) > rlev)))
255                     continue;
256                 if (opt_NEUTRON == 0 && (np->n_flags & N_NEUT))
257                     continue;
258
259                 if (np->n_name && strncmp(p, np->n_name, n) == 0) {
260                     found++;
261                     hold = type;
262                     break;
263                 }
264             }
265             if (found != 1) {
266                 int tt = tlev;
267                 pr("Possible nuke types are:\n");
268                 if (opt_DRNUKE)
269                     tt = (tlev < (rlev / drnuke_const) ? (int)tlev :
270                           (int)(rlev / drnuke_const));
271
272                 show_nuke_build(tt);
273                 player->argp[3] = 0;
274                 goto ask_again;
275             }
276             type = hold;
277             np = &(nchr[type]);
278             rqtech = np->n_tech;
279             break;
280         default:
281             pr("You can't build that!\n");
282             return RET_FAIL;
283         }
284         if (what != 'b' && what != 't') {
285             if (player->argp[4]) {
286                 if (atoi(player->argp[4]) > 20 && !asked) {
287                     s_char bstr[80];
288                     asked = 1;
289                     (void)sprintf(bstr,
290                                   "Are you sure that you want to build %s of them? ",
291                                   player->argp[4]);
292                     p = getstarg(player->argp[6], bstr, buf);
293                     if (p == 0 || *p != 'y')
294                         return RET_SYN;
295                 }
296                 number = atoi(player->argp[4]);
297             }
298         }
299         if (what != 'b' && what != 'n' && what != 't') {
300             if (player->argp[5]) {
301                 tlev = atoi(player->argp[5]);
302                 if (tlev > natp->nat_level[NAT_TLEV] && !player->god) {
303                     pr("Your tech level is only %d.\n",
304                        (int)natp->nat_level[NAT_TLEV]);
305                     return RET_FAIL;
306                 }
307                 if (rqtech > tlev) {
308                     pr("Required tech is %d.\n", rqtech);
309                     return RET_FAIL;
310                 }
311                 pr("building with tech level %d.\n", tlev);
312             } else
313                 tlev = (int)natp->nat_level[NAT_TLEV];
314         }
315         cash = natp->nat_money;
316         while (nxtsct(&nstr, &sect)) {
317             gotsect++;
318             if (!player->owner)
319                 continue;
320             getvec(VT_ITEM, vec, (s_char *)&sect, EF_SECTOR);
321             switch (what) {
322             case 'l':
323                 built = build_land(&sect, lp, vec, tlev);
324                 break;
325             case 's':
326                 built = build_ship(&sect, mp, vec, tlev);
327                 break;
328             case 'b':
329                 built = build_bridge(&sect, vec);
330                 break;
331             case 't':
332                 built = build_tower(&sect, vec);
333                 break;
334             case 'n':
335                 built = build_nuke(&sect, np, vec);
336                 break;
337             case 'p':
338                 built = build_plane(&sect, pp, vec, tlev);
339                 break;
340             default:
341                 pr("internal error in build (%d)\n", what);
342                 return RET_FAIL;
343             }
344             if (built) {
345                 putvec(VT_ITEM, vec, (s_char *)&sect, EF_SECTOR);
346                 putsect(&sect);
347             }
348         }
349     }
350     if (!gotsect) {
351         pr("Bad sector specification.\n");
352     }
353     return RET_OK;
354 }
355
356 static int
357 build_ship(register struct sctstr *sp, register struct mchrstr *mp,
358            register int *vec, int tlev)
359 {
360     struct shpstr ship;
361     struct nstr_item nstr;
362     int cost, i;
363     int w_p_eff, x;
364     float eff = ((float)SHIP_MINEFF / 100.0);
365     int points, lcm, hcm;
366     int freeship = 0;
367     int techdiff;
368
369     hcm = roundavg(((double)mp->m_hcm * (double)eff));
370     lcm = roundavg(((double)mp->m_lcm * (double)eff));
371
372     if (sp->sct_type != SCT_HARBR) {
373         pr("Ships must be built in harbours.\n");
374         return 0;
375     }
376     if (sp->sct_effic < 60 && !player->god) {
377         pr("Sector %s is not 60%% efficient.\n",
378            xyas(sp->sct_x, sp->sct_y, player->cnum));
379         return 0;
380     }
381     if (vec[I_LCM] < lcm || vec[I_HCM] < hcm) {
382         pr("Not enough materials in %s\n",
383            xyas(sp->sct_x, sp->sct_y, player->cnum));
384         return 0;
385     }
386     w_p_eff = (20 + mp->m_lcm + (mp->m_hcm * 2));
387     points = sp->sct_avail * 100 / w_p_eff;
388     if (points < SHIP_MINEFF) {
389         pr("Not enough available work in %s to build a %s\n",
390            xyas(sp->sct_x, sp->sct_y, player->cnum), mp->m_name);
391         pr(" (%d available work required)\n",
392            1 + (w_p_eff * SHIP_MINEFF) / 100);
393         return 0;
394     }
395     cost = mp->m_cost * eff;
396     if (cash < cost) {
397         pr("Not enough money left to build a %s\n", mp->m_name);
398         return 0;
399     }
400     if (!trechk(player->cnum, 0, NEWSHP))
401         return 0;
402     if (!check_sect_ok(sp))
403         return 0;
404     sp->sct_avail = (sp->sct_avail * 100 - w_p_eff * SHIP_MINEFF) / 100;
405     player->dolcost += cost;
406     cash -= cost;
407     snxtitem_all(&nstr, EF_SHIP);
408     while (nxtitem(&nstr, (s_char *)&ship)) {
409         if (ship.shp_own == 0) {
410             freeship++;
411             break;
412         }
413     }
414     if (freeship == 0) {
415         ef_extend(EF_SHIP, 50);
416     }
417     memset(&ship, 0, sizeof(struct shpstr));
418     ship.shp_x = sp->sct_x;
419     ship.shp_y = sp->sct_y;
420     ship.shp_destx[0] = sp->sct_x;
421     ship.shp_desty[0] = sp->sct_y;
422     ship.shp_destx[1] = sp->sct_x;
423     ship.shp_desty[1] = sp->sct_y;
424     ship.shp_autonav = 0;
425     /* new code for autonav, Chad Zabel 1-15-94 */
426     for (i = 0; i < TMAX; ++i) {
427         ship.shp_tstart[i] = ' ';
428         ship.shp_tend[i] = ' ';
429         ship.shp_lstart[i] = 0;
430         ship.shp_lend[i] = 0;
431     }
432     ship.shp_mission = 0;
433     ship.shp_own = player->cnum;
434     ship.shp_type = mp - mchr;
435     ship.shp_effic = SHIP_MINEFF;
436     if (opt_MOB_ACCESS) {
437         time(&ship.shp_access);
438         ship.shp_mobil = -(etu_per_update / sect_mob_neg_factor);
439     } else {
440         ship.shp_mobil = 0;
441     }
442     ship.shp_uid = nstr.cur;
443     ship.shp_nplane = 0;
444     ship.shp_nland = 0;
445     ship.shp_nxlight = 0;
446     ship.shp_nchoppers = 0;
447     ship.shp_fleet = ' ';
448     ship.shp_nv = 0;
449     ship.shp_sell = 0;
450     ship.shp_tech = tlev;
451
452     techdiff = (int)(tlev - mp->m_tech);
453     ship.shp_armor = (short)SHP_DEF(mp->m_armor, techdiff);
454     ship.shp_speed = (short)SHP_SPD(mp->m_speed, techdiff);
455     ship.shp_visib = (short)SHP_VIS(mp->m_visib, techdiff);
456     ship.shp_frnge = (short)SHP_RNG(mp->m_frnge, techdiff);
457     ship.shp_glim = (short)SHP_FIR(mp->m_glim, techdiff);
458
459     ship.shp_mobquota = 0;
460     *ship.shp_path = 0;
461     ship.shp_follow = nstr.cur;
462     ship.shp_name[0] = 0;
463     ship.shp_orig_own = player->cnum;
464     ship.shp_orig_x = sp->sct_x;
465     ship.shp_orig_y = sp->sct_y;
466     ship.shp_fuel = mchr[(int)ship.shp_type].m_fuelc;
467     ship.shp_rflags = 0;
468     for (x = 0; x < 10; x++)
469         ship.shp_rpath[x] = 0;
470
471     vec[I_LCM] -= lcm;
472     vec[I_HCM] -= hcm;
473
474     if (getvar(V_PSTAGE, (s_char *)sp, EF_SECTOR) == PLG_INFECT)
475         putvar(V_PSTAGE, PLG_EXPOSED, (s_char *)&ship, EF_SHIP);
476     makenotlost(EF_SHIP, ship.shp_own, ship.shp_uid, ship.shp_x,
477                 ship.shp_y);
478     putship(ship.shp_uid, &ship);
479     pr("%s", prship(&ship));
480     pr(" built in sector %s\n", xyas(sp->sct_x, sp->sct_y, player->cnum));
481     return 1;
482 }
483
484 static int
485 build_land(register struct sctstr *sp, register struct lchrstr *lp,
486            register int *vec, int tlev)
487 {
488     struct lndstr land;
489     struct nstr_item nstr;
490     int cost;
491     int w_p_eff;
492     int points;
493     struct natstr *natp;
494     float eff = ((float)LAND_MINEFF / 100.0);
495     int mil, lcm, hcm, gun, shell;
496     int freeland = 0;
497
498     /*
499        mil = roundavg(((double)lp->l_mil * (double)eff));
500        shell = roundavg(((double)lp->l_shell * (double)eff));
501        gun = roundavg(((double)lp->l_gun * (double)eff));
502      */
503     mil = shell = gun = 0;
504     hcm = roundavg(((double)lp->l_hcm * (double)eff));
505     lcm = roundavg(((double)lp->l_lcm * (double)eff));
506
507     natp = getnatp(player->cnum);
508
509     if (sp->sct_type != SCT_HEADQ) {
510         pr("Land Units must be built in headquarters.\n");
511         return 0;
512     }
513     if (sp->sct_effic < 60 && !player->god) {
514         pr("Sector %s is not 60%% efficient.\n",
515            xyas(sp->sct_x, sp->sct_y, player->cnum));
516         return 0;
517     }
518     if (vec[I_LCM] < lcm || vec[I_HCM] < hcm) {
519         pr("Not enough materials in %s\n",
520            xyas(sp->sct_x, sp->sct_y, player->cnum));
521         return 0;
522     }
523 #if 0
524     if (vec[I_GUN] < gun || vec[I_GUN] == 0) {
525         pr("Not enough guns in %s\n",
526            xyas(sp->sct_x, sp->sct_y, player->cnum));
527         return 0;
528     }
529     if (vec[I_SHELL] < shell) {
530         pr("Not enough shells in %s\n",
531            xyas(sp->sct_x, sp->sct_y, player->cnum));
532         return 0;
533     }
534     if (vec[I_MILIT] < mil) {
535         pr("Not enough military in %s\n",
536            xyas(sp->sct_x, sp->sct_y, player->cnum));
537         return 0;
538     }
539 #endif
540     if (!trechk(player->cnum, 0, NEWLND))
541         return 0;
542     if (!check_sect_ok(sp))
543         return 0;
544     w_p_eff = (20 + lp->l_lcm + (lp->l_hcm * 2));
545     points = sp->sct_avail * 100 / w_p_eff;
546     if (points < LAND_MINEFF) {
547         pr("Not enough available work in %s to build a %s\n",
548            xyas(sp->sct_x, sp->sct_y, player->cnum), lp->l_name);
549         pr(" (%d available work required)\n",
550            1 + (w_p_eff * LAND_MINEFF) / 100);
551         return 0;
552     }
553     cost = ((float)lp->l_cost * eff);
554     /*  cost = (int)LND_COST(cost, tlev - lp->l_tech); */
555     if (cash < cost) {
556         pr("Not enough money left to build a %s\n", lp->l_name);
557         return 0;
558     }
559     sp->sct_avail = (sp->sct_avail * 100 - w_p_eff * LAND_MINEFF) / 100;
560     player->dolcost += cost;
561     cash -= cost;
562     snxtitem_all(&nstr, EF_LAND);
563     while (nxtitem(&nstr, (s_char *)&land)) {
564         if (land.lnd_own == 0) {
565             freeland++;
566             break;
567         }
568     }
569     if (freeland == 0) {
570         ef_extend(EF_LAND, 50);
571     }
572     memset(&land, 0, sizeof(struct lndstr));
573     land.lnd_x = sp->sct_x;
574     land.lnd_y = sp->sct_y;
575     land.lnd_own = player->cnum;
576     land.lnd_mission = 0;
577     land.lnd_type = lp - lchr;
578     land.lnd_effic = LAND_MINEFF;
579     if (opt_MOB_ACCESS) {
580         time(&land.lnd_access);
581         land.lnd_mobil = -(etu_per_update / sect_mob_neg_factor);
582     } else {
583         land.lnd_mobil = 0;
584     }
585     land.lnd_sell = 0;
586     land.lnd_tech = tlev;
587     land.lnd_uid = nstr.cur;
588     land.lnd_army = ' ';
589     land.lnd_flags = 0;
590     land.lnd_ship = -1;
591     land.lnd_land = -1;
592     land.lnd_nland = 0;
593     land.lnd_harden = 0;
594     land.lnd_retreat = morale_base;
595     land.lnd_fuel = lp->l_fuelc;
596     land.lnd_nxlight = 0;
597     land.lnd_rflags = 0;
598     memset(land.lnd_rpath, 0, sizeof(land.lnd_rpath));
599     land.lnd_rad_max = 0;
600     land.lnd_nv = 0;
601     land.lnd_att = (float)LND_ATTDEF(lp->l_att, tlev - lp->l_tech);
602     land.lnd_def = (float)LND_ATTDEF(lp->l_def, tlev - lp->l_tech);
603     land.lnd_vul = (int)LND_VUL(lp->l_vul, tlev - lp->l_tech);
604     land.lnd_spd = (int)LND_SPD(lp->l_spd, tlev - lp->l_tech);
605     land.lnd_vis = (int)LND_VIS(lp->l_vis, tlev - lp->l_tech);
606     land.lnd_spy = (int)LND_SPY(lp->l_spy, tlev - lp->l_tech);
607     land.lnd_rad = (int)LND_RAD(lp->l_rad, tlev - lp->l_tech);
608     land.lnd_frg = (int)LND_FRG(lp->l_frg, tlev - lp->l_tech);
609     land.lnd_acc = (int)LND_ACC(lp->l_acc, tlev - lp->l_tech);
610     land.lnd_dam = (int)LND_DAM(lp->l_dam, tlev - lp->l_tech);
611     land.lnd_ammo = (int)LND_AMM(lp->l_ammo, lp->l_dam, tlev - lp->l_tech);
612     land.lnd_aaf = (int)LND_AAF(lp->l_aaf, tlev - lp->l_tech);
613     land.lnd_fuelc = (int)LND_FC(lp->l_fuelc, tlev - lp->l_tech);
614     land.lnd_fuelu = (int)LND_FU(lp->l_fuelu, tlev - lp->l_tech);
615     land.lnd_maxlight = (int)LND_XPL(lp->l_nxlight, tlev - lp->l_tech);
616     land.lnd_maxland = (int)LND_MXL(lp->l_mxland, tlev - lp->l_tech);
617
618     vec[I_LCM] -= lcm;
619     vec[I_HCM] -= hcm;
620     vec[I_MILIT] -= mil;
621     vec[I_GUN] -= gun;
622     vec[I_SHELL] -= shell;
623
624 /* Disabled autoloading of food onto units
625     max_amt = vl_find(V_FOOD, lp->l_vtype, lp->l_vamt, (int) lp->l_nv);
626     food_needed = (etu_per_update * eatrate) *
627         (vec[I_CIVIL] + vec[I_MILIT] + vec[I_UW])+1;
628     if ((vec[I_FOOD]-max_amt) < food_needed)
629         max_amt = (vec[I_FOOD]-food_needed);
630     
631     if (max_amt < 0)
632         max_amt = 0;
633     
634     vec[I_FOOD] -= max_amt;
635     
636     memset(lvec, 0, sizeof(lvec));
637     getvec(VT_ITEM, lvec, (s_char *)&land, EF_LAND);
638     lvec[I_FOOD] += max_amt;
639     putvec(VT_ITEM, lvec, (s_char *)&land, EF_LAND);
640 */
641
642     if (getvar(V_PSTAGE, (s_char *)sp, EF_SECTOR) == PLG_INFECT)
643         putvar(V_PSTAGE, PLG_EXPOSED, (s_char *)&land, EF_LAND);
644     putland(nstr.cur, &land);
645     makenotlost(EF_LAND, land.lnd_own, land.lnd_uid, land.lnd_x,
646                 land.lnd_y);
647     pr("%s", prland(&land));
648     pr(" built in sector %s\n", xyas(sp->sct_x, sp->sct_y, player->cnum));
649     return 1;
650 }
651
652 static int
653 build_bridge(register struct sctstr *sp, register int *vec)
654 {
655     extern int buil_bh;
656     extern double buil_bc;
657     struct sctstr sect;
658     int val;
659     int newx, newy;
660     int w_p_eff;
661     int points;
662     int nx, ny, i, good = 0;
663     s_char *p;
664     s_char buf[1024];
665
666     if (opt_EASY_BRIDGES == 0) {        /* must have a bridge head or tower */
667         if (sp->sct_type != SCT_BTOWER) {
668             if (sp->sct_type != SCT_BHEAD)
669                 return 0;
670             if (sp->sct_newtype != SCT_BHEAD)
671                 return 0;
672         }
673     }
674 #if 0
675     else {
676
677         for (i = 1; i <= 6; i++) {
678             struct sctstr s2;
679             nx = sp->sct_x + diroff[i][0];
680             ny = sp->sct_y + diroff[i][1];
681             getsect(nx, ny, &sect);
682             for (j = 1; j <= 6; j++) {
683                 nx2 = sect.sct_x + diroff[j][0];
684                 ny2 = sect.sct_y + diroff[j][1];
685                 getsect(nx2, ny2, &s2);
686                 if ((s2.sct_type != SCT_WATER) &&
687                     (s2.sct_type != SCT_BSPAN))
688                     good = 1;
689             }
690
691         }
692
693         if (!good) {
694             pr("Bridges must be built adjacent to land or bridge towers.\n");
695             pr("No eligible sectors adjacent to this sector.\n");
696             return 0;
697         }
698     }                           /* end EASY_BRIDGES */
699 #endif
700
701     if (sp->sct_effic < 60 && !player->god) {
702         pr("Sector %s is not 60%% efficient.\n",
703            xyas(sp->sct_x, sp->sct_y, player->cnum));
704         return 0;
705     }
706
707     if (!opt_NO_HCMS) {
708         if (vec[I_HCM] < buil_bh) {
709             pr("%s only has %d unit%s of hcm,\n",
710                xyas(sp->sct_x, sp->sct_y, player->cnum),
711                vec[I_HCM], vec[I_HCM] > 1 ? "s" : "");
712             pr("(a bridge span requires %d)\n", buil_bh);
713             return 0;
714         }
715     } else if (!opt_NO_LCMS) {
716         if (vec[I_LCM] < buil_bh) {
717             pr("%s only has %d unit%s of lcm,\n",
718                xyas(sp->sct_x, sp->sct_y, player->cnum),
719                vec[I_LCM], vec[I_LCM] > 1 ? "s" : "");
720             pr("(a bridge span requires %d)\n", buil_bh);
721             return 0;
722         }
723     }
724     if (cash < buil_bc) {
725         pr("A span costs $%.2f to build; ", buil_bc);
726         pr("you only have %d.\n", cash);
727         return 0;
728     }
729     w_p_eff = buil_bh * 2;
730     points = sp->sct_avail * 100 / w_p_eff;
731     if (points < 20) {
732         pr("Not enough available work in %s to build a bridge\n",
733            xyas(sp->sct_x, sp->sct_y, player->cnum));
734         pr(" (%d available work required)\n", 1 + (w_p_eff * 20) / 100);
735         return 0;
736     }
737     if (!player->argp[3]) {
738         pr("Bridge head at %s\n",
739            xyas(sp->sct_x, sp->sct_y, player->cnum));
740         nav_map(sp->sct_x, sp->sct_y, 1);
741     }
742     if (!(p = getstarg(player->argp[3], "build span in what direction? ", buf))
743         || !*p) {
744         return 0;
745     }
746     /* Sanity check time */
747     if (!check_sect_ok(sp))
748         return 0;
749
750     if ((val = chkdir(*p, DIR_FIRST, DIR_LAST)) < 0) {
751         pr("'%c' is not a valid direction...\n", *p);
752         direrr(0, 0, 0);
753         return 0;
754     }
755     newx = sp->sct_x + diroff[val][0];
756     newy = sp->sct_y + diroff[val][1];
757     if (getsect(newx, newy, &sect) == 0 || sect.sct_type != SCT_WATER) {
758         pr("%s is not a water sector\n", xyas(newx, newy, player->cnum));
759         return 0;
760     }
761     if (opt_EASY_BRIDGES) {
762         good = 0;
763
764         for (i = 1; i <= 6; i++) {
765             struct sctstr s2;
766             nx = sect.sct_x + diroff[i][0];
767             ny = sect.sct_y + diroff[i][1];
768             getsect(nx, ny, &s2);
769             if ((s2.sct_type != SCT_WATER) && (s2.sct_type != SCT_BSPAN))
770                 good = 1;
771         }
772         if (!good) {
773             pr("Bridges must be built adjacent to land or bridge towers.\n");
774             pr("That sector is not adjacent to land or a bridge tower.\n");
775             return 0;
776         }
777     }                           /* end EASY_BRIDGES */
778     sp->sct_avail = (sp->sct_avail * 100 - w_p_eff * 20) / 100;
779     player->dolcost += buil_bc;
780     cash -= buil_bc;
781     sect.sct_type = SCT_BSPAN;
782     sect.sct_newtype = SCT_BSPAN;
783     sect.sct_effic = 20;
784     sect.sct_road = 0;
785     sect.sct_rail = 0;
786     sect.sct_defense = 0;
787     if (!opt_DEFENSE_INFRA)
788         sect.sct_defense = sect.sct_effic;
789     if (opt_MOB_ACCESS) {
790         time(&sect.sct_access);
791         sect.sct_mobil = -(etu_per_update / sect_mob_neg_factor);
792     } else {
793         sect.sct_mobil = 0;
794     }
795     putvar(V_MINE, 0, (s_char *)&sect, EF_SECTOR);
796     putsect(&sect);
797     pr("Bridge span built over %s\n",
798        xyas(sect.sct_x, sect.sct_y, player->cnum));
799     if (!opt_NO_HCMS)
800         vec[I_HCM] -= buil_bh;
801     else if (!opt_NO_LCMS)
802         vec[I_LCM] -= buil_bh;
803     return 1;
804 }
805
806 static int
807 build_nuke(register struct sctstr *sp, register struct nchrstr *np,
808            register int *vec)
809 {
810     int w_p_eff;
811     int points;
812
813     if (sp->sct_type != SCT_NUKE && !player->god) {
814         pr("Nuclear weapons must be built in nuclear plants.\n");
815         return 0;
816     }
817     if (sp->sct_effic < 60 && !player->god) {
818         pr("Sector %s is not 60%% efficient.\n",
819            xyas(sp->sct_x, sp->sct_y, player->cnum));
820         return 0;
821     }
822     if (vec[I_HCM] < np->n_hcm || vec[I_LCM] < np->n_lcm ||
823         vec[I_OIL] < np->n_oil || vec[I_RAD] < np->n_rad) {
824         pr("Not enough materials for a %s bomb in %s\n",
825            np->n_name, xyas(sp->sct_x, sp->sct_y, player->cnum));
826         pr("(%d hcm, %d lcm, %d oil, & %d rads).\n",
827            np->n_hcm, np->n_lcm, np->n_oil, np->n_rad);
828         return 0;
829     }
830     if (cash < np->n_cost) {
831         pr("You need $%d, you only have %d.\n", np->n_cost, cash);
832         return 0;
833     }
834     w_p_eff = np->n_rad + np->n_oil + np->n_lcm + np->n_hcm * 2;
835     points = sp->sct_avail * 100 / w_p_eff;
836     /*
837      * XXX when nukes turn into units (or whatever), then
838      * make them start at 20%.  Since they don't have efficiency
839      * now, we choose 20% as a "big" number.
840      */
841     if (points < 20) {
842         pr("Not enough available work in %s to build a %s;\n",
843            xyas(sp->sct_x, sp->sct_y, player->cnum), np->n_name);
844         pr(" (%d available work required)\n", 1 + w_p_eff * 20 / 100);
845         return 0;
846     }
847     if (!trechk(player->cnum, 0, NEWNUK))
848         return 0;
849     if (!check_sect_ok(sp))
850         return 0;
851     sp->sct_avail = (sp->sct_avail * 100 - w_p_eff * 20) / 100;
852     player->dolcost += np->n_cost;
853     cash -= np->n_cost;
854     nuk_add(sp->sct_x, sp->sct_y, np - nchr, 1);
855     vec[I_HCM] -= np->n_hcm;
856     vec[I_LCM] -= np->n_lcm;
857     vec[I_OIL] -= np->n_oil;
858     vec[I_RAD] -= np->n_rad;
859     pr("%s warhead created in %s\n", np->n_name,
860        xyas(sp->sct_x, sp->sct_y, player->cnum));
861     return 1;
862 }
863
864 static int
865 build_plane(register struct sctstr *sp, register struct plchrstr *pp,
866             register int *vec, int tlev)
867 {
868     struct plnstr plane;
869     int cost;
870     struct nstr_item nstr;
871     float eff = ((float)PLANE_MINEFF / 100.0);
872     int points;
873     int w_p_eff;
874     int hcm, lcm, mil;
875     int freeplane = 0;
876
877     mil = roundavg(((double)pp->pl_crew * (double)eff));
878     /* Always use at least 1 mil to build a plane */
879     if (mil == 0 && pp->pl_crew > 0)
880         mil = 1;
881     hcm = roundavg(((double)pp->pl_hcm * (double)eff));
882     lcm = roundavg(((double)pp->pl_lcm * (double)eff));
883     if (sp->sct_type != SCT_AIRPT && !player->god) {
884         pr("Planes must be built in airports.\n");
885         return 0;
886     }
887     if (sp->sct_effic < 60 && !player->god) {
888         pr("Sector %s is not 60%% efficient.\n",
889            xyas(sp->sct_x, sp->sct_y, player->cnum));
890         return 0;
891     }
892     if (vec[I_LCM] < lcm || vec[I_HCM] < hcm) {
893         pr("Not enough materials in %s\n",
894            xyas(sp->sct_x, sp->sct_y, player->cnum));
895         return 0;
896     }
897     w_p_eff = (20 + pp->pl_lcm + (pp->pl_hcm * 2));
898     points = sp->sct_avail * 100 / w_p_eff;
899     if (points < PLANE_MINEFF) {
900         pr("Not enough available work in %s to build a %s\n",
901            xyas(sp->sct_x, sp->sct_y, player->cnum), pp->pl_name);
902         pr(" (%d available work required)\n",
903            1 + PLANE_MINEFF * w_p_eff / 100);
904         return 0;
905     }
906     cost = pp->pl_cost * eff;
907     if (cash < cost) {
908         pr("Not enough money left to build a %s\n", pp->pl_name);
909         return 0;
910     }
911     if (vec[I_MILIT] < mil || (vec[I_MILIT] == 0 && pp->pl_crew > 0)) {
912         pr("Not enough military for crew in %s\n",
913            xyas(sp->sct_x, sp->sct_y, player->cnum));
914         return 0;
915     }
916     if (!trechk(player->cnum, 0, NEWPLN))
917         return 0;
918     if (!check_sect_ok(sp))
919         return 0;
920     sp->sct_avail = (sp->sct_avail * 100 - w_p_eff * PLANE_MINEFF) / 100;
921     player->dolcost += cost;
922     cash -= cost;
923     snxtitem_all(&nstr, EF_PLANE);
924     freeplane = 0;
925     while (nxtitem(&nstr, (s_char *)&plane)) {
926         if (plane.pln_own == 0) {
927             freeplane++;
928             break;
929         }
930     }
931     if (freeplane == 0) {
932         ef_extend(EF_PLANE, 50);
933     }
934     memset(&plane, 0, sizeof(struct plnstr));
935     plane.pln_x = sp->sct_x;
936     plane.pln_y = sp->sct_y;
937     plane.pln_own = sp->sct_own;
938     plane.pln_type = pp - plchr;
939     plane.pln_effic = PLANE_MINEFF;
940     if (opt_MOB_ACCESS) {
941         time(&plane.pln_access);
942         plane.pln_mobil = -(etu_per_update / sect_mob_neg_factor);
943     } else {
944         plane.pln_mobil = 0;
945     }
946     plane.pln_mission = 0;
947     plane.pln_opx = 0;
948     plane.pln_opy = 0;
949     plane.pln_radius = 0;
950
951     /* Note that this next block of variables can be changed so that individual
952      * planes may have their own stats (like based on tech maybe? :) )  Thus,
953      * the code now checks the pln_acc, pln_load and pln_fuel instead of using
954      * the static definitions of them. */
955 /*
956   n = (int) (pp->pl_range * (0.75 + techfact(tlev - pp->pl_tech, 2.0)));
957   if (n > 127)
958   n = 127;
959   plane.pln_range = n;
960   plane.pln_range_max = n;
961   n = (int) (pp->pl_att * (0.75 + techfact(tlev - pp->pl_tech, 2.0)));
962   if (n > 127)
963   n = 127;
964   plane.pln_att = n;
965   n = (int) (pp->pl_def * (0.75 + techfact(tlev - pp->pl_tech, 2.0)));
966   if (n > 127)
967   n = 127;
968   if (n < pp->pl_def)
969   n = pp->pl_def;
970   plane.pln_def = n;
971   plane.pln_acc = pp->pl_acc;
972   plane.pln_load = pp->pl_load;
973   plane.pln_fuel = pp->pl_fuel;
974 */
975     plane.pln_att = PLN_ATTDEF(pp->pl_att, (int)(tlev - pp->pl_tech));
976     plane.pln_def = PLN_ATTDEF(pp->pl_def, (int)(tlev - pp->pl_tech));
977     plane.pln_acc = PLN_ACC(pp->pl_acc, (int)(tlev - pp->pl_tech));
978     plane.pln_range = PLN_RAN(pp->pl_range, (int)(tlev - pp->pl_tech));
979     plane.pln_range_max = plane.pln_range;
980     plane.pln_load = PLN_LOAD(pp->pl_load, (int)(tlev - pp->pl_tech));
981     plane.pln_fuel = pp->pl_fuel;
982
983     plane.pln_wing = ' ';
984     plane.pln_tech = tlev;
985     plane.pln_ship = -1;
986     plane.pln_land = -1;
987     plane.pln_uid = nstr.cur;
988     plane.pln_nuketype = -1;
989     plane.pln_harden = 0;
990     plane.pln_sell = 0;
991     plane.pln_flags = 0;
992     makenotlost(EF_PLANE, plane.pln_own, plane.pln_uid, plane.pln_x,
993                 plane.pln_y);
994     putplane(plane.pln_uid, &plane);
995     pr("%s built in sector %s\n", prplane(&plane),
996        xyas(sp->sct_x, sp->sct_y, player->cnum));
997     vec[I_LCM] -= lcm;
998     vec[I_HCM] -= hcm;
999     vec[I_MILIT] -= mil;
1000     return 1;
1001 }
1002
1003 static int
1004 build_tower(register struct sctstr *sp, register int *vec)
1005 {
1006     extern int buil_tower_bh;
1007     extern double buil_tower_bc;
1008     struct sctstr sect;
1009     int val;
1010     int newx, newy;
1011     int w_p_eff;
1012     int points;
1013     s_char *p;
1014     s_char buf[1024];
1015     int good;
1016     int i;
1017     int nx;
1018     int ny;
1019
1020     if (sp->sct_type != SCT_BSPAN) {
1021         pr("Bridge towers can only be built from bridge spans.\n");
1022         return 0;
1023     }
1024
1025     if (sp->sct_effic < 60 && !player->god) {
1026         pr("Sector %s is not 60%% efficient.\n",
1027            xyas(sp->sct_x, sp->sct_y, player->cnum));
1028         return 0;
1029     }
1030
1031     if (!opt_NO_HCMS) {
1032         if (vec[I_HCM] < buil_tower_bh) {
1033             pr("%s only has %d unit%s of hcm,\n",
1034                xyas(sp->sct_x, sp->sct_y, player->cnum),
1035                vec[I_HCM], vec[I_HCM] > 1 ? "s" : "");
1036             pr("(a bridge tower requires %d)\n", buil_tower_bh);
1037             return 0;
1038         }
1039     } else if (!opt_NO_LCMS) {
1040         if (vec[I_LCM] < buil_tower_bh) {
1041             pr("%s only has %d unit%s of lcm,\n",
1042                xyas(sp->sct_x, sp->sct_y, player->cnum),
1043                vec[I_LCM], vec[I_LCM] > 1 ? "s" : "");
1044             pr("(a bridge tower requires %d)\n", buil_tower_bh);
1045             return 0;
1046         }
1047     }
1048     if (cash < buil_tower_bc) {
1049         pr("A bridge tower costs $%.2f to build; ", buil_tower_bc);
1050         pr("you only have %d.\n", cash);
1051         return 0;
1052     }
1053     w_p_eff = buil_tower_bh * 2;
1054     points = sp->sct_avail * 100 / w_p_eff;
1055     if (points < 20) {
1056         pr("Not enough available work in %s to build a bridge tower\n",
1057            xyas(sp->sct_x, sp->sct_y, player->cnum));
1058         pr(" (%d available work required)\n", 1 + (w_p_eff * 20) / 100);
1059         return 0;
1060     }
1061     if (!player->argp[3]) {
1062         pr("Building from %s\n", xyas(sp->sct_x, sp->sct_y, player->cnum));
1063         nav_map(sp->sct_x, sp->sct_y, 1);
1064     }
1065     if (!(p = getstarg(player->argp[3], "build tower in what direction? ", buf))
1066         || !*p) {
1067         return 0;
1068     }
1069     /* Sanity check time */
1070     if (!check_sect_ok(sp))
1071         return 0;
1072
1073     if ((val = chkdir(*p, DIR_FIRST, DIR_LAST)) < 0) {
1074         pr("'%c' is not a valid direction...\n", *p);
1075         direrr(0, 0, 0);
1076         return 0;
1077     }
1078     newx = sp->sct_x + diroff[val][0];
1079     newy = sp->sct_y + diroff[val][1];
1080     if (getsect(newx, newy, &sect) == 0 || sect.sct_type != SCT_WATER) {
1081         pr("%s is not a water sector\n", xyas(newx, newy, player->cnum));
1082         return 0;
1083     }
1084
1085     /* Now, check.  You aren't allowed to build bridge towers
1086        next to land. */
1087     good = 0;
1088     for (i = 1; i <= 6; i++) {
1089         struct sctstr s2;
1090         nx = sect.sct_x + diroff[i][0];
1091         ny = sect.sct_y + diroff[i][1];
1092         getsect(nx, ny, &s2);
1093         if ((s2.sct_type != SCT_WATER) &&
1094             (s2.sct_type != SCT_BTOWER) && (s2.sct_type != SCT_BSPAN)) {
1095             good = 1;
1096             break;
1097         }
1098     }
1099     if (good) {
1100         pr("Bridge towers cannot be built adjacent to land.\n");
1101         pr("That sector is adjacent to land.\n");
1102         return 0;
1103     }
1104
1105     sp->sct_avail = (sp->sct_avail * 100 - w_p_eff * 20) / 100;
1106     player->dolcost += buil_tower_bc;
1107     cash -= buil_tower_bc;
1108     sect.sct_type = SCT_BTOWER;
1109     sect.sct_newtype = SCT_BTOWER;
1110     sect.sct_effic = 20;
1111     sect.sct_road = 0;
1112     sect.sct_rail = 0;
1113     sect.sct_defense = 0;
1114     if (opt_MOB_ACCESS) {
1115         time(&sect.sct_access);
1116         sect.sct_mobil = -(etu_per_update / sect_mob_neg_factor);
1117     } else {
1118         sect.sct_mobil = 0;
1119     }
1120     if (!opt_DEFENSE_INFRA)
1121         sect.sct_defense = sect.sct_effic;
1122     putvar(V_MINE, 0, (s_char *)&sect, EF_SECTOR);
1123     putsect(&sect);
1124     pr("Bridge tower built in %s\n",
1125        xyas(sect.sct_x, sect.sct_y, player->cnum));
1126     if (!opt_NO_HCMS)
1127         vec[I_HCM] -= buil_tower_bh;
1128     else if (!opt_NO_LCMS)
1129         vec[I_LCM] -= buil_tower_bh;
1130     return 1;
1131 }