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