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