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