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