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