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