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