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