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