]> git.pond.sub.org Git - empserver/blob - src/lib/commands/buil.c
(SCT_BLD_WORK, SHP_BLD_WORK, PLN_BLD_WORK, LND_BLD_WORK,
[empserver] / src / lib / commands / buil.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2000, 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 the "LEGAL", "LICENSE", "CREDITS" and "README" files for all the
23  *  related information and legal notices. It is expected that any future
24  *  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  */
33
34 #include <math.h>
35 #include <string.h>
36 #include "misc.h"
37 #include "player.h"
38 #include "var.h"
39 #include "sect.h"
40 #include "nat.h"
41 #include "retreat.h"
42 #include "ship.h"
43 #include "land.h"
44 #include "nuke.h"
45 #include "plane.h"
46 #include "xy.h"
47 #include "nsc.h"
48 #include "treaty.h"
49 #include "file.h"
50 #include "path.h"
51 #include "optlist.h"
52 #include "commands.h"
53
54 static int build_nuke(register struct sctstr *sp,
55                       register struct nchrstr *np, short *vec);
56 static int build_ship(register struct sctstr *sp,
57                       register struct mchrstr *mp, short *vec,
58                       int tlev);
59 static int build_land(register struct sctstr *sp,
60                       register struct lchrstr *lp, short *vec,
61                       int tlev);
62 static int build_bridge(register struct sctstr *sp, short *vec);
63 static int build_tower(register struct sctstr *sp, short *vec);
64 static int build_plane(register struct sctstr *sp,
65                        register struct plchrstr *pp, short *vec,
66                        int tlev);
67
68 static int cash;                /* static ok */
69
70 /*
71  * build <WHAT> <SECTS> <TYPE|DIR|MEG> [NUMBER]
72  */
73 int
74 buil(void)
75 {
76     struct sctstr sect;
77     struct nstr_sect nstr;
78     struct natstr *natp;
79     int rqtech;
80     int tlev;
81     int rlev;
82     int n;
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 hold, found, 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       ask_again:
110         tlev = (int)natp->nat_level[NAT_TLEV];
111         rlev = (int)natp->nat_level[NAT_RLEV];
112
113         switch (what) {
114         case 'p':
115             p = getstarg(player->argp[3], "Plane type? ", buf);
116             if (p == 0 || *p == 0)
117                 return RET_SYN;
118             n = strlen(p);
119             while (n && iscntrl(p[n - 1]))
120                 n--;
121             if (!n)
122                 return RET_SYN;
123             for (found = 0, type = 0, pp = plchr; type <= pln_maxno;
124                  type++, pp++) {
125                 if (pp->pl_tech > tlev)
126                     continue;
127                 if (pp->pl_name && strncmp(p, pp->pl_name, n) == 0) {
128                     found++;
129                     hold = type;
130                     break;
131                 }
132             }
133             if (found != 1) {
134                 pr("Illegal plane type: \"%s\"\n", p);
135                 if (confirm("List plane types? "))
136                     show_plane_build(tlev);
137                 player->argp[3] = 0;
138                 goto ask_again;
139             }
140             type = hold;
141             pp = &(plchr[type]);
142             rqtech = pp->pl_tech;
143             break;
144         case 's':
145             p = getstarg(player->argp[3], "Ship type? ", buf);
146             if (p == 0 || *p == 0)
147                 return RET_SYN;
148             n = strlen(p);
149             while (n && iscntrl(p[n - 1]))
150                 n--;
151             if (!n)
152                 return RET_SYN;
153             for (found = 0, mp = mchr, type = 0; type <= shp_maxno;
154                  type++, mp++) {
155                 if (mp->m_tech > tlev)
156                     continue;
157                 /* Can't build trade ships unless it's turned on */
158                 if ((mp->m_flags & M_TRADE) && !opt_TRADESHIPS)
159                     continue;
160                 if (mp->m_name && strncmp(p, mp->m_name, n) == 0) {
161                     found++;
162                     hold = type;
163                     break;
164                 }
165             }
166             if (found != 1) {
167                 pr("Illegal ship type: \"%s\"\n", p);
168                 if (confirm("List ship types? "))
169                     show_ship_build(tlev);
170                 player->argp[3] = 0;
171                 goto ask_again;
172             }
173             type = hold;
174             mp = &(mchr[type]);
175             rqtech = mp->m_tech;
176             break;
177         case 'l':
178             p = getstarg(player->argp[3], "Land unit type? ", buf);
179             if (p == 0 || *p == 0)
180                 return RET_SYN;
181             n = strlen(p);
182             while (n && iscntrl(p[n - 1]))
183                 n--;
184             if (!n)
185                 return RET_SYN;
186             for (found = 0, lp = lchr, type = 0; type <= lnd_maxno;
187                  type++, lp++) {
188                 if (lp->l_tech > tlev)
189                     continue;
190                 if ((lp->l_flags & L_SPY) && !opt_LANDSPIES)
191                     continue;
192                 if (lp->l_name && strncmp(p, lp->l_name, n) == 0) {
193                     found++;
194                     hold = type;
195                     break;
196                 }
197             }
198             if (found != 1) {
199                 pr("Illegal land unit type: \"%s\"\n", p);
200                 if (confirm("List unit types? "))
201                     show_land_build(tlev);
202                 player->argp[3] = 0;
203                 goto ask_again;
204             }
205             type = hold;
206             lp = &(lchr[type]);
207             rqtech = lp->l_tech;
208             break;
209         case 'b':
210             if (natp->nat_level[NAT_TLEV] + 0.005 < buil_bt) {
211                 pr("Building a span requires a tech of %.0f\n", buil_bt);
212                 return 2;
213             }
214             break;
215         case 't':
216             if (!opt_BRIDGETOWERS) {
217                 pr("Bridge tower building is disabled.\n");
218                 return RET_FAIL;
219             }
220             if (natp->nat_level[NAT_TLEV] + 0.005 < buil_tower_bt) {
221                 pr("Building a tower requires a tech of %.0f\n",
222                    buil_tower_bt);
223                 return 2;
224             }
225             break;
226         case 'n':
227             if (opt_NONUKES) {
228                 pr("There are no nukes in this game.\n");
229                 return RET_FAIL;
230             }
231             p = getstarg(player->argp[3], "Nuke type? ", buf);
232             if (p == 0 || *p == 0)
233                 return RET_SYN;
234             n = strlen(p);
235             while (n && iscntrl(p[n - 1]))
236                 n--;
237             if (!n)
238                 return RET_SYN;
239             for (found = 0, np = nchr, type = 0; type < nuk_maxno;
240                  type++, np++) {
241                 if ((np->n_tech > tlev)
242                     || (opt_DRNUKE
243                         && ((np->n_tech * drnuke_const) > rlev)))
244                     continue;
245                 if (opt_NEUTRON == 0 && (np->n_flags & N_NEUT))
246                     continue;
247
248                 if (np->n_name && strncmp(p, np->n_name, n) == 0) {
249                     found++;
250                     hold = type;
251                     break;
252                 }
253             }
254             if (found != 1) {
255                 int tt = tlev;
256                 pr("Possible nuke types are:\n");
257                 if (opt_DRNUKE)
258                     tt = (tlev < (rlev / drnuke_const) ? (int)tlev :
259                           (int)(rlev / drnuke_const));
260
261                 show_nuke_build(tt);
262                 player->argp[3] = 0;
263                 goto ask_again;
264             }
265             type = hold;
266             np = &(nchr[type]);
267             rqtech = np->n_tech;
268             break;
269         default:
270             pr("You can't build that!\n");
271             return RET_FAIL;
272         }
273         if (what != 'b' && what != 't') {
274             if (player->argp[4]) {
275                 if (atoi(player->argp[4]) > 20 && !asked) {
276                     s_char bstr[80];
277                     asked = 1;
278                     (void)sprintf(bstr,
279                                   "Are you sure that you want to build %s of them? ",
280                                   player->argp[4]);
281                     p = getstarg(player->argp[6], bstr, buf);
282                     if (p == 0 || *p != 'y')
283                         return RET_SYN;
284                 }
285                 number = atoi(player->argp[4]);
286             }
287         }
288         if (what != 'b' && what != 'n' && what != 't') {
289             if (player->argp[5]) {
290                 tlev = atoi(player->argp[5]);
291                 if (tlev > natp->nat_level[NAT_TLEV] && !player->god) {
292                     pr("Your tech level is only %d.\n",
293                        (int)natp->nat_level[NAT_TLEV]);
294                     return RET_FAIL;
295                 }
296                 if (rqtech > tlev) {
297                     pr("Required tech is %d.\n", rqtech);
298                     return RET_FAIL;
299                 }
300                 pr("building with tech level %d.\n", tlev);
301             } else
302                 tlev = (int)natp->nat_level[NAT_TLEV];
303         }
304         cash = natp->nat_money;
305         while (nxtsct(&nstr, &sect)) {
306             gotsect++;
307             if (!player->owner)
308                 continue;
309             switch (what) {
310             case 'l':
311                 built = build_land(&sect, lp, sect.sct_item, tlev);
312                 break;
313             case 's':
314                 built = build_ship(&sect, mp, sect.sct_item, tlev);
315                 break;
316             case 'b':
317                 built = build_bridge(&sect, sect.sct_item);
318                 break;
319             case 't':
320                 built = build_tower(&sect, sect.sct_item);
321                 break;
322             case 'n':
323                 built = build_nuke(&sect, np, sect.sct_item);
324                 break;
325             case 'p':
326                 built = build_plane(&sect, pp, sect.sct_item, tlev);
327                 break;
328             default:
329                 CANT_HAPPEN("Bad WHAT");
330                 return RET_FAIL;
331             }
332             if (built) {
333                 putsect(&sect);
334             }
335         }
336     }
337     if (!gotsect) {
338         pr("Bad sector specification.\n");
339     }
340     return RET_OK;
341 }
342
343 static int
344 build_ship(register struct sctstr *sp, register struct mchrstr *mp,
345            short *vec, int tlev)
346 {
347     struct shpstr ship;
348     struct nstr_item nstr;
349     int avail, cost, i;
350     float eff = SHIP_MINEFF / 100.0;
351     int lcm, hcm;
352     int freeship = 0;
353     int techdiff;
354
355     hcm = roundavg((double)mp->m_hcm * eff);
356     lcm = roundavg((double)mp->m_lcm * eff);
357
358     if (sp->sct_type != SCT_HARBR) {
359         pr("Ships must be built in harbours.\n");
360         return 0;
361     }
362     if (sp->sct_effic < 60 && !player->god) {
363         pr("Sector %s is not 60%% efficient.\n",
364            xyas(sp->sct_x, sp->sct_y, player->cnum));
365         return 0;
366     }
367     if (vec[I_LCM] < lcm || vec[I_HCM] < hcm) {
368         pr("Not enough materials in %s\n",
369            xyas(sp->sct_x, sp->sct_y, player->cnum));
370         return 0;
371     }
372     avail = (SHP_BLD_WORK(mp->m_lcm, mp->m_hcm) * SHIP_MINEFF + 99) / 100;
373     if (sp->sct_avail < avail) {
374         pr("Not enough available work in %s to build a %s\n",
375            xyas(sp->sct_x, sp->sct_y, player->cnum), mp->m_name);
376         pr(" (%d available work required)\n", avail);
377         return 0;
378     }
379     cost = mp->m_cost * SHIP_MINEFF / 100;
380     if (cash < cost) {
381         pr("Not enough money left to build a %s\n", mp->m_name);
382         return 0;
383     }
384     if (!trechk(player->cnum, 0, NEWSHP))
385         return 0;
386     if (!check_sect_ok(sp))
387         return 0;
388     sp->sct_avail -= avail;
389     player->dolcost += cost;
390     cash -= cost;
391     snxtitem_all(&nstr, EF_SHIP);
392     while (nxtitem(&nstr, (s_char *)&ship)) {
393         if (ship.shp_own == 0) {
394             freeship++;
395             break;
396         }
397     }
398     if (freeship == 0) {
399         ef_extend(EF_SHIP, 50);
400     }
401     memset(&ship, 0, sizeof(struct shpstr));
402     ship.shp_x = sp->sct_x;
403     ship.shp_y = sp->sct_y;
404     ship.shp_destx[0] = sp->sct_x;
405     ship.shp_desty[0] = sp->sct_y;
406     ship.shp_destx[1] = sp->sct_x;
407     ship.shp_desty[1] = sp->sct_y;
408     ship.shp_autonav = 0;
409     /* new code for autonav, Chad Zabel 1-15-94 */
410     for (i = 0; i < TMAX; ++i) {
411         ship.shp_tstart[i] = I_NONE;
412         ship.shp_tend[i] = I_NONE;
413         ship.shp_lstart[i] = 0;
414         ship.shp_lend[i] = 0;
415     }
416     ship.shp_mission = 0;
417     ship.shp_own = player->cnum;
418     ship.shp_type = mp - mchr;
419     ship.shp_effic = SHIP_MINEFF;
420     if (opt_MOB_ACCESS) {
421         time(&ship.shp_access);
422         ship.shp_mobil = -(etu_per_update / sect_mob_neg_factor);
423     } else {
424         ship.shp_mobil = 0;
425     }
426     ship.shp_uid = nstr.cur;
427     ship.shp_nplane = 0;
428     ship.shp_nland = 0;
429     ship.shp_nxlight = 0;
430     ship.shp_nchoppers = 0;
431     ship.shp_fleet = ' ';
432     memset(ship.shp_item, 0, sizeof(ship.shp_item));
433     ship.shp_pstage = PLG_HEALTHY;
434     ship.shp_ptime = 0;
435     ship.shp_tech = tlev;
436
437     techdiff = (int)(tlev - mp->m_tech);
438     ship.shp_armor = (short)SHP_DEF(mp->m_armor, techdiff);
439     ship.shp_speed = (short)SHP_SPD(mp->m_speed, techdiff);
440     ship.shp_visib = (short)SHP_VIS(mp->m_visib, techdiff);
441     ship.shp_frnge = (short)SHP_RNG(mp->m_frnge, techdiff);
442     ship.shp_glim = (short)SHP_FIR(mp->m_glim, techdiff);
443
444     ship.shp_mobquota = 0;
445     *ship.shp_path = 0;
446     ship.shp_follow = nstr.cur;
447     ship.shp_name[0] = 0;
448     ship.shp_orig_own = player->cnum;
449     ship.shp_orig_x = sp->sct_x;
450     ship.shp_orig_y = sp->sct_y;
451     ship.shp_fuel = mchr[(int)ship.shp_type].m_fuelc;
452     ship.shp_rflags = 0;
453     memset(ship.shp_rpath, 0, sizeof(ship.shp_rpath));
454
455     vec[I_LCM] -= lcm;
456     vec[I_HCM] -= hcm;
457
458     if (sp->sct_pstage == PLG_INFECT)
459         ship.shp_pstage = PLG_EXPOSED;
460     makenotlost(EF_SHIP, ship.shp_own, ship.shp_uid, ship.shp_x,
461                 ship.shp_y);
462     putship(ship.shp_uid, &ship);
463     pr("%s", prship(&ship));
464     pr(" built in sector %s\n", xyas(sp->sct_x, sp->sct_y, player->cnum));
465     return 1;
466 }
467
468 static int
469 build_land(register struct sctstr *sp, register struct lchrstr *lp,
470            short *vec, int tlev)
471 {
472     struct lndstr land;
473     struct nstr_item nstr;
474     int avail, cost;
475     struct natstr *natp;
476     float eff = LAND_MINEFF / 100.0;
477     int mil, lcm, hcm, gun, shell;
478     int freeland = 0;
479
480 #if 0
481     mil = roundavg(((double)lp->l_mil * eff));
482     shell = roundavg(((double)lp->l_shell * eff));
483     gun = roundavg(((double)lp->l_gun * eff));
484 #else
485     mil = shell = gun = 0;
486 #endif
487     hcm = roundavg(((double)lp->l_hcm * eff));
488     lcm = roundavg(((double)lp->l_lcm * eff));
489
490     natp = getnatp(player->cnum);
491
492     if (sp->sct_type != SCT_HEADQ) {
493         pr("Land Units must be built in headquarters.\n");
494         return 0;
495     }
496     if (sp->sct_effic < 60 && !player->god) {
497         pr("Sector %s is not 60%% efficient.\n",
498            xyas(sp->sct_x, sp->sct_y, player->cnum));
499         return 0;
500     }
501     if (vec[I_LCM] < lcm || vec[I_HCM] < hcm) {
502         pr("Not enough materials in %s\n",
503            xyas(sp->sct_x, sp->sct_y, player->cnum));
504         return 0;
505     }
506 #if 0
507     if (vec[I_GUN] < gun || vec[I_GUN] == 0) {
508         pr("Not enough guns in %s\n",
509            xyas(sp->sct_x, sp->sct_y, player->cnum));
510         return 0;
511     }
512     if (vec[I_SHELL] < shell) {
513         pr("Not enough shells in %s\n",
514            xyas(sp->sct_x, sp->sct_y, player->cnum));
515         return 0;
516     }
517     if (vec[I_MILIT] < mil) {
518         pr("Not enough military in %s\n",
519            xyas(sp->sct_x, sp->sct_y, player->cnum));
520         return 0;
521     }
522 #endif
523     if (!trechk(player->cnum, 0, NEWLND))
524         return 0;
525     if (!check_sect_ok(sp))
526         return 0;
527     avail = (LND_BLD_WORK(lp->l_lcm, lp->l_hcm) * LAND_MINEFF + 99) / 100;
528     if (sp->sct_avail < avail) {
529         pr("Not enough available work in %s to build a %s\n",
530            xyas(sp->sct_x, sp->sct_y, player->cnum), lp->l_name);
531         pr(" (%d available work required)\n", avail);
532         return 0;
533     }
534     cost = lp->l_cost * LAND_MINEFF / 100;
535     if (cash < cost) {
536         pr("Not enough money left to build a %s\n", lp->l_name);
537         return 0;
538     }
539     sp->sct_avail -= avail;
540     player->dolcost += cost;
541     cash -= cost;
542     snxtitem_all(&nstr, EF_LAND);
543     while (nxtitem(&nstr, (s_char *)&land)) {
544         if (land.lnd_own == 0) {
545             freeland++;
546             break;
547         }
548     }
549     if (freeland == 0) {
550         ef_extend(EF_LAND, 50);
551     }
552     memset(&land, 0, sizeof(struct lndstr));
553     land.lnd_x = sp->sct_x;
554     land.lnd_y = sp->sct_y;
555     land.lnd_own = player->cnum;
556     land.lnd_mission = 0;
557     land.lnd_type = lp - lchr;
558     land.lnd_effic = LAND_MINEFF;
559     if (opt_MOB_ACCESS) {
560         time(&land.lnd_access);
561         land.lnd_mobil = -(etu_per_update / sect_mob_neg_factor);
562     } else {
563         land.lnd_mobil = 0;
564     }
565     land.lnd_tech = tlev;
566     land.lnd_uid = nstr.cur;
567     land.lnd_army = ' ';
568     land.lnd_flags = 0;
569     land.lnd_ship = -1;
570     land.lnd_land = -1;
571     land.lnd_nland = 0;
572     land.lnd_harden = 0;
573     land.lnd_retreat = morale_base;
574     land.lnd_fuel = lp->l_fuelc;
575     land.lnd_nxlight = 0;
576     land.lnd_rflags = 0;
577     memset(land.lnd_rpath, 0, sizeof(land.lnd_rpath));
578     land.lnd_rad_max = 0;
579     memset(land.lnd_item, 0, sizeof(land.lnd_item));
580     land.lnd_pstage = PLG_HEALTHY;
581     land.lnd_ptime = 0;
582     land.lnd_att = (float)LND_ATTDEF(lp->l_att, tlev - lp->l_tech);
583     land.lnd_def = (float)LND_ATTDEF(lp->l_def, tlev - lp->l_tech);
584     land.lnd_vul = (int)LND_VUL(lp->l_vul, tlev - lp->l_tech);
585     land.lnd_spd = (int)LND_SPD(lp->l_spd, tlev - lp->l_tech);
586     land.lnd_vis = (int)LND_VIS(lp->l_vis, tlev - lp->l_tech);
587     land.lnd_spy = (int)LND_SPY(lp->l_spy, tlev - lp->l_tech);
588     land.lnd_rad = (int)LND_RAD(lp->l_rad, tlev - lp->l_tech);
589     land.lnd_frg = (int)LND_FRG(lp->l_frg, tlev - lp->l_tech);
590     land.lnd_acc = (int)LND_ACC(lp->l_acc, tlev - lp->l_tech);
591     land.lnd_dam = (int)LND_DAM(lp->l_dam, tlev - lp->l_tech);
592     land.lnd_ammo = (int)LND_AMM(lp->l_ammo, lp->l_dam, tlev - lp->l_tech);
593     land.lnd_aaf = (int)LND_AAF(lp->l_aaf, tlev - lp->l_tech);
594     land.lnd_fuelc = (int)LND_FC(lp->l_fuelc, tlev - lp->l_tech);
595     land.lnd_fuelu = (int)LND_FU(lp->l_fuelu, tlev - lp->l_tech);
596     land.lnd_maxlight = (int)LND_XPL(lp->l_nxlight, tlev - lp->l_tech);
597     land.lnd_maxland = (int)LND_MXL(lp->l_mxland, tlev - lp->l_tech);
598
599     vec[I_LCM] -= lcm;
600     vec[I_HCM] -= hcm;
601     vec[I_MILIT] -= mil;
602     vec[I_GUN] -= gun;
603     vec[I_SHELL] -= shell;
604
605     if (sp->sct_pstage == PLG_INFECT)
606         land.lnd_pstage = PLG_EXPOSED;
607     putland(nstr.cur, &land);
608     makenotlost(EF_LAND, land.lnd_own, land.lnd_uid, land.lnd_x,
609                 land.lnd_y);
610     pr("%s", prland(&land));
611     pr(" built in sector %s\n", xyas(sp->sct_x, sp->sct_y, player->cnum));
612     return 1;
613 }
614
615 static int
616 build_bridge(register struct sctstr *sp, short *vec)
617 {
618     struct sctstr sect;
619     int val;
620     int newx, newy;
621     int avail;
622     int nx, ny, i, good = 0;
623     s_char *p;
624     s_char buf[1024];
625
626     if (opt_EASY_BRIDGES == 0) {        /* must have a bridge head or tower */
627         if (sp->sct_type != SCT_BTOWER) {
628             if (sp->sct_type != SCT_BHEAD)
629                 return 0;
630             if (sp->sct_newtype != SCT_BHEAD)
631                 return 0;
632         }
633     }
634 #if 0
635     else {
636
637         for (i = 1; i <= 6; i++) {
638             struct sctstr s2;
639             nx = sp->sct_x + diroff[i][0];
640             ny = sp->sct_y + diroff[i][1];
641             getsect(nx, ny, &sect);
642             for (j = 1; j <= 6; j++) {
643                 nx2 = sect.sct_x + diroff[j][0];
644                 ny2 = sect.sct_y + diroff[j][1];
645                 getsect(nx2, ny2, &s2);
646                 if ((s2.sct_type != SCT_WATER) &&
647                     (s2.sct_type != SCT_BSPAN))
648                     good = 1;
649             }
650
651         }
652
653         if (!good) {
654             pr("Bridges must be built adjacent to land or bridge towers.\n");
655             pr("No eligible sectors adjacent to this sector.\n");
656             return 0;
657         }
658     }                           /* end EASY_BRIDGES */
659 #endif
660
661     if (sp->sct_effic < 60 && !player->god) {
662         pr("Sector %s is not 60%% efficient.\n",
663            xyas(sp->sct_x, sp->sct_y, player->cnum));
664         return 0;
665     }
666
667     if (!opt_NO_HCMS) {
668         if (vec[I_HCM] < buil_bh) {
669             pr("%s only has %d unit%s of hcm,\n",
670                xyas(sp->sct_x, sp->sct_y, player->cnum),
671                vec[I_HCM], vec[I_HCM] > 1 ? "s" : "");
672             pr("(a bridge span requires %d)\n", buil_bh);
673             return 0;
674         }
675     } else if (!opt_NO_LCMS) {
676         if (vec[I_LCM] < buil_bh) {
677             pr("%s only has %d unit%s of lcm,\n",
678                xyas(sp->sct_x, sp->sct_y, player->cnum),
679                vec[I_LCM], vec[I_LCM] > 1 ? "s" : "");
680             pr("(a bridge span requires %d)\n", buil_bh);
681             return 0;
682         }
683     }
684     if (cash < buil_bc) {
685         pr("A span costs $%.2f to build; ", buil_bc);
686         pr("you only have %d.\n", cash);
687         return 0;
688     }
689     avail = (SCT_BLD_WORK(0, buil_bh) * SCT_MINEFF + 99) / 100;
690     if (sp->sct_avail < avail) {
691         pr("Not enough available work in %s to build a bridge\n",
692            xyas(sp->sct_x, sp->sct_y, player->cnum));
693         pr(" (%d available work required)\n", avail);
694         return 0;
695     }
696     if (!player->argp[3]) {
697         pr("Bridge head at %s\n",
698            xyas(sp->sct_x, sp->sct_y, player->cnum));
699         nav_map(sp->sct_x, sp->sct_y, 1);
700     }
701     if (!(p = getstarg(player->argp[3], "build span in what direction? ", buf))
702         || !*p) {
703         return 0;
704     }
705     /* Sanity check time */
706     if (!check_sect_ok(sp))
707         return 0;
708
709     if ((val = chkdir(*p, DIR_FIRST, DIR_LAST)) < 0) {
710         pr("'%c' is not a valid direction...\n", *p);
711         direrr(0, 0, 0);
712         return 0;
713     }
714     newx = sp->sct_x + diroff[val][0];
715     newy = sp->sct_y + diroff[val][1];
716     if (getsect(newx, newy, &sect) == 0 || sect.sct_type != SCT_WATER) {
717         pr("%s is not a water sector\n", xyas(newx, newy, player->cnum));
718         return 0;
719     }
720     if (opt_EASY_BRIDGES) {
721         good = 0;
722
723         for (i = 1; i <= 6; i++) {
724             struct sctstr s2;
725             nx = sect.sct_x + diroff[i][0];
726             ny = sect.sct_y + diroff[i][1];
727             getsect(nx, ny, &s2);
728             if ((s2.sct_type != SCT_WATER) && (s2.sct_type != SCT_BSPAN))
729                 good = 1;
730         }
731         if (!good) {
732             pr("Bridges must be built adjacent to land or bridge towers.\n");
733             pr("That sector is not adjacent to land or a bridge tower.\n");
734             return 0;
735         }
736     }                           /* end EASY_BRIDGES */
737     sp->sct_avail -= avail;
738     player->dolcost += buil_bc;
739     cash -= buil_bc;
740     sect.sct_type = SCT_BSPAN;
741     sect.sct_newtype = SCT_BSPAN;
742     sect.sct_effic = SCT_MINEFF;
743     sect.sct_road = 0;
744     sect.sct_rail = 0;
745     sect.sct_defense = 0;
746     if (!opt_DEFENSE_INFRA)
747         sect.sct_defense = sect.sct_effic;
748     if (opt_MOB_ACCESS) {
749         time(&sect.sct_access);
750         sect.sct_mobil = -(etu_per_update / sect_mob_neg_factor);
751     } else {
752         sect.sct_mobil = 0;
753     }
754     sect.sct_mines = 0;
755     putsect(&sect);
756     pr("Bridge span built over %s\n",
757        xyas(sect.sct_x, sect.sct_y, player->cnum));
758     if (!opt_NO_HCMS)
759         vec[I_HCM] -= buil_bh;
760     else if (!opt_NO_LCMS)
761         vec[I_LCM] -= buil_bh;
762     return 1;
763 }
764
765 static int
766 build_nuke(register struct sctstr *sp, register struct nchrstr *np,
767            short *vec)
768 {
769     int avail;
770
771     if (sp->sct_type != SCT_NUKE && !player->god) {
772         pr("Nuclear weapons must be built in nuclear plants.\n");
773         return 0;
774     }
775     if (sp->sct_effic < 60 && !player->god) {
776         pr("Sector %s is not 60%% efficient.\n",
777            xyas(sp->sct_x, sp->sct_y, player->cnum));
778         return 0;
779     }
780     if (vec[I_HCM] < np->n_hcm || vec[I_LCM] < np->n_lcm ||
781         vec[I_OIL] < np->n_oil || vec[I_RAD] < np->n_rad) {
782         pr("Not enough materials for a %s bomb in %s\n",
783            np->n_name, xyas(sp->sct_x, sp->sct_y, player->cnum));
784         pr("(%d hcm, %d lcm, %d oil, & %d rads).\n",
785            np->n_hcm, np->n_lcm, np->n_oil, np->n_rad);
786         return 0;
787     }
788     if (cash < np->n_cost) {
789         pr("You need $%d, you only have %d.\n", np->n_cost, cash);
790         return 0;
791     }
792     avail = NUK_BLD_WORK(np->n_lcm, np->n_hcm, np->n_oil, np->n_rad);
793     /*
794      * XXX when nukes turn into units (or whatever), then
795      * make them start at 20%.  Since they don't have efficiency
796      * now, we charge all the work right away.
797      */
798     if (sp->sct_avail < avail) {
799         pr("Not enough available work in %s to build a %s;\n",
800            xyas(sp->sct_x, sp->sct_y, player->cnum), np->n_name);
801         pr(" (%d available work required)\n", avail);
802         return 0;
803     }
804     if (!trechk(player->cnum, 0, NEWNUK))
805         return 0;
806     if (!check_sect_ok(sp))
807         return 0;
808     sp->sct_avail -= avail;
809     player->dolcost += np->n_cost;
810     cash -= np->n_cost;
811     nuk_add(sp->sct_x, sp->sct_y, np - nchr, 1);
812     vec[I_HCM] -= np->n_hcm;
813     vec[I_LCM] -= np->n_lcm;
814     vec[I_OIL] -= np->n_oil;
815     vec[I_RAD] -= np->n_rad;
816     pr("%s warhead created in %s\n", np->n_name,
817        xyas(sp->sct_x, sp->sct_y, player->cnum));
818     return 1;
819 }
820
821 static int
822 build_plane(register struct sctstr *sp, register struct plchrstr *pp,
823             short *vec, int tlev)
824 {
825     struct plnstr plane;
826     int avail, cost;
827     struct nstr_item nstr;
828     float eff = PLANE_MINEFF / 100.0;
829     int hcm, lcm, mil;
830     int freeplane = 0;
831
832     mil = roundavg(((double)pp->pl_crew * eff));
833     /* Always use at least 1 mil to build a plane */
834     if (mil == 0 && pp->pl_crew > 0)
835         mil = 1;
836     hcm = roundavg(((double)pp->pl_hcm * eff));
837     lcm = roundavg(((double)pp->pl_lcm * eff));
838     if (sp->sct_type != SCT_AIRPT && !player->god) {
839         pr("Planes must be built in airports.\n");
840         return 0;
841     }
842     if (sp->sct_effic < 60 && !player->god) {
843         pr("Sector %s is not 60%% efficient.\n",
844            xyas(sp->sct_x, sp->sct_y, player->cnum));
845         return 0;
846     }
847     if (vec[I_LCM] < lcm || vec[I_HCM] < hcm) {
848         pr("Not enough materials in %s\n",
849            xyas(sp->sct_x, sp->sct_y, player->cnum));
850         return 0;
851     }
852     avail = (PLN_BLD_WORK(pp->pl_lcm, pp->pl_hcm) * PLANE_MINEFF + 99) / 100;
853     if (sp->sct_avail < avail) {
854         pr("Not enough available work in %s to build a %s\n",
855            xyas(sp->sct_x, sp->sct_y, player->cnum), pp->pl_name);
856         pr(" (%d available work required)\n", avail);
857         return 0;
858     }
859     cost = pp->pl_cost * PLANE_MINEFF / 100;
860     if (cash < cost) {
861         pr("Not enough money left to build a %s\n", pp->pl_name);
862         return 0;
863     }
864     if (vec[I_MILIT] < mil || (vec[I_MILIT] == 0 && pp->pl_crew > 0)) {
865         pr("Not enough military for crew in %s\n",
866            xyas(sp->sct_x, sp->sct_y, player->cnum));
867         return 0;
868     }
869     if (!trechk(player->cnum, 0, NEWPLN))
870         return 0;
871     if (!check_sect_ok(sp))
872         return 0;
873     sp->sct_avail -= avail;
874     player->dolcost += cost;
875     cash -= cost;
876     snxtitem_all(&nstr, EF_PLANE);
877     freeplane = 0;
878     while (nxtitem(&nstr, (s_char *)&plane)) {
879         if (plane.pln_own == 0) {
880             freeplane++;
881             break;
882         }
883     }
884     if (freeplane == 0) {
885         ef_extend(EF_PLANE, 50);
886     }
887     memset(&plane, 0, sizeof(struct plnstr));
888     plane.pln_x = sp->sct_x;
889     plane.pln_y = sp->sct_y;
890     plane.pln_own = sp->sct_own;
891     plane.pln_type = pp - plchr;
892     plane.pln_effic = PLANE_MINEFF;
893     if (opt_MOB_ACCESS) {
894         time(&plane.pln_access);
895         plane.pln_mobil = -(etu_per_update / sect_mob_neg_factor);
896     } else {
897         plane.pln_mobil = 0;
898     }
899     plane.pln_mission = 0;
900     plane.pln_opx = 0;
901     plane.pln_opy = 0;
902     plane.pln_radius = 0;
903
904     /* Note that this next block of variables can be changed so that individual
905      * planes may have their own stats (like based on tech maybe? :) )  Thus,
906      * the code now checks the pln_acc, pln_load and pln_fuel instead of using
907      * the static definitions of them. */
908     plane.pln_att = PLN_ATTDEF(pp->pl_att, (int)(tlev - pp->pl_tech));
909     plane.pln_def = PLN_ATTDEF(pp->pl_def, (int)(tlev - pp->pl_tech));
910     plane.pln_acc = PLN_ACC(pp->pl_acc, (int)(tlev - pp->pl_tech));
911     plane.pln_range = PLN_RAN(pp->pl_range, (int)(tlev - pp->pl_tech));
912     plane.pln_range_max = plane.pln_range;
913     plane.pln_load = PLN_LOAD(pp->pl_load, (int)(tlev - pp->pl_tech));
914     plane.pln_fuel = pp->pl_fuel;
915
916     plane.pln_wing = ' ';
917     plane.pln_tech = tlev;
918     plane.pln_ship = -1;
919     plane.pln_land = -1;
920     plane.pln_uid = nstr.cur;
921     plane.pln_nuketype = -1;
922     plane.pln_harden = 0;
923     plane.pln_flags = 0;
924     makenotlost(EF_PLANE, plane.pln_own, plane.pln_uid, plane.pln_x,
925                 plane.pln_y);
926     putplane(plane.pln_uid, &plane);
927     pr("%s built in sector %s\n", prplane(&plane),
928        xyas(sp->sct_x, sp->sct_y, player->cnum));
929     vec[I_LCM] -= lcm;
930     vec[I_HCM] -= hcm;
931     vec[I_MILIT] -= mil;
932     return 1;
933 }
934
935 static int
936 build_tower(register struct sctstr *sp, short *vec)
937 {
938     struct sctstr sect;
939     int val;
940     int newx, newy;
941     int avail;
942     s_char *p;
943     s_char buf[1024];
944     int good;
945     int i;
946     int nx;
947     int ny;
948
949     if (sp->sct_type != SCT_BSPAN) {
950         pr("Bridge towers can only be built from bridge spans.\n");
951         return 0;
952     }
953
954     if (sp->sct_effic < 60 && !player->god) {
955         pr("Sector %s is not 60%% efficient.\n",
956            xyas(sp->sct_x, sp->sct_y, player->cnum));
957         return 0;
958     }
959
960     if (!opt_NO_HCMS) {
961         if (vec[I_HCM] < buil_tower_bh) {
962             pr("%s only has %d unit%s of hcm,\n",
963                xyas(sp->sct_x, sp->sct_y, player->cnum),
964                vec[I_HCM], vec[I_HCM] > 1 ? "s" : "");
965             pr("(a bridge tower requires %d)\n", buil_tower_bh);
966             return 0;
967         }
968     } else if (!opt_NO_LCMS) {
969         if (vec[I_LCM] < buil_tower_bh) {
970             pr("%s only has %d unit%s of lcm,\n",
971                xyas(sp->sct_x, sp->sct_y, player->cnum),
972                vec[I_LCM], vec[I_LCM] > 1 ? "s" : "");
973             pr("(a bridge tower requires %d)\n", buil_tower_bh);
974             return 0;
975         }
976     }
977     if (cash < buil_tower_bc) {
978         pr("A bridge tower costs $%.2f to build; ", buil_tower_bc);
979         pr("you only have %d.\n", cash);
980         return 0;
981     }
982     avail = (SCT_BLD_WORK(0, buil_tower_bh) * SCT_MINEFF + 99) / 100;
983     if (sp->sct_avail < avail) {
984         pr("Not enough available work in %s to build a bridge tower\n",
985            xyas(sp->sct_x, sp->sct_y, player->cnum));
986         pr(" (%d available work required)\n", avail);
987         return 0;
988     }
989     if (!player->argp[3]) {
990         pr("Building from %s\n", xyas(sp->sct_x, sp->sct_y, player->cnum));
991         nav_map(sp->sct_x, sp->sct_y, 1);
992     }
993     if (!(p = getstarg(player->argp[3], "build tower in what direction? ", buf))
994         || !*p) {
995         return 0;
996     }
997     /* Sanity check time */
998     if (!check_sect_ok(sp))
999         return 0;
1000
1001     if ((val = chkdir(*p, DIR_FIRST, DIR_LAST)) < 0) {
1002         pr("'%c' is not a valid direction...\n", *p);
1003         direrr(0, 0, 0);
1004         return 0;
1005     }
1006     newx = sp->sct_x + diroff[val][0];
1007     newy = sp->sct_y + diroff[val][1];
1008     if (getsect(newx, newy, &sect) == 0 || sect.sct_type != SCT_WATER) {
1009         pr("%s is not a water sector\n", xyas(newx, newy, player->cnum));
1010         return 0;
1011     }
1012
1013     /* Now, check.  You aren't allowed to build bridge towers
1014        next to land. */
1015     good = 0;
1016     for (i = 1; i <= 6; i++) {
1017         struct sctstr s2;
1018         nx = sect.sct_x + diroff[i][0];
1019         ny = sect.sct_y + diroff[i][1];
1020         getsect(nx, ny, &s2);
1021         if ((s2.sct_type != SCT_WATER) &&
1022             (s2.sct_type != SCT_BTOWER) && (s2.sct_type != SCT_BSPAN)) {
1023             good = 1;
1024             break;
1025         }
1026     }
1027     if (good) {
1028         pr("Bridge towers cannot be built adjacent to land.\n");
1029         pr("That sector is adjacent to land.\n");
1030         return 0;
1031     }
1032
1033     sp->sct_avail -= avail;
1034     player->dolcost += buil_tower_bc;
1035     cash -= buil_tower_bc;
1036     sect.sct_type = SCT_BTOWER;
1037     sect.sct_newtype = SCT_BTOWER;
1038     sect.sct_effic = SCT_MINEFF;
1039     sect.sct_road = 0;
1040     sect.sct_rail = 0;
1041     sect.sct_defense = 0;
1042     if (opt_MOB_ACCESS) {
1043         time(&sect.sct_access);
1044         sect.sct_mobil = -(etu_per_update / sect_mob_neg_factor);
1045     } else {
1046         sect.sct_mobil = 0;
1047     }
1048     if (!opt_DEFENSE_INFRA)
1049         sect.sct_defense = sect.sct_effic;
1050     sect.sct_mines = 0;
1051     putsect(&sect);
1052     pr("Bridge tower built in %s\n",
1053        xyas(sect.sct_x, sect.sct_y, player->cnum));
1054     if (!opt_NO_HCMS)
1055         vec[I_HCM] -= buil_tower_bh;
1056     else if (!opt_NO_LCMS)
1057         vec[I_LCM] -= buil_tower_bh;
1058     return 1;
1059 }