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