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