]> git.pond.sub.org Git - empserver/blob - src/lib/commands/buil.c
e962bde7b557bdd43951f42552bfc0ace3ea6473
[empserver] / src / lib / commands / buil.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2008, 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-2008
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     if ((p =
90          getstarg(player->argp[1],
91                   "Build (ship, nuke, bridge, plane, land unit, tower)? ",
92                   buf)) == 0)
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 == 0 || *p == 0)
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 == 0 || *p == 0)
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 == 0 || *p == 0)
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 == 0 || *p == 0)
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 == 0 || *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_flags = 0;
462     land.lnd_ship = -1;
463     land.lnd_land = -1;
464     land.lnd_harden = 0;
465     memset(land.lnd_item, 0, sizeof(land.lnd_item));
466     land.lnd_pstage = PLG_HEALTHY;
467     land.lnd_ptime = 0;
468     lnd_set_tech(&land, tlev);
469     unit_wipe_orders((struct empobj *)&land);
470
471     vec[I_LCM] -= lcm;
472     vec[I_HCM] -= hcm;
473     vec[I_MILIT] -= mil;
474     vec[I_GUN] -= gun;
475     vec[I_SHELL] -= shell;
476
477     if (sp->sct_pstage == PLG_INFECT)
478         land.lnd_pstage = PLG_EXPOSED;
479     putland(nstr.cur, &land);
480     pr("%s", prland(&land));
481     pr(" built in sector %s\n", xyas(sp->sct_x, sp->sct_y, player->cnum));
482     return 1;
483 }
484
485 static int
486 build_bridge(struct sctstr *sp, short *vec)
487 {
488     struct sctstr sect;
489     int val;
490     int newx, newy;
491     int avail;
492     int nx, ny, i, good = 0;
493     char *p;
494     char buf[1024];
495
496     if (opt_EASY_BRIDGES == 0) {        /* must have a bridge head or tower */
497         if (sp->sct_type != SCT_BTOWER) {
498             if (sp->sct_type != SCT_BHEAD)
499                 return 0;
500             if (sp->sct_newtype != SCT_BHEAD)
501                 return 0;
502         }
503     }
504
505     if (sp->sct_effic < 60 && !player->god) {
506         pr("Sector %s is not 60%% efficient.\n",
507            xyas(sp->sct_x, sp->sct_y, player->cnum));
508         return 0;
509     }
510
511     if (vec[I_HCM] < buil_bh) {
512         pr("%s only has %d unit%s of hcm,\n",
513            xyas(sp->sct_x, sp->sct_y, player->cnum),
514            vec[I_HCM], vec[I_HCM] > 1 ? "s" : "");
515         pr("(a bridge span requires %d)\n", buil_bh);
516         return 0;
517     }
518
519     if (!build_can_afford(buil_bc, dchr[SCT_BSPAN].d_name))
520         return 0;
521     avail = (SCT_BLD_WORK(0, buil_bh) * SCT_MINEFF + 99) / 100;
522     if (sp->sct_avail < avail) {
523         pr("Not enough available work in %s to build a bridge\n",
524            xyas(sp->sct_x, sp->sct_y, player->cnum));
525         pr(" (%d available work required)\n", avail);
526         return 0;
527     }
528     if (!player->argp[3]) {
529         pr("Bridge head at %s\n",
530            xyas(sp->sct_x, sp->sct_y, player->cnum));
531         nav_map(sp->sct_x, sp->sct_y, 1);
532     }
533     p = getstarg(player->argp[3], "build span in what direction? ", buf);
534     if (!p || !*p) {
535         return 0;
536     }
537     /* Sanity check time */
538     if (!check_sect_ok(sp))
539         return 0;
540
541     if ((val = chkdir(*p, DIR_FIRST, DIR_LAST)) < 0) {
542         pr("'%c' is not a valid direction...\n", *p);
543         direrr(0, 0, 0);
544         return 0;
545     }
546     newx = sp->sct_x + diroff[val][0];
547     newy = sp->sct_y + diroff[val][1];
548     if (getsect(newx, newy, &sect) == 0 || sect.sct_type != SCT_WATER) {
549         pr("%s is not a water sector\n", xyas(newx, newy, player->cnum));
550         return 0;
551     }
552     if (opt_EASY_BRIDGES) {
553         good = 0;
554
555         for (i = 1; i <= 6; i++) {
556             struct sctstr s2;
557             nx = sect.sct_x + diroff[i][0];
558             ny = sect.sct_y + diroff[i][1];
559             getsect(nx, ny, &s2);
560             if ((s2.sct_type != SCT_WATER) && (s2.sct_type != SCT_BSPAN))
561                 good = 1;
562         }
563         if (!good) {
564             pr("Bridges must be built adjacent to land or bridge towers.\n");
565             pr("That sector is not adjacent to land or a bridge tower.\n");
566             return 0;
567         }
568     }                           /* end EASY_BRIDGES */
569     sp->sct_avail -= avail;
570     player->dolcost += buil_bc;
571     sect.sct_type = SCT_BSPAN;
572     sect.sct_newtype = SCT_BSPAN;
573     sect.sct_effic = SCT_MINEFF;
574     sect.sct_road = 0;
575     sect.sct_rail = 0;
576     sect.sct_defense = 0;
577     if (opt_MOB_ACCESS) {
578         game_tick_to_now(&sect.sct_access);
579         sect.sct_mobil = -(etu_per_update / sect_mob_neg_factor);
580     } else {
581         sect.sct_mobil = 0;
582     }
583     sect.sct_mines = 0;
584     map_set(player->cnum, sect.sct_x, sect.sct_y, dchr[SCT_BSPAN].d_mnem, 2);
585     writemap(player->cnum);
586     putsect(&sect);
587     pr("Bridge span built over %s\n",
588        xyas(sect.sct_x, sect.sct_y, player->cnum));
589     vec[I_HCM] -= buil_bh;
590     return 1;
591 }
592
593 static int
594 build_nuke(struct sctstr *sp, struct nchrstr *np, short *vec, int tlev)
595 {
596     struct nukstr nuke;
597     struct nstr_item nstr;
598     int avail;
599     int freenuke;
600
601     if (sp->sct_type != SCT_NUKE && !player->god) {
602         pr("Nuclear weapons must be built in nuclear plants.\n");
603         return 0;
604     }
605     if (sp->sct_effic < 60 && !player->god) {
606         pr("Sector %s is not 60%% efficient.\n",
607            xyas(sp->sct_x, sp->sct_y, player->cnum));
608         return 0;
609     }
610     if (vec[I_HCM] < np->n_hcm || vec[I_LCM] < np->n_lcm ||
611         vec[I_OIL] < np->n_oil || vec[I_RAD] < np->n_rad) {
612         pr("Not enough materials for a %s bomb in %s\n",
613            np->n_name, xyas(sp->sct_x, sp->sct_y, player->cnum));
614         pr("(%d hcm, %d lcm, %d oil, & %d rads).\n",
615            np->n_hcm, np->n_lcm, np->n_oil, np->n_rad);
616         return 0;
617     }
618     if (!build_can_afford(np->n_cost, np->n_name))
619         return 0;
620     avail = NUK_BLD_WORK(np->n_lcm, np->n_hcm, np->n_oil, np->n_rad);
621     /*
622      * XXX when nukes turn into units (or whatever), then
623      * make them start at 20%.  Since they don't have efficiency
624      * now, we charge all the work right away.
625      */
626     if (sp->sct_avail < avail) {
627         pr("Not enough available work in %s to build a %s;\n",
628            xyas(sp->sct_x, sp->sct_y, player->cnum), np->n_name);
629         pr(" (%d available work required)\n", avail);
630         return 0;
631     }
632     if (!trechk(player->cnum, 0, NEWNUK))
633         return 0;
634     if (!check_sect_ok(sp))
635         return 0;
636     sp->sct_avail -= avail;
637     player->dolcost += np->n_cost;
638     snxtitem_all(&nstr, EF_NUKE);
639     freenuke = 0;
640     while (nxtitem(&nstr, &nuke)) {
641         if (nuke.nuk_own == 0) {
642             freenuke++;
643             break;
644         }
645     }
646     if (freenuke == 0) {
647         ef_extend(EF_NUKE, 50);
648     }
649     ef_blank(EF_NUKE, nstr.cur, &nuke);
650     nuke.nuk_x = sp->sct_x;
651     nuke.nuk_y = sp->sct_y;
652     nuke.nuk_own = sp->sct_own;
653     nuke.nuk_type = np - nchr;
654     nuke.nuk_effic = 100;
655     nuke.nuk_ship = nuke.nuk_plane = nuke.nuk_land = -1;
656     nuke.nuk_tech = tlev;
657     unit_wipe_orders((struct empobj *)&nuke);
658
659     vec[I_HCM] -= np->n_hcm;
660     vec[I_LCM] -= np->n_lcm;
661     vec[I_OIL] -= np->n_oil;
662     vec[I_RAD] -= np->n_rad;
663
664     putnuke(nuke.nuk_uid, &nuke);
665     pr("%s created in %s\n", prnuke(&nuke),
666        xyas(sp->sct_x, sp->sct_y, player->cnum));
667     return 1;
668 }
669
670 static int
671 build_plane(struct sctstr *sp, struct plchrstr *pp, short *vec, int tlev)
672 {
673     struct plnstr plane;
674     struct nstr_item nstr;
675     int avail;
676     double cost;
677     double eff = PLANE_MINEFF / 100.0;
678     int hcm, lcm, mil;
679     int freeplane;
680
681     mil = roundavg(pp->pl_crew * eff);
682     /* Always use at least 1 mil to build a plane */
683     if (mil == 0 && pp->pl_crew > 0)
684         mil = 1;
685     hcm = roundavg(pp->pl_hcm * eff);
686     lcm = roundavg(pp->pl_lcm * eff);
687     if (sp->sct_type != SCT_AIRPT && !player->god) {
688         pr("Planes must be built in airports.\n");
689         return 0;
690     }
691     if (sp->sct_effic < 60 && !player->god) {
692         pr("Sector %s is not 60%% efficient.\n",
693            xyas(sp->sct_x, sp->sct_y, player->cnum));
694         return 0;
695     }
696     if (vec[I_LCM] < lcm || vec[I_HCM] < hcm) {
697         pr("Not enough materials in %s\n",
698            xyas(sp->sct_x, sp->sct_y, player->cnum));
699         return 0;
700     }
701     avail = (PLN_BLD_WORK(pp->pl_lcm, pp->pl_hcm) * PLANE_MINEFF + 99) / 100;
702     if (sp->sct_avail < avail) {
703         pr("Not enough available work in %s to build a %s\n",
704            xyas(sp->sct_x, sp->sct_y, player->cnum), pp->pl_name);
705         pr(" (%d available work required)\n", avail);
706         return 0;
707     }
708     cost = pp->pl_cost * PLANE_MINEFF / 100.0;
709     if (!build_can_afford(cost, pp->pl_name))
710         return 0;
711     if (vec[I_MILIT] < mil || (vec[I_MILIT] == 0 && pp->pl_crew > 0)) {
712         pr("Not enough military for crew in %s\n",
713            xyas(sp->sct_x, sp->sct_y, player->cnum));
714         return 0;
715     }
716     if (!trechk(player->cnum, 0, NEWPLN))
717         return 0;
718     if (!check_sect_ok(sp))
719         return 0;
720     sp->sct_avail -= avail;
721     player->dolcost += cost;
722     snxtitem_all(&nstr, EF_PLANE);
723     freeplane = 0;
724     while (nxtitem(&nstr, &plane)) {
725         if (plane.pln_own == 0) {
726             freeplane++;
727             break;
728         }
729     }
730     if (freeplane == 0) {
731         ef_extend(EF_PLANE, 50);
732     }
733     ef_blank(EF_PLANE, nstr.cur, &plane);
734     plane.pln_x = sp->sct_x;
735     plane.pln_y = sp->sct_y;
736     plane.pln_own = sp->sct_own;
737     plane.pln_type = pp - plchr;
738     plane.pln_effic = PLANE_MINEFF;
739     if (opt_MOB_ACCESS) {
740         game_tick_to_now(&plane.pln_access);
741         plane.pln_mobil = -(etu_per_update / sect_mob_neg_factor);
742     } else {
743         plane.pln_mobil = 0;
744     }
745     plane.pln_range = UCHAR_MAX; /* will be adjusted by pln_set_tech() */
746     plane.pln_ship = -1;
747     plane.pln_land = -1;
748     plane.pln_harden = 0;
749     plane.pln_flags = 0;
750     pln_set_tech(&plane, tlev);
751     unit_wipe_orders((struct empobj *)&plane);
752
753     vec[I_LCM] -= lcm;
754     vec[I_HCM] -= hcm;
755     vec[I_MILIT] -= mil;
756
757     putplane(plane.pln_uid, &plane);
758     pr("%s built in sector %s\n", prplane(&plane),
759        xyas(sp->sct_x, sp->sct_y, player->cnum));
760     return 1;
761 }
762
763 static int
764 build_tower(struct sctstr *sp, short *vec)
765 {
766     struct sctstr sect;
767     int val;
768     int newx, newy;
769     int avail;
770     char *p;
771     char buf[1024];
772     int good;
773     int i;
774     int nx;
775     int ny;
776
777     if (sp->sct_type != SCT_BSPAN) {
778         pr("Bridge towers can only be built from bridge spans.\n");
779         return 0;
780     }
781
782     if (sp->sct_effic < 60 && !player->god) {
783         pr("Sector %s is not 60%% efficient.\n",
784            xyas(sp->sct_x, sp->sct_y, player->cnum));
785         return 0;
786     }
787
788     if (vec[I_HCM] < buil_tower_bh) {
789         pr("%s only has %d unit%s of hcm,\n",
790            xyas(sp->sct_x, sp->sct_y, player->cnum),
791            vec[I_HCM], vec[I_HCM] > 1 ? "s" : "");
792         pr("(a bridge tower requires %d)\n", buil_tower_bh);
793         return 0;
794     }
795
796     if (!build_can_afford(buil_tower_bc, dchr[SCT_BTOWER].d_name))
797         return 0;
798     avail = (SCT_BLD_WORK(0, buil_tower_bh) * SCT_MINEFF + 99) / 100;
799     if (sp->sct_avail < avail) {
800         pr("Not enough available work in %s to build a bridge tower\n",
801            xyas(sp->sct_x, sp->sct_y, player->cnum));
802         pr(" (%d available work required)\n", avail);
803         return 0;
804     }
805     if (!player->argp[3]) {
806         pr("Building from %s\n", xyas(sp->sct_x, sp->sct_y, player->cnum));
807         nav_map(sp->sct_x, sp->sct_y, 1);
808     }
809     p = getstarg(player->argp[3], "build tower in what direction? ", buf);
810     if (!p || !*p) {
811         return 0;
812     }
813     /* Sanity check time */
814     if (!check_sect_ok(sp))
815         return 0;
816
817     if ((val = chkdir(*p, DIR_FIRST, DIR_LAST)) < 0) {
818         pr("'%c' is not a valid direction...\n", *p);
819         direrr(0, 0, 0);
820         return 0;
821     }
822     newx = sp->sct_x + diroff[val][0];
823     newy = sp->sct_y + diroff[val][1];
824     if (getsect(newx, newy, &sect) == 0 || sect.sct_type != SCT_WATER) {
825         pr("%s is not a water sector\n", xyas(newx, newy, player->cnum));
826         return 0;
827     }
828
829     /* Now, check.  You aren't allowed to build bridge towers
830        next to land. */
831     good = 0;
832     for (i = 1; i <= 6; i++) {
833         struct sctstr s2;
834         nx = sect.sct_x + diroff[i][0];
835         ny = sect.sct_y + diroff[i][1];
836         getsect(nx, ny, &s2);
837         if ((s2.sct_type != SCT_WATER) &&
838             (s2.sct_type != SCT_BTOWER) && (s2.sct_type != SCT_BSPAN)) {
839             good = 1;
840             break;
841         }
842     }
843     if (good) {
844         pr("Bridge towers cannot be built adjacent to land.\n");
845         pr("That sector is adjacent to land.\n");
846         return 0;
847     }
848
849     sp->sct_avail -= avail;
850     player->dolcost += buil_tower_bc;
851     sect.sct_type = SCT_BTOWER;
852     sect.sct_newtype = SCT_BTOWER;
853     sect.sct_effic = SCT_MINEFF;
854     sect.sct_road = 0;
855     sect.sct_rail = 0;
856     sect.sct_defense = 0;
857     if (opt_MOB_ACCESS) {
858         game_tick_to_now(&sect.sct_access);
859         sect.sct_mobil = -(etu_per_update / sect_mob_neg_factor);
860     } else {
861         sect.sct_mobil = 0;
862     }
863     sect.sct_mines = 0;
864     map_set(player->cnum, sect.sct_x, sect.sct_y, dchr[SCT_BTOWER].d_mnem, 2);
865     writemap(player->cnum);
866     putsect(&sect);
867     pr("Bridge tower built in %s\n",
868        xyas(sect.sct_x, sect.sct_y, player->cnum));
869     vec[I_HCM] -= buil_tower_bh;
870     return 1;
871 }
872
873 static int
874 build_can_afford(double cost, char *what)
875 {
876     struct natstr *natp = getnatp(player->cnum);
877     if (natp->nat_money < player->dolcost + cost) {
878         pr("Not enough money left to build a %s\n", what);
879         return 0;
880     }
881     return 1;
882 }