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