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