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