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