]> git.pond.sub.org Git - empserver/blob - src/lib/commands/buil.c
(build_bridge,build_tower,retreat_ship1): Mark "X" on sector
[empserver] / src / lib / commands / buil.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2005, 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 <limits.h>
35 #include <string.h>
36 #include "misc.h"
37 #include "player.h"
38 #include "plague.h"
39 #include "sect.h"
40 #include "nat.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(struct sctstr *sp,
54                       struct nchrstr *np, short *vec);
55 static int build_ship(struct sctstr *sp,
56                       struct mchrstr *mp, short *vec,
57                       int tlev);
58 static int build_land(struct sctstr *sp,
59                       struct lchrstr *lp, short *vec,
60                       int tlev);
61 static int build_bridge(struct sctstr *sp, short *vec);
62 static int build_tower(struct sctstr *sp, short *vec);
63 static int build_plane(struct sctstr *sp,
64                        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(struct sctstr *sp, 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(struct sctstr *sp, struct lchrstr *lp,
460            short *vec, int tlev)
461 {
462     struct lndstr land;
463     struct nstr_item nstr;
464     int avail, cost;
465     float eff = LAND_MINEFF / 100.0;
466     int mil, lcm, hcm, gun, shell;
467     int freeland = 0;
468
469 #if 0
470     mil = roundavg(((double)lp->l_mil * eff));
471     shell = roundavg(((double)lp->l_shell * eff));
472     gun = roundavg(((double)lp->l_gun * eff));
473 #else
474     mil = shell = gun = 0;
475 #endif
476     hcm = roundavg(((double)lp->l_hcm * eff));
477     lcm = roundavg(((double)lp->l_lcm * eff));
478
479     if (sp->sct_type != SCT_HEADQ) {
480         pr("Land Units must be built in headquarters.\n");
481         return 0;
482     }
483     if (sp->sct_effic < 60 && !player->god) {
484         pr("Sector %s is not 60%% efficient.\n",
485            xyas(sp->sct_x, sp->sct_y, player->cnum));
486         return 0;
487     }
488     if (vec[I_LCM] < lcm || vec[I_HCM] < hcm) {
489         pr("Not enough materials in %s\n",
490            xyas(sp->sct_x, sp->sct_y, player->cnum));
491         return 0;
492     }
493 #if 0
494     if (vec[I_GUN] < gun || vec[I_GUN] == 0) {
495         pr("Not enough guns in %s\n",
496            xyas(sp->sct_x, sp->sct_y, player->cnum));
497         return 0;
498     }
499     if (vec[I_SHELL] < shell) {
500         pr("Not enough shells in %s\n",
501            xyas(sp->sct_x, sp->sct_y, player->cnum));
502         return 0;
503     }
504     if (vec[I_MILIT] < mil) {
505         pr("Not enough military in %s\n",
506            xyas(sp->sct_x, sp->sct_y, player->cnum));
507         return 0;
508     }
509 #endif
510     if (!trechk(player->cnum, 0, NEWLND))
511         return 0;
512     if (!check_sect_ok(sp))
513         return 0;
514     avail = (LND_BLD_WORK(lp->l_lcm, lp->l_hcm) * LAND_MINEFF + 99) / 100;
515     if (sp->sct_avail < avail) {
516         pr("Not enough available work in %s to build a %s\n",
517            xyas(sp->sct_x, sp->sct_y, player->cnum), lp->l_name);
518         pr(" (%d available work required)\n", avail);
519         return 0;
520     }
521     cost = lp->l_cost * LAND_MINEFF / 100;
522     if (cash < cost) {
523         pr("Not enough money left to build a %s\n", lp->l_name);
524         return 0;
525     }
526     sp->sct_avail -= avail;
527     player->dolcost += cost;
528     cash -= cost;
529     snxtitem_all(&nstr, EF_LAND);
530     while (nxtitem(&nstr, (s_char *)&land)) {
531         if (land.lnd_own == 0) {
532             freeland++;
533             break;
534         }
535     }
536     if (freeland == 0) {
537         ef_extend(EF_LAND, 50);
538     }
539     memset(&land, 0, sizeof(struct lndstr));
540     land.lnd_x = sp->sct_x;
541     land.lnd_y = sp->sct_y;
542     land.lnd_own = player->cnum;
543     land.lnd_mission = 0;
544     land.lnd_type = lp - lchr;
545     land.lnd_effic = LAND_MINEFF;
546     if (opt_MOB_ACCESS) {
547         time(&land.lnd_access);
548         land.lnd_mobil = -(etu_per_update / sect_mob_neg_factor);
549     } else {
550         land.lnd_mobil = 0;
551     }
552     land.lnd_uid = nstr.cur;
553     land.lnd_army = ' ';
554     land.lnd_flags = 0;
555     land.lnd_ship = -1;
556     land.lnd_land = -1;
557     land.lnd_nland = 0;
558     land.lnd_harden = 0;
559     land.lnd_retreat = morale_base;
560     land.lnd_fuel = lp->l_fuelc;
561     land.lnd_nxlight = 0;
562     land.lnd_rflags = 0;
563     memset(land.lnd_rpath, 0, sizeof(land.lnd_rpath));
564     land.lnd_rad_max = 0;
565     memset(land.lnd_item, 0, sizeof(land.lnd_item));
566     land.lnd_pstage = PLG_HEALTHY;
567     land.lnd_ptime = 0;
568     lnd_set_tech(&land, tlev);
569
570     vec[I_LCM] -= lcm;
571     vec[I_HCM] -= hcm;
572     vec[I_MILIT] -= mil;
573     vec[I_GUN] -= gun;
574     vec[I_SHELL] -= shell;
575
576     if (sp->sct_pstage == PLG_INFECT)
577         land.lnd_pstage = PLG_EXPOSED;
578     putland(nstr.cur, &land);
579     makenotlost(EF_LAND, land.lnd_own, land.lnd_uid, land.lnd_x,
580                 land.lnd_y);
581     pr("%s", prland(&land));
582     pr(" built in sector %s\n", xyas(sp->sct_x, sp->sct_y, player->cnum));
583     return 1;
584 }
585
586 static int
587 build_bridge(struct sctstr *sp, short *vec)
588 {
589     struct sctstr sect;
590     int val;
591     int newx, newy;
592     int avail;
593     int nx, ny, i, good = 0;
594     s_char *p;
595     s_char buf[1024];
596
597     if (opt_EASY_BRIDGES == 0) {        /* must have a bridge head or tower */
598         if (sp->sct_type != SCT_BTOWER) {
599             if (sp->sct_type != SCT_BHEAD)
600                 return 0;
601             if (sp->sct_newtype != SCT_BHEAD)
602                 return 0;
603         }
604     }
605 #if 0
606     else {
607
608         for (i = 1; i <= 6; i++) {
609             struct sctstr s2;
610             nx = sp->sct_x + diroff[i][0];
611             ny = sp->sct_y + diroff[i][1];
612             getsect(nx, ny, &sect);
613             for (j = 1; j <= 6; j++) {
614                 nx2 = sect.sct_x + diroff[j][0];
615                 ny2 = sect.sct_y + diroff[j][1];
616                 getsect(nx2, ny2, &s2);
617                 if ((s2.sct_type != SCT_WATER) &&
618                     (s2.sct_type != SCT_BSPAN))
619                     good = 1;
620             }
621
622         }
623
624         if (!good) {
625             pr("Bridges must be built adjacent to land or bridge towers.\n");
626             pr("No eligible sectors adjacent to this sector.\n");
627             return 0;
628         }
629     }                           /* end EASY_BRIDGES */
630 #endif
631
632     if (sp->sct_effic < 60 && !player->god) {
633         pr("Sector %s is not 60%% efficient.\n",
634            xyas(sp->sct_x, sp->sct_y, player->cnum));
635         return 0;
636     }
637
638     if (!opt_NO_HCMS) {
639         if (vec[I_HCM] < buil_bh) {
640             pr("%s only has %d unit%s of hcm,\n",
641                xyas(sp->sct_x, sp->sct_y, player->cnum),
642                vec[I_HCM], vec[I_HCM] > 1 ? "s" : "");
643             pr("(a bridge span requires %d)\n", buil_bh);
644             return 0;
645         }
646     } else if (!opt_NO_LCMS) {
647         if (vec[I_LCM] < buil_bh) {
648             pr("%s only has %d unit%s of lcm,\n",
649                xyas(sp->sct_x, sp->sct_y, player->cnum),
650                vec[I_LCM], vec[I_LCM] > 1 ? "s" : "");
651             pr("(a bridge span requires %d)\n", buil_bh);
652             return 0;
653         }
654     }
655     if (cash < buil_bc) {
656         pr("A span costs $%.2f to build; ", buil_bc);
657         pr("you only have %d.\n", cash);
658         return 0;
659     }
660     avail = (SCT_BLD_WORK(0, buil_bh) * SCT_MINEFF + 99) / 100;
661     if (sp->sct_avail < avail) {
662         pr("Not enough available work in %s to build a bridge\n",
663            xyas(sp->sct_x, sp->sct_y, player->cnum));
664         pr(" (%d available work required)\n", avail);
665         return 0;
666     }
667     if (!player->argp[3]) {
668         pr("Bridge head at %s\n",
669            xyas(sp->sct_x, sp->sct_y, player->cnum));
670         nav_map(sp->sct_x, sp->sct_y, 1);
671     }
672     if (!(p = getstarg(player->argp[3], "build span in what direction? ", buf))
673         || !*p) {
674         return 0;
675     }
676     /* Sanity check time */
677     if (!check_sect_ok(sp))
678         return 0;
679
680     if ((val = chkdir(*p, DIR_FIRST, DIR_LAST)) < 0) {
681         pr("'%c' is not a valid direction...\n", *p);
682         direrr(0, 0, 0);
683         return 0;
684     }
685     newx = sp->sct_x + diroff[val][0];
686     newy = sp->sct_y + diroff[val][1];
687     if (getsect(newx, newy, &sect) == 0 || sect.sct_type != SCT_WATER) {
688         pr("%s is not a water sector\n", xyas(newx, newy, player->cnum));
689         return 0;
690     }
691     if (opt_EASY_BRIDGES) {
692         good = 0;
693
694         for (i = 1; i <= 6; i++) {
695             struct sctstr s2;
696             nx = sect.sct_x + diroff[i][0];
697             ny = sect.sct_y + diroff[i][1];
698             getsect(nx, ny, &s2);
699             if ((s2.sct_type != SCT_WATER) && (s2.sct_type != SCT_BSPAN))
700                 good = 1;
701         }
702         if (!good) {
703             pr("Bridges must be built adjacent to land or bridge towers.\n");
704             pr("That sector is not adjacent to land or a bridge tower.\n");
705             return 0;
706         }
707     }                           /* end EASY_BRIDGES */
708     sp->sct_avail -= avail;
709     player->dolcost += buil_bc;
710     cash -= buil_bc;
711     sect.sct_type = SCT_BSPAN;
712     sect.sct_newtype = SCT_BSPAN;
713     sect.sct_effic = SCT_MINEFF;
714     sect.sct_road = 0;
715     sect.sct_rail = 0;
716     sect.sct_defense = 0;
717     if (!opt_DEFENSE_INFRA)
718         sect.sct_defense = sect.sct_effic;
719     if (opt_MOB_ACCESS) {
720         time(&sect.sct_access);
721         sect.sct_mobil = -(etu_per_update / sect_mob_neg_factor);
722     } else {
723         sect.sct_mobil = 0;
724     }
725     sect.sct_mines = 0;
726     map_set(player->cnum, sect.sct_x, sect.sct_y, dchr[SCT_BSPAN].d_mnem, 2);
727     writemap(player->cnum);
728     putsect(&sect);
729     pr("Bridge span built over %s\n",
730        xyas(sect.sct_x, sect.sct_y, player->cnum));
731     if (!opt_NO_HCMS)
732         vec[I_HCM] -= buil_bh;
733     else if (!opt_NO_LCMS)
734         vec[I_LCM] -= buil_bh;
735     return 1;
736 }
737
738 static int
739 build_nuke(struct sctstr *sp, struct nchrstr *np,
740            short *vec)
741 {
742     int avail;
743
744     if (sp->sct_type != SCT_NUKE && !player->god) {
745         pr("Nuclear weapons must be built in nuclear plants.\n");
746         return 0;
747     }
748     if (sp->sct_effic < 60 && !player->god) {
749         pr("Sector %s is not 60%% efficient.\n",
750            xyas(sp->sct_x, sp->sct_y, player->cnum));
751         return 0;
752     }
753     if (vec[I_HCM] < np->n_hcm || vec[I_LCM] < np->n_lcm ||
754         vec[I_OIL] < np->n_oil || vec[I_RAD] < np->n_rad) {
755         pr("Not enough materials for a %s bomb in %s\n",
756            np->n_name, xyas(sp->sct_x, sp->sct_y, player->cnum));
757         pr("(%d hcm, %d lcm, %d oil, & %d rads).\n",
758            np->n_hcm, np->n_lcm, np->n_oil, np->n_rad);
759         return 0;
760     }
761     if (cash < np->n_cost) {
762         pr("You need $%d, you only have %d.\n", np->n_cost, cash);
763         return 0;
764     }
765     avail = NUK_BLD_WORK(np->n_lcm, np->n_hcm, np->n_oil, np->n_rad);
766     /*
767      * XXX when nukes turn into units (or whatever), then
768      * make them start at 20%.  Since they don't have efficiency
769      * now, we charge all the work right away.
770      */
771     if (sp->sct_avail < avail) {
772         pr("Not enough available work in %s to build a %s;\n",
773            xyas(sp->sct_x, sp->sct_y, player->cnum), np->n_name);
774         pr(" (%d available work required)\n", avail);
775         return 0;
776     }
777     if (!trechk(player->cnum, 0, NEWNUK))
778         return 0;
779     if (!check_sect_ok(sp))
780         return 0;
781     sp->sct_avail -= avail;
782     player->dolcost += np->n_cost;
783     cash -= np->n_cost;
784     nuk_add(sp->sct_x, sp->sct_y, np - nchr, 1);
785     vec[I_HCM] -= np->n_hcm;
786     vec[I_LCM] -= np->n_lcm;
787     vec[I_OIL] -= np->n_oil;
788     vec[I_RAD] -= np->n_rad;
789     pr("%s warhead created in %s\n", np->n_name,
790        xyas(sp->sct_x, sp->sct_y, player->cnum));
791     return 1;
792 }
793
794 static int
795 build_plane(struct sctstr *sp, struct plchrstr *pp,
796             short *vec, int tlev)
797 {
798     struct plnstr plane;
799     int avail, cost;
800     struct nstr_item nstr;
801     float eff = PLANE_MINEFF / 100.0;
802     int hcm, lcm, mil;
803     int freeplane = 0;
804
805     mil = roundavg(((double)pp->pl_crew * eff));
806     /* Always use at least 1 mil to build a plane */
807     if (mil == 0 && pp->pl_crew > 0)
808         mil = 1;
809     hcm = roundavg(((double)pp->pl_hcm * eff));
810     lcm = roundavg(((double)pp->pl_lcm * eff));
811     if (sp->sct_type != SCT_AIRPT && !player->god) {
812         pr("Planes must be built in airports.\n");
813         return 0;
814     }
815     if (sp->sct_effic < 60 && !player->god) {
816         pr("Sector %s is not 60%% efficient.\n",
817            xyas(sp->sct_x, sp->sct_y, player->cnum));
818         return 0;
819     }
820     if (vec[I_LCM] < lcm || vec[I_HCM] < hcm) {
821         pr("Not enough materials in %s\n",
822            xyas(sp->sct_x, sp->sct_y, player->cnum));
823         return 0;
824     }
825     avail = (PLN_BLD_WORK(pp->pl_lcm, pp->pl_hcm) * PLANE_MINEFF + 99) / 100;
826     if (sp->sct_avail < avail) {
827         pr("Not enough available work in %s to build a %s\n",
828            xyas(sp->sct_x, sp->sct_y, player->cnum), pp->pl_name);
829         pr(" (%d available work required)\n", avail);
830         return 0;
831     }
832     cost = pp->pl_cost * PLANE_MINEFF / 100;
833     if (cash < cost) {
834         pr("Not enough money left to build a %s\n", pp->pl_name);
835         return 0;
836     }
837     if (vec[I_MILIT] < mil || (vec[I_MILIT] == 0 && pp->pl_crew > 0)) {
838         pr("Not enough military for crew in %s\n",
839            xyas(sp->sct_x, sp->sct_y, player->cnum));
840         return 0;
841     }
842     if (!trechk(player->cnum, 0, NEWPLN))
843         return 0;
844     if (!check_sect_ok(sp))
845         return 0;
846     sp->sct_avail -= avail;
847     player->dolcost += cost;
848     cash -= cost;
849     snxtitem_all(&nstr, EF_PLANE);
850     freeplane = 0;
851     while (nxtitem(&nstr, (s_char *)&plane)) {
852         if (plane.pln_own == 0) {
853             freeplane++;
854             break;
855         }
856     }
857     if (freeplane == 0) {
858         ef_extend(EF_PLANE, 50);
859     }
860     memset(&plane, 0, sizeof(struct plnstr));
861     plane.pln_x = sp->sct_x;
862     plane.pln_y = sp->sct_y;
863     plane.pln_own = sp->sct_own;
864     plane.pln_type = pp - plchr;
865     plane.pln_effic = PLANE_MINEFF;
866     if (opt_MOB_ACCESS) {
867         time(&plane.pln_access);
868         plane.pln_mobil = -(etu_per_update / sect_mob_neg_factor);
869     } else {
870         plane.pln_mobil = 0;
871     }
872     plane.pln_mission = 0;
873     plane.pln_opx = 0;
874     plane.pln_opy = 0;
875     plane.pln_radius = 0;
876     plane.pln_range = UCHAR_MAX; /* will be adjusted by pln_set_tech() */
877     plane.pln_range_max = plane.pln_range;
878     plane.pln_wing = ' ';
879     plane.pln_ship = -1;
880     plane.pln_land = -1;
881     plane.pln_uid = nstr.cur;
882     plane.pln_nuketype = -1;
883     plane.pln_harden = 0;
884     plane.pln_flags = 0;
885     pln_set_tech(&plane, tlev);
886
887     vec[I_LCM] -= lcm;
888     vec[I_HCM] -= hcm;
889     vec[I_MILIT] -= mil;
890
891     makenotlost(EF_PLANE, plane.pln_own, plane.pln_uid, plane.pln_x,
892                 plane.pln_y);
893     putplane(plane.pln_uid, &plane);
894     pr("%s built in sector %s\n", prplane(&plane),
895        xyas(sp->sct_x, sp->sct_y, player->cnum));
896     return 1;
897 }
898
899 static int
900 build_tower(struct sctstr *sp, short *vec)
901 {
902     struct sctstr sect;
903     int val;
904     int newx, newy;
905     int avail;
906     s_char *p;
907     s_char buf[1024];
908     int good;
909     int i;
910     int nx;
911     int ny;
912
913     if (sp->sct_type != SCT_BSPAN) {
914         pr("Bridge towers can only be built from bridge spans.\n");
915         return 0;
916     }
917
918     if (sp->sct_effic < 60 && !player->god) {
919         pr("Sector %s is not 60%% efficient.\n",
920            xyas(sp->sct_x, sp->sct_y, player->cnum));
921         return 0;
922     }
923
924     if (!opt_NO_HCMS) {
925         if (vec[I_HCM] < buil_tower_bh) {
926             pr("%s only has %d unit%s of hcm,\n",
927                xyas(sp->sct_x, sp->sct_y, player->cnum),
928                vec[I_HCM], vec[I_HCM] > 1 ? "s" : "");
929             pr("(a bridge tower requires %d)\n", buil_tower_bh);
930             return 0;
931         }
932     } else if (!opt_NO_LCMS) {
933         if (vec[I_LCM] < buil_tower_bh) {
934             pr("%s only has %d unit%s of lcm,\n",
935                xyas(sp->sct_x, sp->sct_y, player->cnum),
936                vec[I_LCM], vec[I_LCM] > 1 ? "s" : "");
937             pr("(a bridge tower requires %d)\n", buil_tower_bh);
938             return 0;
939         }
940     }
941     if (cash < buil_tower_bc) {
942         pr("A bridge tower costs $%.2f to build; ", buil_tower_bc);
943         pr("you only have %d.\n", cash);
944         return 0;
945     }
946     avail = (SCT_BLD_WORK(0, buil_tower_bh) * SCT_MINEFF + 99) / 100;
947     if (sp->sct_avail < avail) {
948         pr("Not enough available work in %s to build a bridge tower\n",
949            xyas(sp->sct_x, sp->sct_y, player->cnum));
950         pr(" (%d available work required)\n", avail);
951         return 0;
952     }
953     if (!player->argp[3]) {
954         pr("Building from %s\n", xyas(sp->sct_x, sp->sct_y, player->cnum));
955         nav_map(sp->sct_x, sp->sct_y, 1);
956     }
957     if (!(p = getstarg(player->argp[3], "build tower in what direction? ", buf))
958         || !*p) {
959         return 0;
960     }
961     /* Sanity check time */
962     if (!check_sect_ok(sp))
963         return 0;
964
965     if ((val = chkdir(*p, DIR_FIRST, DIR_LAST)) < 0) {
966         pr("'%c' is not a valid direction...\n", *p);
967         direrr(0, 0, 0);
968         return 0;
969     }
970     newx = sp->sct_x + diroff[val][0];
971     newy = sp->sct_y + diroff[val][1];
972     if (getsect(newx, newy, &sect) == 0 || sect.sct_type != SCT_WATER) {
973         pr("%s is not a water sector\n", xyas(newx, newy, player->cnum));
974         return 0;
975     }
976
977     /* Now, check.  You aren't allowed to build bridge towers
978        next to land. */
979     good = 0;
980     for (i = 1; i <= 6; i++) {
981         struct sctstr s2;
982         nx = sect.sct_x + diroff[i][0];
983         ny = sect.sct_y + diroff[i][1];
984         getsect(nx, ny, &s2);
985         if ((s2.sct_type != SCT_WATER) &&
986             (s2.sct_type != SCT_BTOWER) && (s2.sct_type != SCT_BSPAN)) {
987             good = 1;
988             break;
989         }
990     }
991     if (good) {
992         pr("Bridge towers cannot be built adjacent to land.\n");
993         pr("That sector is adjacent to land.\n");
994         return 0;
995     }
996
997     sp->sct_avail -= avail;
998     player->dolcost += buil_tower_bc;
999     cash -= buil_tower_bc;
1000     sect.sct_type = SCT_BTOWER;
1001     sect.sct_newtype = SCT_BTOWER;
1002     sect.sct_effic = SCT_MINEFF;
1003     sect.sct_road = 0;
1004     sect.sct_rail = 0;
1005     sect.sct_defense = 0;
1006     if (opt_MOB_ACCESS) {
1007         time(&sect.sct_access);
1008         sect.sct_mobil = -(etu_per_update / sect_mob_neg_factor);
1009     } else {
1010         sect.sct_mobil = 0;
1011     }
1012     if (!opt_DEFENSE_INFRA)
1013         sect.sct_defense = sect.sct_effic;
1014     sect.sct_mines = 0;
1015     map_set(player->cnum, sect.sct_x, sect.sct_y, dchr[SCT_BTOWER].d_mnem, 2);
1016     writemap(player->cnum);
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 }