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