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