]> git.pond.sub.org Git - empserver/blob - src/lib/commands/buil.c
(buil, vers, opt_NONUKES, show_nuke_build, show_nuke_capab):
[empserver] / src / lib / commands / buil.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2005, 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 <limits.h>
35 #include <string.h>
36 #include "misc.h"
37 #include "player.h"
38 #include "plague.h"
39 #include "sect.h"
40 #include "nat.h"
41 #include "ship.h"
42 #include "land.h"
43 #include "nuke.h"
44 #include "plane.h"
45 #include "xy.h"
46 #include "nsc.h"
47 #include "treaty.h"
48 #include "file.h"
49 #include "path.h"
50 #include "optlist.h"
51 #include "commands.h"
52
53 static int build_nuke(struct sctstr *sp,
54                       struct nchrstr *np, short *vec);
55 static int build_ship(struct sctstr *sp,
56                       struct mchrstr *mp, short *vec,
57                       int tlev);
58 static int build_land(struct sctstr *sp,
59                       struct lchrstr *lp, short *vec,
60                       int tlev);
61 static int build_bridge(struct sctstr *sp, short *vec);
62 static int build_tower(struct sctstr *sp, short *vec);
63 static int build_plane(struct sctstr *sp,
64                        struct plchrstr *pp, short *vec,
65                        int tlev);
66
67 static int cash;                /* static ok */
68
69 /*
70  * build <WHAT> <SECTS> <TYPE|DIR|MEG> [NUMBER]
71  */
72 int
73 buil(void)
74 {
75     struct sctstr sect;
76     struct nstr_sect nstr;
77     struct natstr *natp;
78     int rqtech;
79     int tlev;
80     int rlev;
81     int type;
82     int what;
83     struct lchrstr *lp;
84     struct mchrstr *mp;
85     struct plchrstr *pp;
86     struct nchrstr *np;
87     s_char *p;
88     int gotsect = 0;
89     int built;
90     int number = 1, x;
91     int asked = 0;
92     s_char buf[1024];
93
94     natp = getnatp(player->cnum);
95     if ((p =
96          getstarg(player->argp[1],
97                   "Build (ship, nuke, bridge, plane, land unit, tower)? ",
98                   buf)) == 0)
99         return RET_SYN;
100     what = *p;
101
102     for (x = 0; x < number; x++) {
103         if (!snxtsct(&nstr, player->argp[2])) {
104             pr("Bad sector specification.\n");
105             return RET_SYN;
106         }
107       ask_again:
108         tlev = (int)natp->nat_level[NAT_TLEV];
109         rlev = (int)natp->nat_level[NAT_RLEV];
110
111         switch (what) {
112         case 'p':
113             p = getstarg(player->argp[3], "Plane type? ", buf);
114             if (p == 0 || *p == 0)
115                 return RET_SYN;
116             type = typematch(p, EF_PLANE);
117             if (type >= 0) {
118                 pp = &plchr[type];
119                 rqtech = pp->pl_tech;
120                 if (rqtech > tlev)
121                     type = -1;
122             }
123             if (type < 0) {
124                 pr("Illegal plane type: \"%s\"\n", p);
125                 if (confirm("List plane types? "))
126                     show_plane_build(tlev);
127                 player->argp[3] = 0;
128                 goto ask_again;
129             }
130             break;
131         case 's':
132             p = getstarg(player->argp[3], "Ship type? ", buf);
133             if (p == 0 || *p == 0)
134                 return RET_SYN;
135             type = typematch(p, EF_SHIP);
136             if (type >= 0) {
137                 mp = &mchr[type];
138                 rqtech = mp->m_tech;
139                 if (rqtech > tlev)
140                     type = -1;
141                 if ((mp->m_flags & M_TRADE) && !opt_TRADESHIPS)
142                     type = -1;
143             }
144             if (type < 0) {
145                 pr("Illegal ship type: \"%s\"\n", p);
146                 if (confirm("List ship types? "))
147                     show_ship_build(tlev);
148                 player->argp[3] = 0;
149                 goto ask_again;
150             }
151             break;
152         case 'l':
153             p = getstarg(player->argp[3], "Land unit type? ", buf);
154             if (p == 0 || *p == 0)
155                 return RET_SYN;
156             type = typematch(p, EF_LAND);
157             if (type >= 0) {
158                 lp = &lchr[type];
159                 rqtech = lp->l_tech;
160                 if (rqtech > tlev)
161                     type = -1;
162                 if ((lp->l_flags & L_SPY) && !opt_LANDSPIES)
163                     type = -1;
164             }
165             if (type < 0) {
166                 pr("Illegal land unit type: \"%s\"\n", p);
167                 if (confirm("List unit types? "))
168                     show_land_build(tlev);
169                 player->argp[3] = 0;
170                 goto ask_again;
171             }
172             break;
173         case 'b':
174             if (natp->nat_level[NAT_TLEV] + 0.005 < buil_bt) {
175                 pr("Building a span requires a tech of %.0f\n", buil_bt);
176                 return 2;
177             }
178             break;
179         case 't':
180             if (!opt_BRIDGETOWERS) {
181                 pr("Bridge tower building is disabled.\n");
182                 return RET_FAIL;
183             }
184             if (natp->nat_level[NAT_TLEV] + 0.005 < buil_tower_bt) {
185                 pr("Building a tower requires a tech of %.0f\n",
186                    buil_tower_bt);
187                 return 2;
188             }
189             break;
190         case 'n':
191             if (!ef_nelem(EF_NUKE_CHR)) {
192                 pr("There are no nukes in this game.\n");
193                 return RET_FAIL;
194             }
195             p = getstarg(player->argp[3], "Nuke type? ", buf);
196             if (p == 0 || *p == 0)
197                 return RET_SYN;
198             type = typematch(p, EF_NUKE);
199             if (type >= 0) {
200                 np = &nchr[type];
201                 rqtech = np->n_tech;
202                 if (rqtech > tlev
203                     || (opt_DRNUKE && np->n_tech * drnuke_const > rlev))
204                     type = -1;
205                 if ((np->n_flags & N_NEUT) && !opt_NEUTRON)
206                     type = -1;
207             }
208             if (type < 0) {
209                 int tt = tlev;
210                 pr("Possible nuke types are:\n");
211                 if (opt_DRNUKE)
212                     tt = (tlev < (rlev / drnuke_const) ? (int)tlev :
213                           (int)(rlev / drnuke_const));
214
215                 show_nuke_build(tt);
216                 player->argp[3] = 0;
217                 goto ask_again;
218             }
219             break;
220         default:
221             pr("You can't build that!\n");
222             return RET_FAIL;
223         }
224         if (what != 'b' && what != 't') {
225             if (player->argp[4]) {
226                 if (atoi(player->argp[4]) > 20 && !asked) {
227                     s_char bstr[80];
228                     asked = 1;
229                     (void)sprintf(bstr,
230                                   "Are you sure that you want to build %s of them? ",
231                                   player->argp[4]);
232                     p = getstarg(player->argp[6], bstr, buf);
233                     if (p == 0 || *p != 'y')
234                         return RET_SYN;
235                 }
236                 number = atoi(player->argp[4]);
237             }
238         }
239         if (what != 'b' && what != 'n' && what != 't') {
240             if (player->argp[5]) {
241                 tlev = atoi(player->argp[5]);
242                 if (tlev > natp->nat_level[NAT_TLEV] && !player->god) {
243                     pr("Your tech level is only %d.\n",
244                        (int)natp->nat_level[NAT_TLEV]);
245                     return RET_FAIL;
246                 }
247                 if (rqtech > tlev) {
248                     pr("Required tech is %d.\n", rqtech);
249                     return RET_FAIL;
250                 }
251                 pr("building with tech level %d.\n", tlev);
252             } else
253                 tlev = (int)natp->nat_level[NAT_TLEV];
254         }
255         cash = natp->nat_money;
256         while (nxtsct(&nstr, &sect)) {
257             gotsect++;
258             if (!player->owner)
259                 continue;
260             switch (what) {
261             case 'l':
262                 built = build_land(&sect, lp, sect.sct_item, tlev);
263                 break;
264             case 's':
265                 built = build_ship(&sect, mp, sect.sct_item, tlev);
266                 break;
267             case 'b':
268                 built = build_bridge(&sect, sect.sct_item);
269                 break;
270             case 't':
271                 built = build_tower(&sect, sect.sct_item);
272                 break;
273             case 'n':
274                 built = build_nuke(&sect, np, sect.sct_item);
275                 break;
276             case 'p':
277                 built = build_plane(&sect, pp, sect.sct_item, tlev);
278                 break;
279             default:
280                 CANT_HAPPEN("Bad WHAT");
281                 return RET_FAIL;
282             }
283             if (built) {
284                 putsect(&sect);
285             }
286         }
287     }
288     if (!gotsect) {
289         pr("Bad sector specification.\n");
290     }
291     return RET_OK;
292 }
293
294 static int
295 build_ship(struct sctstr *sp, struct mchrstr *mp,
296            short *vec, int tlev)
297 {
298     struct shpstr ship;
299     struct nstr_item nstr;
300     int avail, cost, i;
301     float eff = SHIP_MINEFF / 100.0;
302     int lcm, hcm;
303     int freeship = 0;
304
305     hcm = roundavg((double)mp->m_hcm * eff);
306     lcm = roundavg((double)mp->m_lcm * eff);
307
308     if (sp->sct_type != SCT_HARBR) {
309         pr("Ships must be built in harbours.\n");
310         return 0;
311     }
312     if (sp->sct_effic < 60 && !player->god) {
313         pr("Sector %s is not 60%% efficient.\n",
314            xyas(sp->sct_x, sp->sct_y, player->cnum));
315         return 0;
316     }
317     if (vec[I_LCM] < lcm || vec[I_HCM] < hcm) {
318         pr("Not enough materials in %s\n",
319            xyas(sp->sct_x, sp->sct_y, player->cnum));
320         return 0;
321     }
322     avail = (SHP_BLD_WORK(mp->m_lcm, mp->m_hcm) * SHIP_MINEFF + 99) / 100;
323     if (sp->sct_avail < avail) {
324         pr("Not enough available work in %s to build a %s\n",
325            xyas(sp->sct_x, sp->sct_y, player->cnum), mp->m_name);
326         pr(" (%d available work required)\n", avail);
327         return 0;
328     }
329     cost = mp->m_cost * SHIP_MINEFF / 100;
330     if (cash < cost) {
331         pr("Not enough money left to build a %s\n", mp->m_name);
332         return 0;
333     }
334     if (!trechk(player->cnum, 0, NEWSHP))
335         return 0;
336     if (!check_sect_ok(sp))
337         return 0;
338     sp->sct_avail -= avail;
339     player->dolcost += cost;
340     cash -= cost;
341     snxtitem_all(&nstr, EF_SHIP);
342     while (nxtitem(&nstr, &ship)) {
343         if (ship.shp_own == 0) {
344             freeship++;
345             break;
346         }
347     }
348     if (freeship == 0) {
349         ef_extend(EF_SHIP, 50);
350     }
351     memset(&ship, 0, sizeof(struct shpstr));
352     ship.shp_x = sp->sct_x;
353     ship.shp_y = sp->sct_y;
354     ship.shp_destx[0] = sp->sct_x;
355     ship.shp_desty[0] = sp->sct_y;
356     ship.shp_destx[1] = sp->sct_x;
357     ship.shp_desty[1] = sp->sct_y;
358     ship.shp_autonav = 0;
359     /* new code for autonav, Chad Zabel 1-15-94 */
360     for (i = 0; i < TMAX; ++i) {
361         ship.shp_tstart[i] = I_NONE;
362         ship.shp_tend[i] = I_NONE;
363         ship.shp_lstart[i] = 0;
364         ship.shp_lend[i] = 0;
365     }
366     ship.shp_mission = 0;
367     ship.shp_own = player->cnum;
368     ship.shp_type = mp - mchr;
369     ship.shp_effic = SHIP_MINEFF;
370     if (opt_MOB_ACCESS) {
371         time(&ship.shp_access);
372         ship.shp_mobil = -(etu_per_update / sect_mob_neg_factor);
373     } else {
374         ship.shp_mobil = 0;
375     }
376     ship.shp_uid = nstr.cur;
377     ship.shp_nplane = 0;
378     ship.shp_nland = 0;
379     ship.shp_nxlight = 0;
380     ship.shp_nchoppers = 0;
381     ship.shp_fleet = ' ';
382     memset(ship.shp_item, 0, sizeof(ship.shp_item));
383     ship.shp_pstage = PLG_HEALTHY;
384     ship.shp_ptime = 0;
385     ship.shp_mobquota = 0;
386     *ship.shp_path = 0;
387     ship.shp_follow = nstr.cur;
388     ship.shp_name[0] = 0;
389     ship.shp_orig_own = player->cnum;
390     ship.shp_orig_x = sp->sct_x;
391     ship.shp_orig_y = sp->sct_y;
392     ship.shp_fuel = mchr[(int)ship.shp_type].m_fuelc;
393     ship.shp_rflags = 0;
394     memset(ship.shp_rpath, 0, sizeof(ship.shp_rpath));
395     shp_set_tech(&ship, tlev);
396
397     vec[I_LCM] -= lcm;
398     vec[I_HCM] -= hcm;
399
400     if (sp->sct_pstage == PLG_INFECT)
401         ship.shp_pstage = PLG_EXPOSED;
402     makenotlost(EF_SHIP, ship.shp_own, ship.shp_uid, ship.shp_x,
403                 ship.shp_y);
404     putship(ship.shp_uid, &ship);
405     pr("%s", prship(&ship));
406     pr(" built in sector %s\n", xyas(sp->sct_x, sp->sct_y, player->cnum));
407     return 1;
408 }
409
410 static int
411 build_land(struct sctstr *sp, struct lchrstr *lp,
412            short *vec, int tlev)
413 {
414     struct lndstr land;
415     struct nstr_item nstr;
416     int avail, cost;
417     float eff = LAND_MINEFF / 100.0;
418     int mil, lcm, hcm, gun, shell;
419     int freeland = 0;
420
421 #if 0
422     mil = roundavg(((double)lp->l_mil * eff));
423     shell = roundavg(((double)lp->l_shell * eff));
424     gun = roundavg(((double)lp->l_gun * eff));
425 #else
426     mil = shell = gun = 0;
427 #endif
428     hcm = roundavg(((double)lp->l_hcm * eff));
429     lcm = roundavg(((double)lp->l_lcm * eff));
430
431     if (sp->sct_type != SCT_HEADQ) {
432         pr("Land Units must be built in headquarters.\n");
433         return 0;
434     }
435     if (sp->sct_effic < 60 && !player->god) {
436         pr("Sector %s is not 60%% efficient.\n",
437            xyas(sp->sct_x, sp->sct_y, player->cnum));
438         return 0;
439     }
440     if (vec[I_LCM] < lcm || vec[I_HCM] < hcm) {
441         pr("Not enough materials in %s\n",
442            xyas(sp->sct_x, sp->sct_y, player->cnum));
443         return 0;
444     }
445 #if 0
446     if (vec[I_GUN] < gun || vec[I_GUN] == 0) {
447         pr("Not enough guns in %s\n",
448            xyas(sp->sct_x, sp->sct_y, player->cnum));
449         return 0;
450     }
451     if (vec[I_SHELL] < shell) {
452         pr("Not enough shells in %s\n",
453            xyas(sp->sct_x, sp->sct_y, player->cnum));
454         return 0;
455     }
456     if (vec[I_MILIT] < mil) {
457         pr("Not enough military in %s\n",
458            xyas(sp->sct_x, sp->sct_y, player->cnum));
459         return 0;
460     }
461 #endif
462     if (!trechk(player->cnum, 0, NEWLND))
463         return 0;
464     if (!check_sect_ok(sp))
465         return 0;
466     avail = (LND_BLD_WORK(lp->l_lcm, lp->l_hcm) * LAND_MINEFF + 99) / 100;
467     if (sp->sct_avail < avail) {
468         pr("Not enough available work in %s to build a %s\n",
469            xyas(sp->sct_x, sp->sct_y, player->cnum), lp->l_name);
470         pr(" (%d available work required)\n", avail);
471         return 0;
472     }
473     cost = lp->l_cost * LAND_MINEFF / 100;
474     if (cash < cost) {
475         pr("Not enough money left to build a %s\n", lp->l_name);
476         return 0;
477     }
478     sp->sct_avail -= avail;
479     player->dolcost += cost;
480     cash -= cost;
481     snxtitem_all(&nstr, EF_LAND);
482     while (nxtitem(&nstr, &land)) {
483         if (land.lnd_own == 0) {
484             freeland++;
485             break;
486         }
487     }
488     if (freeland == 0) {
489         ef_extend(EF_LAND, 50);
490     }
491     memset(&land, 0, sizeof(struct lndstr));
492     land.lnd_x = sp->sct_x;
493     land.lnd_y = sp->sct_y;
494     land.lnd_own = player->cnum;
495     land.lnd_mission = 0;
496     land.lnd_type = lp - lchr;
497     land.lnd_effic = LAND_MINEFF;
498     if (opt_MOB_ACCESS) {
499         time(&land.lnd_access);
500         land.lnd_mobil = -(etu_per_update / sect_mob_neg_factor);
501     } else {
502         land.lnd_mobil = 0;
503     }
504     land.lnd_uid = nstr.cur;
505     land.lnd_army = ' ';
506     land.lnd_flags = 0;
507     land.lnd_ship = -1;
508     land.lnd_land = -1;
509     land.lnd_nland = 0;
510     land.lnd_harden = 0;
511     land.lnd_retreat = morale_base;
512     land.lnd_fuel = lp->l_fuelc;
513     land.lnd_nxlight = 0;
514     land.lnd_rflags = 0;
515     memset(land.lnd_rpath, 0, sizeof(land.lnd_rpath));
516     land.lnd_rad_max = 0;
517     memset(land.lnd_item, 0, sizeof(land.lnd_item));
518     land.lnd_pstage = PLG_HEALTHY;
519     land.lnd_ptime = 0;
520     lnd_set_tech(&land, tlev);
521
522     vec[I_LCM] -= lcm;
523     vec[I_HCM] -= hcm;
524     vec[I_MILIT] -= mil;
525     vec[I_GUN] -= gun;
526     vec[I_SHELL] -= shell;
527
528     if (sp->sct_pstage == PLG_INFECT)
529         land.lnd_pstage = PLG_EXPOSED;
530     putland(nstr.cur, &land);
531     makenotlost(EF_LAND, land.lnd_own, land.lnd_uid, land.lnd_x,
532                 land.lnd_y);
533     pr("%s", prland(&land));
534     pr(" built in sector %s\n", xyas(sp->sct_x, sp->sct_y, player->cnum));
535     return 1;
536 }
537
538 static int
539 build_bridge(struct sctstr *sp, short *vec)
540 {
541     struct sctstr sect;
542     int val;
543     int newx, newy;
544     int avail;
545     int nx, ny, i, good = 0;
546     s_char *p;
547     s_char buf[1024];
548
549     if (opt_EASY_BRIDGES == 0) {        /* must have a bridge head or tower */
550         if (sp->sct_type != SCT_BTOWER) {
551             if (sp->sct_type != SCT_BHEAD)
552                 return 0;
553             if (sp->sct_newtype != SCT_BHEAD)
554                 return 0;
555         }
556     }
557
558     if (sp->sct_effic < 60 && !player->god) {
559         pr("Sector %s is not 60%% efficient.\n",
560            xyas(sp->sct_x, sp->sct_y, player->cnum));
561         return 0;
562     }
563
564     if (!opt_NO_HCMS) {
565         if (vec[I_HCM] < buil_bh) {
566             pr("%s only has %d unit%s of hcm,\n",
567                xyas(sp->sct_x, sp->sct_y, player->cnum),
568                vec[I_HCM], vec[I_HCM] > 1 ? "s" : "");
569             pr("(a bridge span requires %d)\n", buil_bh);
570             return 0;
571         }
572     } else if (!opt_NO_LCMS) {
573         if (vec[I_LCM] < buil_bh) {
574             pr("%s only has %d unit%s of lcm,\n",
575                xyas(sp->sct_x, sp->sct_y, player->cnum),
576                vec[I_LCM], vec[I_LCM] > 1 ? "s" : "");
577             pr("(a bridge span requires %d)\n", buil_bh);
578             return 0;
579         }
580     }
581     if (cash < buil_bc) {
582         pr("A span costs $%.2f to build; ", buil_bc);
583         pr("you only have %d.\n", cash);
584         return 0;
585     }
586     avail = (SCT_BLD_WORK(0, buil_bh) * SCT_MINEFF + 99) / 100;
587     if (sp->sct_avail < avail) {
588         pr("Not enough available work in %s to build a bridge\n",
589            xyas(sp->sct_x, sp->sct_y, player->cnum));
590         pr(" (%d available work required)\n", avail);
591         return 0;
592     }
593     if (!player->argp[3]) {
594         pr("Bridge head at %s\n",
595            xyas(sp->sct_x, sp->sct_y, player->cnum));
596         nav_map(sp->sct_x, sp->sct_y, 1);
597     }
598     if (!(p = getstarg(player->argp[3], "build span in what direction? ", buf))
599         || !*p) {
600         return 0;
601     }
602     /* Sanity check time */
603     if (!check_sect_ok(sp))
604         return 0;
605
606     if ((val = chkdir(*p, DIR_FIRST, DIR_LAST)) < 0) {
607         pr("'%c' is not a valid direction...\n", *p);
608         direrr(0, 0, 0);
609         return 0;
610     }
611     newx = sp->sct_x + diroff[val][0];
612     newy = sp->sct_y + diroff[val][1];
613     if (getsect(newx, newy, &sect) == 0 || sect.sct_type != SCT_WATER) {
614         pr("%s is not a water sector\n", xyas(newx, newy, player->cnum));
615         return 0;
616     }
617     if (opt_EASY_BRIDGES) {
618         good = 0;
619
620         for (i = 1; i <= 6; i++) {
621             struct sctstr s2;
622             nx = sect.sct_x + diroff[i][0];
623             ny = sect.sct_y + diroff[i][1];
624             getsect(nx, ny, &s2);
625             if ((s2.sct_type != SCT_WATER) && (s2.sct_type != SCT_BSPAN))
626                 good = 1;
627         }
628         if (!good) {
629             pr("Bridges must be built adjacent to land or bridge towers.\n");
630             pr("That sector is not adjacent to land or a bridge tower.\n");
631             return 0;
632         }
633     }                           /* end EASY_BRIDGES */
634     sp->sct_avail -= avail;
635     player->dolcost += buil_bc;
636     cash -= buil_bc;
637     sect.sct_type = SCT_BSPAN;
638     sect.sct_newtype = SCT_BSPAN;
639     sect.sct_effic = SCT_MINEFF;
640     sect.sct_road = 0;
641     sect.sct_rail = 0;
642     sect.sct_defense = 0;
643     if (!opt_DEFENSE_INFRA)
644         sect.sct_defense = sect.sct_effic;
645     if (opt_MOB_ACCESS) {
646         time(&sect.sct_access);
647         sect.sct_mobil = -(etu_per_update / sect_mob_neg_factor);
648     } else {
649         sect.sct_mobil = 0;
650     }
651     sect.sct_mines = 0;
652     map_set(player->cnum, sect.sct_x, sect.sct_y, dchr[SCT_BSPAN].d_mnem, 2);
653     writemap(player->cnum);
654     putsect(&sect);
655     pr("Bridge span built over %s\n",
656        xyas(sect.sct_x, sect.sct_y, player->cnum));
657     if (!opt_NO_HCMS)
658         vec[I_HCM] -= buil_bh;
659     else if (!opt_NO_LCMS)
660         vec[I_LCM] -= buil_bh;
661     return 1;
662 }
663
664 static int
665 build_nuke(struct sctstr *sp, struct nchrstr *np,
666            short *vec)
667 {
668     int avail;
669
670     if (sp->sct_type != SCT_NUKE && !player->god) {
671         pr("Nuclear weapons must be built in nuclear plants.\n");
672         return 0;
673     }
674     if (sp->sct_effic < 60 && !player->god) {
675         pr("Sector %s is not 60%% efficient.\n",
676            xyas(sp->sct_x, sp->sct_y, player->cnum));
677         return 0;
678     }
679     if (vec[I_HCM] < np->n_hcm || vec[I_LCM] < np->n_lcm ||
680         vec[I_OIL] < np->n_oil || vec[I_RAD] < np->n_rad) {
681         pr("Not enough materials for a %s bomb in %s\n",
682            np->n_name, xyas(sp->sct_x, sp->sct_y, player->cnum));
683         pr("(%d hcm, %d lcm, %d oil, & %d rads).\n",
684            np->n_hcm, np->n_lcm, np->n_oil, np->n_rad);
685         return 0;
686     }
687     if (cash < np->n_cost) {
688         pr("You need $%d, you only have %d.\n", np->n_cost, cash);
689         return 0;
690     }
691     avail = NUK_BLD_WORK(np->n_lcm, np->n_hcm, np->n_oil, np->n_rad);
692     /*
693      * XXX when nukes turn into units (or whatever), then
694      * make them start at 20%.  Since they don't have efficiency
695      * now, we charge all the work right away.
696      */
697     if (sp->sct_avail < avail) {
698         pr("Not enough available work in %s to build a %s;\n",
699            xyas(sp->sct_x, sp->sct_y, player->cnum), np->n_name);
700         pr(" (%d available work required)\n", avail);
701         return 0;
702     }
703     if (!trechk(player->cnum, 0, NEWNUK))
704         return 0;
705     if (!check_sect_ok(sp))
706         return 0;
707     sp->sct_avail -= avail;
708     player->dolcost += np->n_cost;
709     cash -= np->n_cost;
710     nuk_add(sp->sct_x, sp->sct_y, np - nchr, 1);
711     vec[I_HCM] -= np->n_hcm;
712     vec[I_LCM] -= np->n_lcm;
713     vec[I_OIL] -= np->n_oil;
714     vec[I_RAD] -= np->n_rad;
715     pr("%s warhead created in %s\n", np->n_name,
716        xyas(sp->sct_x, sp->sct_y, player->cnum));
717     return 1;
718 }
719
720 static int
721 build_plane(struct sctstr *sp, struct plchrstr *pp,
722             short *vec, int tlev)
723 {
724     struct plnstr plane;
725     int avail, cost;
726     struct nstr_item nstr;
727     float eff = PLANE_MINEFF / 100.0;
728     int hcm, lcm, mil;
729     int freeplane = 0;
730
731     mil = roundavg(((double)pp->pl_crew * eff));
732     /* Always use at least 1 mil to build a plane */
733     if (mil == 0 && pp->pl_crew > 0)
734         mil = 1;
735     hcm = roundavg(((double)pp->pl_hcm * eff));
736     lcm = roundavg(((double)pp->pl_lcm * eff));
737     if (sp->sct_type != SCT_AIRPT && !player->god) {
738         pr("Planes must be built in airports.\n");
739         return 0;
740     }
741     if (sp->sct_effic < 60 && !player->god) {
742         pr("Sector %s is not 60%% efficient.\n",
743            xyas(sp->sct_x, sp->sct_y, player->cnum));
744         return 0;
745     }
746     if (vec[I_LCM] < lcm || vec[I_HCM] < hcm) {
747         pr("Not enough materials in %s\n",
748            xyas(sp->sct_x, sp->sct_y, player->cnum));
749         return 0;
750     }
751     avail = (PLN_BLD_WORK(pp->pl_lcm, pp->pl_hcm) * PLANE_MINEFF + 99) / 100;
752     if (sp->sct_avail < avail) {
753         pr("Not enough available work in %s to build a %s\n",
754            xyas(sp->sct_x, sp->sct_y, player->cnum), pp->pl_name);
755         pr(" (%d available work required)\n", avail);
756         return 0;
757     }
758     cost = pp->pl_cost * PLANE_MINEFF / 100;
759     if (cash < cost) {
760         pr("Not enough money left to build a %s\n", pp->pl_name);
761         return 0;
762     }
763     if (vec[I_MILIT] < mil || (vec[I_MILIT] == 0 && pp->pl_crew > 0)) {
764         pr("Not enough military for crew in %s\n",
765            xyas(sp->sct_x, sp->sct_y, player->cnum));
766         return 0;
767     }
768     if (!trechk(player->cnum, 0, NEWPLN))
769         return 0;
770     if (!check_sect_ok(sp))
771         return 0;
772     sp->sct_avail -= avail;
773     player->dolcost += cost;
774     cash -= cost;
775     snxtitem_all(&nstr, EF_PLANE);
776     freeplane = 0;
777     while (nxtitem(&nstr, &plane)) {
778         if (plane.pln_own == 0) {
779             freeplane++;
780             break;
781         }
782     }
783     if (freeplane == 0) {
784         ef_extend(EF_PLANE, 50);
785     }
786     memset(&plane, 0, sizeof(struct plnstr));
787     plane.pln_x = sp->sct_x;
788     plane.pln_y = sp->sct_y;
789     plane.pln_own = sp->sct_own;
790     plane.pln_type = pp - plchr;
791     plane.pln_effic = PLANE_MINEFF;
792     if (opt_MOB_ACCESS) {
793         time(&plane.pln_access);
794         plane.pln_mobil = -(etu_per_update / sect_mob_neg_factor);
795     } else {
796         plane.pln_mobil = 0;
797     }
798     plane.pln_mission = 0;
799     plane.pln_opx = 0;
800     plane.pln_opy = 0;
801     plane.pln_radius = 0;
802     plane.pln_range = UCHAR_MAX; /* will be adjusted by pln_set_tech() */
803     plane.pln_range_max = plane.pln_range;
804     plane.pln_wing = ' ';
805     plane.pln_ship = -1;
806     plane.pln_land = -1;
807     plane.pln_uid = nstr.cur;
808     plane.pln_nuketype = -1;
809     plane.pln_harden = 0;
810     plane.pln_flags = 0;
811     pln_set_tech(&plane, tlev);
812
813     vec[I_LCM] -= lcm;
814     vec[I_HCM] -= hcm;
815     vec[I_MILIT] -= mil;
816
817     makenotlost(EF_PLANE, plane.pln_own, plane.pln_uid, plane.pln_x,
818                 plane.pln_y);
819     putplane(plane.pln_uid, &plane);
820     pr("%s built in sector %s\n", prplane(&plane),
821        xyas(sp->sct_x, sp->sct_y, player->cnum));
822     return 1;
823 }
824
825 static int
826 build_tower(struct sctstr *sp, short *vec)
827 {
828     struct sctstr sect;
829     int val;
830     int newx, newy;
831     int avail;
832     s_char *p;
833     s_char buf[1024];
834     int good;
835     int i;
836     int nx;
837     int ny;
838
839     if (sp->sct_type != SCT_BSPAN) {
840         pr("Bridge towers can only be built from bridge spans.\n");
841         return 0;
842     }
843
844     if (sp->sct_effic < 60 && !player->god) {
845         pr("Sector %s is not 60%% efficient.\n",
846            xyas(sp->sct_x, sp->sct_y, player->cnum));
847         return 0;
848     }
849
850     if (!opt_NO_HCMS) {
851         if (vec[I_HCM] < buil_tower_bh) {
852             pr("%s only has %d unit%s of hcm,\n",
853                xyas(sp->sct_x, sp->sct_y, player->cnum),
854                vec[I_HCM], vec[I_HCM] > 1 ? "s" : "");
855             pr("(a bridge tower requires %d)\n", buil_tower_bh);
856             return 0;
857         }
858     } else if (!opt_NO_LCMS) {
859         if (vec[I_LCM] < buil_tower_bh) {
860             pr("%s only has %d unit%s of lcm,\n",
861                xyas(sp->sct_x, sp->sct_y, player->cnum),
862                vec[I_LCM], vec[I_LCM] > 1 ? "s" : "");
863             pr("(a bridge tower requires %d)\n", buil_tower_bh);
864             return 0;
865         }
866     }
867     if (cash < buil_tower_bc) {
868         pr("A bridge tower costs $%.2f to build; ", buil_tower_bc);
869         pr("you only have %d.\n", cash);
870         return 0;
871     }
872     avail = (SCT_BLD_WORK(0, buil_tower_bh) * SCT_MINEFF + 99) / 100;
873     if (sp->sct_avail < avail) {
874         pr("Not enough available work in %s to build a bridge tower\n",
875            xyas(sp->sct_x, sp->sct_y, player->cnum));
876         pr(" (%d available work required)\n", avail);
877         return 0;
878     }
879     if (!player->argp[3]) {
880         pr("Building from %s\n", xyas(sp->sct_x, sp->sct_y, player->cnum));
881         nav_map(sp->sct_x, sp->sct_y, 1);
882     }
883     if (!(p = getstarg(player->argp[3], "build tower in what direction? ", buf))
884         || !*p) {
885         return 0;
886     }
887     /* Sanity check time */
888     if (!check_sect_ok(sp))
889         return 0;
890
891     if ((val = chkdir(*p, DIR_FIRST, DIR_LAST)) < 0) {
892         pr("'%c' is not a valid direction...\n", *p);
893         direrr(0, 0, 0);
894         return 0;
895     }
896     newx = sp->sct_x + diroff[val][0];
897     newy = sp->sct_y + diroff[val][1];
898     if (getsect(newx, newy, &sect) == 0 || sect.sct_type != SCT_WATER) {
899         pr("%s is not a water sector\n", xyas(newx, newy, player->cnum));
900         return 0;
901     }
902
903     /* Now, check.  You aren't allowed to build bridge towers
904        next to land. */
905     good = 0;
906     for (i = 1; i <= 6; i++) {
907         struct sctstr s2;
908         nx = sect.sct_x + diroff[i][0];
909         ny = sect.sct_y + diroff[i][1];
910         getsect(nx, ny, &s2);
911         if ((s2.sct_type != SCT_WATER) &&
912             (s2.sct_type != SCT_BTOWER) && (s2.sct_type != SCT_BSPAN)) {
913             good = 1;
914             break;
915         }
916     }
917     if (good) {
918         pr("Bridge towers cannot be built adjacent to land.\n");
919         pr("That sector is adjacent to land.\n");
920         return 0;
921     }
922
923     sp->sct_avail -= avail;
924     player->dolcost += buil_tower_bc;
925     cash -= buil_tower_bc;
926     sect.sct_type = SCT_BTOWER;
927     sect.sct_newtype = SCT_BTOWER;
928     sect.sct_effic = SCT_MINEFF;
929     sect.sct_road = 0;
930     sect.sct_rail = 0;
931     sect.sct_defense = 0;
932     if (opt_MOB_ACCESS) {
933         time(&sect.sct_access);
934         sect.sct_mobil = -(etu_per_update / sect_mob_neg_factor);
935     } else {
936         sect.sct_mobil = 0;
937     }
938     if (!opt_DEFENSE_INFRA)
939         sect.sct_defense = sect.sct_effic;
940     sect.sct_mines = 0;
941     map_set(player->cnum, sect.sct_x, sect.sct_y, dchr[SCT_BTOWER].d_mnem, 2);
942     writemap(player->cnum);
943     putsect(&sect);
944     pr("Bridge tower built in %s\n",
945        xyas(sect.sct_x, sect.sct_y, player->cnum));
946     if (!opt_NO_HCMS)
947         vec[I_HCM] -= buil_tower_bh;
948     else if (!opt_NO_LCMS)
949         vec[I_LCM] -= buil_tower_bh;
950     return 1;
951 }