]> git.pond.sub.org Git - empserver/blob - src/lib/commands/buil.c
Bridge building required 0.005 tech less than advertized, fix
[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 (*build_it)(struct sctstr *, short[]);
647     int gotsect;
648     struct sctstr sect;
649
650     switch (what) {
651     case 'b':
652         if (natp->nat_level[NAT_TLEV] < buil_bt) {
653             pr("Building a span requires a tech of %.0f\n", buil_bt);
654             return RET_FAIL;
655         }
656         build_it = build_bspan;
657         break;
658     case 't':
659         if (!opt_BRIDGETOWERS) {
660             pr("Bridge tower building is disabled.\n");
661             return RET_FAIL;
662         }
663         if (natp->nat_level[NAT_TLEV] < buil_tower_bt) {
664             pr("Building a tower requires a tech of %.0f\n",
665                buil_tower_bt);
666             return RET_FAIL;
667         }
668         build_it = build_btower;
669         break;
670     default:
671         CANT_REACH();
672         return RET_FAIL;
673     }
674
675     if (!snxtsct(&nstr, player->argp[2]))
676         return RET_SYN;
677     gotsect = 0;
678     while (nxtsct(&nstr, &sect)) {
679         gotsect++;
680         if (!player->owner)
681             continue;
682         if (build_it(&sect, sect.sct_item))
683             putsect(&sect);
684     }
685     if (!gotsect) {
686         pr("Bad sector specification.\n");
687     }
688     return RET_OK;
689 }
690
691 static int
692 build_bspan(struct sctstr *sp, short *vec)
693 {
694     struct sctstr sect;
695     int val;
696     int newx, newy;
697     int avail;
698     int nx, ny, i, good = 0;
699     char *p;
700     char buf[1024];
701
702     if (opt_EASY_BRIDGES == 0) {        /* must have a bridge head or tower */
703         if (sp->sct_type != SCT_BTOWER) {
704             if (sp->sct_type != SCT_BHEAD)
705                 return 0;
706             if (sp->sct_newtype != SCT_BHEAD)
707                 return 0;
708         }
709     }
710
711     if (sp->sct_effic < 60 && !player->god) {
712         pr("Sector %s is not 60%% efficient.\n",
713            xyas(sp->sct_x, sp->sct_y, player->cnum));
714         return 0;
715     }
716
717     if (vec[I_HCM] < buil_bh) {
718         pr("%s only has %d unit%s of hcm,\n",
719            xyas(sp->sct_x, sp->sct_y, player->cnum),
720            vec[I_HCM], vec[I_HCM] > 1 ? "s" : "");
721         pr("(a bridge span requires %d)\n", buil_bh);
722         return 0;
723     }
724
725     if (!build_can_afford(buil_bc, dchr[SCT_BSPAN].d_name))
726         return 0;
727     avail = (SCT_BLD_WORK(0, buil_bh) * SCT_MINEFF + 99) / 100;
728     if (sp->sct_avail < avail) {
729         pr("Not enough available work in %s to build a bridge\n",
730            xyas(sp->sct_x, sp->sct_y, player->cnum));
731         pr(" (%d available work required)\n", avail);
732         return 0;
733     }
734     if (!player->argp[3]) {
735         pr("Bridge head at %s\n",
736            xyas(sp->sct_x, sp->sct_y, player->cnum));
737         nav_map(sp->sct_x, sp->sct_y, 1);
738     }
739     p = getstarg(player->argp[3], "build span in what direction? ", buf);
740     if (!p || !*p) {
741         return 0;
742     }
743     /* Sanity check time */
744     if (!check_sect_ok(sp))
745         return 0;
746
747     if ((val = chkdir(*p, DIR_FIRST, DIR_LAST)) < 0) {
748         pr("'%c' is not a valid direction...\n", *p);
749         direrr(NULL, NULL, NULL);
750         return 0;
751     }
752     newx = sp->sct_x + diroff[val][0];
753     newy = sp->sct_y + diroff[val][1];
754     if (getsect(newx, newy, &sect) == 0 || sect.sct_type != SCT_WATER) {
755         pr("%s is not a water sector\n", xyas(newx, newy, player->cnum));
756         return 0;
757     }
758     if (opt_EASY_BRIDGES) {
759         good = 0;
760
761         for (i = 1; i <= 6; i++) {
762             struct sctstr s2;
763             nx = sect.sct_x + diroff[i][0];
764             ny = sect.sct_y + diroff[i][1];
765             getsect(nx, ny, &s2);
766             if ((s2.sct_type != SCT_WATER) && (s2.sct_type != SCT_BSPAN))
767                 good = 1;
768         }
769         if (!good) {
770             pr("Bridges must be built adjacent to land or bridge towers.\n");
771             pr("That sector is not adjacent to land or a bridge tower.\n");
772             return 0;
773         }
774     }                           /* end EASY_BRIDGES */
775     sp->sct_avail -= avail;
776     player->dolcost += buil_bc;
777     sect.sct_type = SCT_BSPAN;
778     sect.sct_newtype = SCT_BSPAN;
779     sect.sct_effic = SCT_MINEFF;
780     sect.sct_road = 0;
781     sect.sct_rail = 0;
782     sect.sct_defense = 0;
783     if (opt_MOB_ACCESS) {
784         game_tick_to_now(&sect.sct_access);
785         sect.sct_mobil = -(etu_per_update / sect_mob_neg_factor);
786     } else {
787         sect.sct_mobil = 0;
788     }
789     sect.sct_mines = 0;
790     map_set(player->cnum, sect.sct_x, sect.sct_y, dchr[SCT_BSPAN].d_mnem, 2);
791     writemap(player->cnum);
792     putsect(&sect);
793     pr("Bridge span built over %s\n",
794        xyas(sect.sct_x, sect.sct_y, player->cnum));
795     vec[I_HCM] -= buil_bh;
796     return 1;
797 }
798
799 static int
800 build_btower(struct sctstr *sp, short *vec)
801 {
802     struct sctstr sect;
803     int val;
804     int newx, newy;
805     int avail;
806     char *p;
807     char buf[1024];
808     int i;
809     int nx;
810     int ny;
811
812     if (sp->sct_type != SCT_BSPAN) {
813         pr("Bridge towers can only be built from bridge spans.\n");
814         return 0;
815     }
816
817     if (sp->sct_effic < 60 && !player->god) {
818         pr("Sector %s is not 60%% efficient.\n",
819            xyas(sp->sct_x, sp->sct_y, player->cnum));
820         return 0;
821     }
822
823     if (vec[I_HCM] < buil_tower_bh) {
824         pr("%s only has %d unit%s of hcm,\n",
825            xyas(sp->sct_x, sp->sct_y, player->cnum),
826            vec[I_HCM], vec[I_HCM] > 1 ? "s" : "");
827         pr("(a bridge tower requires %d)\n", buil_tower_bh);
828         return 0;
829     }
830
831     if (!build_can_afford(buil_tower_bc, dchr[SCT_BTOWER].d_name))
832         return 0;
833     avail = (SCT_BLD_WORK(0, buil_tower_bh) * SCT_MINEFF + 99) / 100;
834     if (sp->sct_avail < avail) {
835         pr("Not enough available work in %s to build a bridge tower\n",
836            xyas(sp->sct_x, sp->sct_y, player->cnum));
837         pr(" (%d available work required)\n", avail);
838         return 0;
839     }
840     if (!player->argp[3]) {
841         pr("Building from %s\n", xyas(sp->sct_x, sp->sct_y, player->cnum));
842         nav_map(sp->sct_x, sp->sct_y, 1);
843     }
844     p = getstarg(player->argp[3], "build tower in what direction? ", buf);
845     if (!p || !*p) {
846         return 0;
847     }
848     /* Sanity check time */
849     if (!check_sect_ok(sp))
850         return 0;
851
852     if ((val = chkdir(*p, DIR_FIRST, DIR_LAST)) < 0) {
853         pr("'%c' is not a valid direction...\n", *p);
854         direrr(NULL, NULL, NULL);
855         return 0;
856     }
857     newx = sp->sct_x + diroff[val][0];
858     newy = sp->sct_y + diroff[val][1];
859     if (getsect(newx, newy, &sect) == 0 || sect.sct_type != SCT_WATER) {
860         pr("%s is not a water sector\n", xyas(newx, newy, player->cnum));
861         return 0;
862     }
863
864     /* Now, check.  You aren't allowed to build bridge towers
865        next to land. */
866     for (i = 1; i <= 6; i++) {
867         struct sctstr s2;
868         nx = sect.sct_x + diroff[i][0];
869         ny = sect.sct_y + diroff[i][1];
870         getsect(nx, ny, &s2);
871         if ((s2.sct_type != SCT_WATER) &&
872             (s2.sct_type != SCT_BTOWER) && (s2.sct_type != SCT_BSPAN)) {
873             pr("Bridge towers cannot be built adjacent to land.\n");
874             pr("That sector is adjacent to land.\n");
875             return 0;
876         }
877     }
878
879     sp->sct_avail -= avail;
880     player->dolcost += buil_tower_bc;
881     sect.sct_type = SCT_BTOWER;
882     sect.sct_newtype = SCT_BTOWER;
883     sect.sct_effic = SCT_MINEFF;
884     sect.sct_road = 0;
885     sect.sct_rail = 0;
886     sect.sct_defense = 0;
887     if (opt_MOB_ACCESS) {
888         game_tick_to_now(&sect.sct_access);
889         sect.sct_mobil = -(etu_per_update / sect_mob_neg_factor);
890     } else {
891         sect.sct_mobil = 0;
892     }
893     sect.sct_mines = 0;
894     map_set(player->cnum, sect.sct_x, sect.sct_y, dchr[SCT_BTOWER].d_mnem, 2);
895     writemap(player->cnum);
896     putsect(&sect);
897     pr("Bridge tower built in %s\n",
898        xyas(sect.sct_x, sect.sct_y, player->cnum));
899     vec[I_HCM] -= buil_tower_bh;
900     return 1;
901 }
902
903 static int
904 build_can_afford(double cost, char *what)
905 {
906     struct natstr *natp = getnatp(player->cnum);
907     if (natp->nat_money < player->dolcost + cost) {
908         pr("Not enough money left to build a %s\n", what);
909         return 0;
910     }
911     return 1;
912 }