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