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