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