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