]> git.pond.sub.org Git - empserver/blob - src/lib/commands/buil.c
Remove option TREATIES
[empserver] / src / lib / commands / buil.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2014, 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-2013
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 "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 pick_unused_unit_uid(int);
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     int avail;
211     double cost;
212     double eff = SHIP_MINEFF / 100.0;
213     int lcm, hcm;
214
215     hcm = roundavg(mp->m_hcm * eff);
216     lcm = roundavg(mp->m_lcm * eff);
217
218     if (sp->sct_type != SCT_HARBR) {
219         pr("Ships must be built in harbours.\n");
220         return 0;
221     }
222     if (sp->sct_effic < 60 && !player->god) {
223         pr("Sector %s is not 60%% efficient.\n",
224            xyas(sp->sct_x, sp->sct_y, player->cnum));
225         return 0;
226     }
227     if (vec[I_LCM] < lcm || vec[I_HCM] < hcm) {
228         pr("Not enough materials in %s\n",
229            xyas(sp->sct_x, sp->sct_y, player->cnum));
230         return 0;
231     }
232     avail = (SHP_BLD_WORK(mp->m_lcm, mp->m_hcm) * SHIP_MINEFF + 99) / 100;
233     if (sp->sct_avail < avail) {
234         pr("Not enough available work in %s to build a %s\n",
235            xyas(sp->sct_x, sp->sct_y, player->cnum), mp->m_name);
236         pr(" (%d available work required)\n", avail);
237         return 0;
238     }
239     cost = mp->m_cost * SHIP_MINEFF / 100.0;
240     if (!build_can_afford(cost, mp->m_name))
241         return 0;
242     sp->sct_avail -= avail;
243     player->dolcost += cost;
244     ef_blank(EF_SHIP, pick_unused_unit_uid(EF_SHIP), &ship);
245     ship.shp_x = sp->sct_x;
246     ship.shp_y = sp->sct_y;
247     ship.shp_own = sp->sct_own;
248     ship.shp_type = mp - mchr;
249     ship.shp_effic = SHIP_MINEFF;
250     if (opt_MOB_ACCESS) {
251         game_tick_to_now(&ship.shp_access);
252         ship.shp_mobil = -(etu_per_update / sect_mob_neg_factor);
253     } else {
254         ship.shp_mobil = 0;
255     }
256     memset(ship.shp_item, 0, sizeof(ship.shp_item));
257     ship.shp_pstage = PLG_HEALTHY;
258     ship.shp_ptime = 0;
259     ship.shp_name[0] = 0;
260     ship.shp_orig_own = sp->sct_own;
261     ship.shp_orig_x = sp->sct_x;
262     ship.shp_orig_y = sp->sct_y;
263     shp_set_tech(&ship, tlev);
264     unit_wipe_orders((struct empobj *)&ship);
265
266     vec[I_LCM] -= lcm;
267     vec[I_HCM] -= hcm;
268
269     if (sp->sct_pstage == PLG_INFECT)
270         ship.shp_pstage = PLG_EXPOSED;
271     putship(ship.shp_uid, &ship);
272     pr("%s", prship(&ship));
273     pr(" built in sector %s\n", xyas(sp->sct_x, sp->sct_y, player->cnum));
274     return 1;
275 }
276
277 static int
278 build_land(struct sctstr *sp, int type, int tlev)
279 {
280     short *vec = sp->sct_item;
281     struct lchrstr *lp = &lchr[type];
282     struct lndstr land;
283     int avail;
284     double cost;
285     double eff = LAND_MINEFF / 100.0;
286     int mil, lcm, hcm, gun, shell;
287
288 #if 0
289     mil = roundavg(lp->l_mil * eff);
290     shell = roundavg(lp->l_shell * eff);
291     gun = roundavg(lp->l_gun * eff);
292 #else
293     mil = shell = gun = 0;
294 #endif
295     hcm = roundavg(lp->l_hcm * eff);
296     lcm = roundavg(lp->l_lcm * eff);
297
298     if (sp->sct_type != SCT_HEADQ) {
299         pr("Land units must be built in headquarters.\n");
300         return 0;
301     }
302     if (sp->sct_effic < 60 && !player->god) {
303         pr("Sector %s is not 60%% efficient.\n",
304            xyas(sp->sct_x, sp->sct_y, player->cnum));
305         return 0;
306     }
307     if (vec[I_LCM] < lcm || vec[I_HCM] < hcm) {
308         pr("Not enough materials in %s\n",
309            xyas(sp->sct_x, sp->sct_y, player->cnum));
310         return 0;
311     }
312 #if 0
313     if (vec[I_GUN] < gun || vec[I_GUN] == 0) {
314         pr("Not enough guns in %s\n",
315            xyas(sp->sct_x, sp->sct_y, player->cnum));
316         return 0;
317     }
318     if (vec[I_SHELL] < shell) {
319         pr("Not enough shells in %s\n",
320            xyas(sp->sct_x, sp->sct_y, player->cnum));
321         return 0;
322     }
323     if (vec[I_MILIT] < mil) {
324         pr("Not enough military in %s\n",
325            xyas(sp->sct_x, sp->sct_y, player->cnum));
326         return 0;
327     }
328 #endif
329     avail = (LND_BLD_WORK(lp->l_lcm, lp->l_hcm) * LAND_MINEFF + 99) / 100;
330     if (sp->sct_avail < avail) {
331         pr("Not enough available work in %s to build a %s\n",
332            xyas(sp->sct_x, sp->sct_y, player->cnum), lp->l_name);
333         pr(" (%d available work required)\n", avail);
334         return 0;
335     }
336     cost = lp->l_cost * LAND_MINEFF / 100.0;
337     if (!build_can_afford(cost, lp->l_name))
338         return 0;
339     sp->sct_avail -= avail;
340     player->dolcost += cost;
341     ef_blank(EF_LAND, pick_unused_unit_uid(EF_LAND), &land);
342     land.lnd_x = sp->sct_x;
343     land.lnd_y = sp->sct_y;
344     land.lnd_own = sp->sct_own;
345     land.lnd_type = lp - lchr;
346     land.lnd_effic = LAND_MINEFF;
347     if (opt_MOB_ACCESS) {
348         game_tick_to_now(&land.lnd_access);
349         land.lnd_mobil = -(etu_per_update / sect_mob_neg_factor);
350     } else {
351         land.lnd_mobil = 0;
352     }
353     land.lnd_ship = -1;
354     land.lnd_land = -1;
355     land.lnd_harden = 0;
356     memset(land.lnd_item, 0, sizeof(land.lnd_item));
357     land.lnd_pstage = PLG_HEALTHY;
358     land.lnd_ptime = 0;
359     lnd_set_tech(&land, tlev);
360     unit_wipe_orders((struct empobj *)&land);
361
362     vec[I_LCM] -= lcm;
363     vec[I_HCM] -= hcm;
364     vec[I_MILIT] -= mil;
365     vec[I_GUN] -= gun;
366     vec[I_SHELL] -= shell;
367
368     if (sp->sct_pstage == PLG_INFECT)
369         land.lnd_pstage = PLG_EXPOSED;
370     putland(land.lnd_uid, &land);
371     pr("%s", prland(&land));
372     pr(" built in sector %s\n", xyas(sp->sct_x, sp->sct_y, player->cnum));
373     return 1;
374 }
375
376 static int
377 build_nuke(struct sctstr *sp, int type, int tlev)
378 {
379     short *vec = sp->sct_item;
380     struct nchrstr *np = &nchr[type];
381     struct nukstr nuke;
382     int avail;
383
384     if (sp->sct_type != SCT_NUKE && !player->god) {
385         pr("Nuclear weapons must be built in nuclear plants.\n");
386         return 0;
387     }
388     if (sp->sct_effic < 60 && !player->god) {
389         pr("Sector %s is not 60%% efficient.\n",
390            xyas(sp->sct_x, sp->sct_y, player->cnum));
391         return 0;
392     }
393     if (vec[I_HCM] < np->n_hcm || vec[I_LCM] < np->n_lcm ||
394         vec[I_OIL] < np->n_oil || vec[I_RAD] < np->n_rad) {
395         pr("Not enough materials for a %s bomb in %s\n",
396            np->n_name, xyas(sp->sct_x, sp->sct_y, player->cnum));
397         pr("(%d hcm, %d lcm, %d oil, & %d rads).\n",
398            np->n_hcm, np->n_lcm, np->n_oil, np->n_rad);
399         return 0;
400     }
401     if (!build_can_afford(np->n_cost, np->n_name))
402         return 0;
403     avail = NUK_BLD_WORK(np->n_lcm, np->n_hcm, np->n_oil, np->n_rad);
404     /*
405      * XXX when nukes turn into units (or whatever), then
406      * make them start at 20%.  Since they don't have efficiency
407      * now, we charge all the work right away.
408      */
409     if (sp->sct_avail < avail) {
410         pr("Not enough available work in %s to build a %s;\n",
411            xyas(sp->sct_x, sp->sct_y, player->cnum), np->n_name);
412         pr(" (%d available work required)\n", avail);
413         return 0;
414     }
415     sp->sct_avail -= avail;
416     player->dolcost += np->n_cost;
417     ef_blank(EF_NUKE, pick_unused_unit_uid(EF_NUKE), &nuke);
418     nuke.nuk_x = sp->sct_x;
419     nuke.nuk_y = sp->sct_y;
420     nuke.nuk_own = sp->sct_own;
421     nuke.nuk_type = np - nchr;
422     nuke.nuk_effic = 100;
423     nuke.nuk_plane = -1;
424     nuke.nuk_tech = tlev;
425     unit_wipe_orders((struct empobj *)&nuke);
426
427     vec[I_HCM] -= np->n_hcm;
428     vec[I_LCM] -= np->n_lcm;
429     vec[I_OIL] -= np->n_oil;
430     vec[I_RAD] -= np->n_rad;
431
432     putnuke(nuke.nuk_uid, &nuke);
433     pr("%s created in %s\n", prnuke(&nuke),
434        xyas(sp->sct_x, sp->sct_y, player->cnum));
435     return 1;
436 }
437
438 static int
439 build_plane(struct sctstr *sp, int type, int tlev)
440 {
441     short *vec = sp->sct_item;
442     struct plchrstr *pp = &plchr[type];
443     struct plnstr plane;
444     int avail;
445     double cost;
446     double eff = PLANE_MINEFF / 100.0;
447     int hcm, lcm, mil;
448
449     mil = roundavg(pp->pl_crew * eff);
450     /* Always use at least 1 mil to build a plane */
451     if (mil == 0 && pp->pl_crew > 0)
452         mil = 1;
453     hcm = roundavg(pp->pl_hcm * eff);
454     lcm = roundavg(pp->pl_lcm * eff);
455     if (sp->sct_type != SCT_AIRPT && !player->god) {
456         pr("Planes must be built in airports.\n");
457         return 0;
458     }
459     if (sp->sct_effic < 60 && !player->god) {
460         pr("Sector %s is not 60%% efficient.\n",
461            xyas(sp->sct_x, sp->sct_y, player->cnum));
462         return 0;
463     }
464     if (vec[I_LCM] < lcm || vec[I_HCM] < hcm) {
465         pr("Not enough materials in %s\n",
466            xyas(sp->sct_x, sp->sct_y, player->cnum));
467         return 0;
468     }
469     avail = (PLN_BLD_WORK(pp->pl_lcm, pp->pl_hcm) * PLANE_MINEFF + 99) / 100;
470     if (sp->sct_avail < avail) {
471         pr("Not enough available work in %s to build a %s\n",
472            xyas(sp->sct_x, sp->sct_y, player->cnum), pp->pl_name);
473         pr(" (%d available work required)\n", avail);
474         return 0;
475     }
476     cost = pp->pl_cost * PLANE_MINEFF / 100.0;
477     if (!build_can_afford(cost, pp->pl_name))
478         return 0;
479     if (vec[I_MILIT] < mil || (vec[I_MILIT] == 0 && pp->pl_crew > 0)) {
480         pr("Not enough military for crew in %s\n",
481            xyas(sp->sct_x, sp->sct_y, player->cnum));
482         return 0;
483     }
484     sp->sct_avail -= avail;
485     player->dolcost += cost;
486     ef_blank(EF_PLANE, pick_unused_unit_uid(EF_PLANE), &plane);
487     plane.pln_x = sp->sct_x;
488     plane.pln_y = sp->sct_y;
489     plane.pln_own = sp->sct_own;
490     plane.pln_type = pp - plchr;
491     plane.pln_effic = PLANE_MINEFF;
492     if (opt_MOB_ACCESS) {
493         game_tick_to_now(&plane.pln_access);
494         plane.pln_mobil = -(etu_per_update / sect_mob_neg_factor);
495     } else {
496         plane.pln_mobil = 0;
497     }
498     plane.pln_range = UCHAR_MAX; /* will be adjusted by pln_set_tech() */
499     plane.pln_ship = -1;
500     plane.pln_land = -1;
501     plane.pln_harden = 0;
502     plane.pln_flags = 0;
503     pln_set_tech(&plane, tlev);
504     unit_wipe_orders((struct empobj *)&plane);
505
506     vec[I_LCM] -= lcm;
507     vec[I_HCM] -= hcm;
508     vec[I_MILIT] -= mil;
509
510     putplane(plane.pln_uid, &plane);
511     pr("%s built in sector %s\n", prplane(&plane),
512        xyas(sp->sct_x, sp->sct_y, player->cnum));
513     return 1;
514 }
515
516 static int
517 pick_unused_unit_uid(int type)
518 {
519     struct nstr_item nstr;
520     union empobj_storage unit;
521
522     snxtitem_all(&nstr, type);
523     while (nxtitem(&nstr, &unit)) {
524         if (!unit.gen.own)
525             return nstr.cur;
526     }
527     ef_extend(type, 50);
528     return nstr.cur;
529 }
530
531 static int
532 build_bridge(char what)
533 {
534     struct natstr *natp = getnatp(player->cnum);
535     struct nstr_sect nstr;
536     int (*build_it)(struct sctstr *);
537     int gotsect;
538     struct sctstr sect;
539
540     switch (what) {
541     case 'b':
542         if (natp->nat_level[NAT_TLEV] < buil_bt) {
543             pr("Building a span requires a tech of %.0f\n", buil_bt);
544             return RET_FAIL;
545         }
546         build_it = build_bspan;
547         break;
548     case 't':
549         if (!opt_BRIDGETOWERS) {
550             pr("Bridge tower building is disabled.\n");
551             return RET_FAIL;
552         }
553         if (natp->nat_level[NAT_TLEV] < buil_tower_bt) {
554             pr("Building a tower requires a tech of %.0f\n",
555                buil_tower_bt);
556             return RET_FAIL;
557         }
558         build_it = build_btower;
559         break;
560     default:
561         CANT_REACH();
562         return RET_FAIL;
563     }
564
565     if (!snxtsct(&nstr, player->argp[2]))
566         return RET_SYN;
567     gotsect = 0;
568     while (nxtsct(&nstr, &sect)) {
569         if (!player->owner)
570             continue;
571         gotsect = 1;
572         if (build_it(&sect))
573             putsect(&sect);
574     }
575     if (!gotsect)
576         pr("No sectors.\n");
577     return RET_OK;
578 }
579
580 static int
581 build_bspan(struct sctstr *sp)
582 {
583     short *vec = sp->sct_item;
584     struct sctstr sect;
585     int val;
586     int newx, newy;
587     int avail;
588     int nx, ny, i, good = 0;
589     char *p;
590     char buf[1024];
591
592     if (opt_EASY_BRIDGES == 0) {        /* must have a bridge head or tower */
593         if (sp->sct_type != SCT_BTOWER) {
594             if (sp->sct_type != SCT_BHEAD)
595                 return 0;
596             if (sp->sct_newtype != SCT_BHEAD)
597                 return 0;
598         }
599     }
600
601     if (sp->sct_effic < 60 && !player->god) {
602         pr("Sector %s is not 60%% efficient.\n",
603            xyas(sp->sct_x, sp->sct_y, player->cnum));
604         return 0;
605     }
606
607     if (vec[I_HCM] < buil_bh) {
608         pr("%s only has %d unit%s of hcm,\n",
609            xyas(sp->sct_x, sp->sct_y, player->cnum),
610            vec[I_HCM], vec[I_HCM] > 1 ? "s" : "");
611         pr("(a bridge span requires %d)\n", buil_bh);
612         return 0;
613     }
614
615     if (!build_can_afford(buil_bc, dchr[SCT_BSPAN].d_name))
616         return 0;
617     avail = (SCT_BLD_WORK(0, buil_bh) * SCT_MINEFF + 99) / 100;
618     if (sp->sct_avail < avail) {
619         pr("Not enough available work in %s to build a bridge\n",
620            xyas(sp->sct_x, sp->sct_y, player->cnum));
621         pr(" (%d available work required)\n", avail);
622         return 0;
623     }
624     if (!player->argp[3]) {
625         pr("Bridge head at %s\n",
626            xyas(sp->sct_x, sp->sct_y, player->cnum));
627         nav_map(sp->sct_x, sp->sct_y, 1);
628     }
629     p = getstarg(player->argp[3], "build span in what direction? ", buf);
630     if (!p || !*p) {
631         return 0;
632     }
633     /* Sanity check time */
634     if (!check_sect_ok(sp))
635         return 0;
636
637     if ((val = chkdir(*p, DIR_FIRST, DIR_LAST)) < 0) {
638         pr("'%c' is not a valid direction...\n", *p);
639         direrr(NULL, NULL, NULL);
640         return 0;
641     }
642     newx = sp->sct_x + diroff[val][0];
643     newy = sp->sct_y + diroff[val][1];
644     if (getsect(newx, newy, &sect) == 0 || sect.sct_type != SCT_WATER) {
645         pr("%s is not a water sector\n", xyas(newx, newy, player->cnum));
646         return 0;
647     }
648     if (opt_EASY_BRIDGES) {
649         good = 0;
650
651         for (i = 1; i <= 6; i++) {
652             struct sctstr s2;
653             nx = sect.sct_x + diroff[i][0];
654             ny = sect.sct_y + diroff[i][1];
655             getsect(nx, ny, &s2);
656             if ((s2.sct_type != SCT_WATER) && (s2.sct_type != SCT_BSPAN))
657                 good = 1;
658         }
659         if (!good) {
660             pr("Bridges must be built adjacent to land or bridge towers.\n");
661             pr("That sector is not adjacent to land or a bridge tower.\n");
662             return 0;
663         }
664     }                           /* end EASY_BRIDGES */
665     sp->sct_avail -= avail;
666     player->dolcost += buil_bc;
667     sect.sct_type = SCT_BSPAN;
668     sect.sct_newtype = SCT_BSPAN;
669     sect.sct_effic = SCT_MINEFF;
670     sect.sct_road = 0;
671     sect.sct_rail = 0;
672     sect.sct_defense = 0;
673     if (opt_MOB_ACCESS) {
674         game_tick_to_now(&sect.sct_access);
675         sect.sct_mobil = -(etu_per_update / sect_mob_neg_factor);
676     } else {
677         sect.sct_mobil = 0;
678     }
679     sect.sct_mines = 0;
680     map_set(player->cnum, sect.sct_x, sect.sct_y, dchr[SCT_BSPAN].d_mnem, 2);
681     writemap(player->cnum);
682     putsect(&sect);
683     pr("Bridge span built over %s\n",
684        xyas(sect.sct_x, sect.sct_y, player->cnum));
685     vec[I_HCM] -= buil_bh;
686     return 1;
687 }
688
689 static int
690 build_btower(struct sctstr *sp)
691 {
692     short *vec = sp->sct_item;
693     struct sctstr sect;
694     int val;
695     int newx, newy;
696     int avail;
697     char *p;
698     char buf[1024];
699     int i;
700     int nx;
701     int ny;
702
703     if (sp->sct_type != SCT_BSPAN) {
704         pr("Bridge towers can only be built from bridge spans.\n");
705         return 0;
706     }
707
708     if (sp->sct_effic < 60 && !player->god) {
709         pr("Sector %s is not 60%% efficient.\n",
710            xyas(sp->sct_x, sp->sct_y, player->cnum));
711         return 0;
712     }
713
714     if (vec[I_HCM] < buil_tower_bh) {
715         pr("%s only has %d unit%s of hcm,\n",
716            xyas(sp->sct_x, sp->sct_y, player->cnum),
717            vec[I_HCM], vec[I_HCM] > 1 ? "s" : "");
718         pr("(a bridge tower requires %d)\n", buil_tower_bh);
719         return 0;
720     }
721
722     if (!build_can_afford(buil_tower_bc, dchr[SCT_BTOWER].d_name))
723         return 0;
724     avail = (SCT_BLD_WORK(0, buil_tower_bh) * SCT_MINEFF + 99) / 100;
725     if (sp->sct_avail < avail) {
726         pr("Not enough available work in %s to build a bridge tower\n",
727            xyas(sp->sct_x, sp->sct_y, player->cnum));
728         pr(" (%d available work required)\n", avail);
729         return 0;
730     }
731     if (!player->argp[3]) {
732         pr("Building from %s\n", xyas(sp->sct_x, sp->sct_y, player->cnum));
733         nav_map(sp->sct_x, sp->sct_y, 1);
734     }
735     p = getstarg(player->argp[3], "build tower in what direction? ", buf);
736     if (!p || !*p) {
737         return 0;
738     }
739     /* Sanity check time */
740     if (!check_sect_ok(sp))
741         return 0;
742
743     if ((val = chkdir(*p, DIR_FIRST, DIR_LAST)) < 0) {
744         pr("'%c' is not a valid direction...\n", *p);
745         direrr(NULL, NULL, NULL);
746         return 0;
747     }
748     newx = sp->sct_x + diroff[val][0];
749     newy = sp->sct_y + diroff[val][1];
750     if (getsect(newx, newy, &sect) == 0 || sect.sct_type != SCT_WATER) {
751         pr("%s is not a water sector\n", xyas(newx, newy, player->cnum));
752         return 0;
753     }
754
755     /* Now, check.  You aren't allowed to build bridge towers
756        next to land. */
757     for (i = 1; i <= 6; i++) {
758         struct sctstr s2;
759         nx = sect.sct_x + diroff[i][0];
760         ny = sect.sct_y + diroff[i][1];
761         getsect(nx, ny, &s2);
762         if ((s2.sct_type != SCT_WATER) &&
763             (s2.sct_type != SCT_BTOWER) && (s2.sct_type != SCT_BSPAN)) {
764             pr("Bridge towers cannot be built adjacent to land.\n");
765             pr("That sector is adjacent to land.\n");
766             return 0;
767         }
768     }
769
770     sp->sct_avail -= avail;
771     player->dolcost += buil_tower_bc;
772     sect.sct_type = SCT_BTOWER;
773     sect.sct_newtype = SCT_BTOWER;
774     sect.sct_effic = SCT_MINEFF;
775     sect.sct_road = 0;
776     sect.sct_rail = 0;
777     sect.sct_defense = 0;
778     if (opt_MOB_ACCESS) {
779         game_tick_to_now(&sect.sct_access);
780         sect.sct_mobil = -(etu_per_update / sect_mob_neg_factor);
781     } else {
782         sect.sct_mobil = 0;
783     }
784     sect.sct_mines = 0;
785     map_set(player->cnum, sect.sct_x, sect.sct_y, dchr[SCT_BTOWER].d_mnem, 2);
786     writemap(player->cnum);
787     putsect(&sect);
788     pr("Bridge tower built in %s\n",
789        xyas(sect.sct_x, sect.sct_y, player->cnum));
790     vec[I_HCM] -= buil_tower_bh;
791     return 1;
792 }
793
794 static int
795 build_can_afford(double cost, char *what)
796 {
797     struct natstr *natp = getnatp(player->cnum);
798     if (natp->nat_money < player->dolcost + cost) {
799         pr("Not enough money left to build a %s\n", what);
800         return 0;
801     }
802     return 1;
803 }