]> git.pond.sub.org Git - empserver/blob - src/lib/commands/buil.c
Update copyright notice.
[empserver] / src / lib / commands / buil.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2004, 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 "var.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(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     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(register 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     putsect(&sect);
727     pr("Bridge span built over %s\n",
728        xyas(sect.sct_x, sect.sct_y, player->cnum));
729     if (!opt_NO_HCMS)
730         vec[I_HCM] -= buil_bh;
731     else if (!opt_NO_LCMS)
732         vec[I_LCM] -= buil_bh;
733     return 1;
734 }
735
736 static int
737 build_nuke(register struct sctstr *sp, register struct nchrstr *np,
738            short *vec)
739 {
740     int avail;
741
742     if (sp->sct_type != SCT_NUKE && !player->god) {
743         pr("Nuclear weapons must be built in nuclear plants.\n");
744         return 0;
745     }
746     if (sp->sct_effic < 60 && !player->god) {
747         pr("Sector %s is not 60%% efficient.\n",
748            xyas(sp->sct_x, sp->sct_y, player->cnum));
749         return 0;
750     }
751     if (vec[I_HCM] < np->n_hcm || vec[I_LCM] < np->n_lcm ||
752         vec[I_OIL] < np->n_oil || vec[I_RAD] < np->n_rad) {
753         pr("Not enough materials for a %s bomb in %s\n",
754            np->n_name, xyas(sp->sct_x, sp->sct_y, player->cnum));
755         pr("(%d hcm, %d lcm, %d oil, & %d rads).\n",
756            np->n_hcm, np->n_lcm, np->n_oil, np->n_rad);
757         return 0;
758     }
759     if (cash < np->n_cost) {
760         pr("You need $%d, you only have %d.\n", np->n_cost, cash);
761         return 0;
762     }
763     avail = NUK_BLD_WORK(np->n_lcm, np->n_hcm, np->n_oil, np->n_rad);
764     /*
765      * XXX when nukes turn into units (or whatever), then
766      * make them start at 20%.  Since they don't have efficiency
767      * now, we charge all the work right away.
768      */
769     if (sp->sct_avail < avail) {
770         pr("Not enough available work in %s to build a %s;\n",
771            xyas(sp->sct_x, sp->sct_y, player->cnum), np->n_name);
772         pr(" (%d available work required)\n", avail);
773         return 0;
774     }
775     if (!trechk(player->cnum, 0, NEWNUK))
776         return 0;
777     if (!check_sect_ok(sp))
778         return 0;
779     sp->sct_avail -= avail;
780     player->dolcost += np->n_cost;
781     cash -= np->n_cost;
782     nuk_add(sp->sct_x, sp->sct_y, np - nchr, 1);
783     vec[I_HCM] -= np->n_hcm;
784     vec[I_LCM] -= np->n_lcm;
785     vec[I_OIL] -= np->n_oil;
786     vec[I_RAD] -= np->n_rad;
787     pr("%s warhead created in %s\n", np->n_name,
788        xyas(sp->sct_x, sp->sct_y, player->cnum));
789     return 1;
790 }
791
792 static int
793 build_plane(register struct sctstr *sp, register struct plchrstr *pp,
794             short *vec, int tlev)
795 {
796     struct plnstr plane;
797     int avail, cost;
798     struct nstr_item nstr;
799     float eff = PLANE_MINEFF / 100.0;
800     int hcm, lcm, mil;
801     int freeplane = 0;
802
803     mil = roundavg(((double)pp->pl_crew * eff));
804     /* Always use at least 1 mil to build a plane */
805     if (mil == 0 && pp->pl_crew > 0)
806         mil = 1;
807     hcm = roundavg(((double)pp->pl_hcm * eff));
808     lcm = roundavg(((double)pp->pl_lcm * eff));
809     if (sp->sct_type != SCT_AIRPT && !player->god) {
810         pr("Planes must be built in airports.\n");
811         return 0;
812     }
813     if (sp->sct_effic < 60 && !player->god) {
814         pr("Sector %s is not 60%% efficient.\n",
815            xyas(sp->sct_x, sp->sct_y, player->cnum));
816         return 0;
817     }
818     if (vec[I_LCM] < lcm || vec[I_HCM] < hcm) {
819         pr("Not enough materials in %s\n",
820            xyas(sp->sct_x, sp->sct_y, player->cnum));
821         return 0;
822     }
823     avail = (PLN_BLD_WORK(pp->pl_lcm, pp->pl_hcm) * PLANE_MINEFF + 99) / 100;
824     if (sp->sct_avail < avail) {
825         pr("Not enough available work in %s to build a %s\n",
826            xyas(sp->sct_x, sp->sct_y, player->cnum), pp->pl_name);
827         pr(" (%d available work required)\n", avail);
828         return 0;
829     }
830     cost = pp->pl_cost * PLANE_MINEFF / 100;
831     if (cash < cost) {
832         pr("Not enough money left to build a %s\n", pp->pl_name);
833         return 0;
834     }
835     if (vec[I_MILIT] < mil || (vec[I_MILIT] == 0 && pp->pl_crew > 0)) {
836         pr("Not enough military for crew in %s\n",
837            xyas(sp->sct_x, sp->sct_y, player->cnum));
838         return 0;
839     }
840     if (!trechk(player->cnum, 0, NEWPLN))
841         return 0;
842     if (!check_sect_ok(sp))
843         return 0;
844     sp->sct_avail -= avail;
845     player->dolcost += cost;
846     cash -= cost;
847     snxtitem_all(&nstr, EF_PLANE);
848     freeplane = 0;
849     while (nxtitem(&nstr, (s_char *)&plane)) {
850         if (plane.pln_own == 0) {
851             freeplane++;
852             break;
853         }
854     }
855     if (freeplane == 0) {
856         ef_extend(EF_PLANE, 50);
857     }
858     memset(&plane, 0, sizeof(struct plnstr));
859     plane.pln_x = sp->sct_x;
860     plane.pln_y = sp->sct_y;
861     plane.pln_own = sp->sct_own;
862     plane.pln_type = pp - plchr;
863     plane.pln_effic = PLANE_MINEFF;
864     if (opt_MOB_ACCESS) {
865         time(&plane.pln_access);
866         plane.pln_mobil = -(etu_per_update / sect_mob_neg_factor);
867     } else {
868         plane.pln_mobil = 0;
869     }
870     plane.pln_mission = 0;
871     plane.pln_opx = 0;
872     plane.pln_opy = 0;
873     plane.pln_radius = 0;
874     plane.pln_range = UCHAR_MAX; /* will be adjusted by pln_set_tech() */
875     plane.pln_range_max = plane.pln_range;
876     plane.pln_fuel = pp->pl_fuel;
877     plane.pln_wing = ' ';
878     plane.pln_ship = -1;
879     plane.pln_land = -1;
880     plane.pln_uid = nstr.cur;
881     plane.pln_nuketype = -1;
882     plane.pln_harden = 0;
883     plane.pln_flags = 0;
884     pln_set_tech(&plane, tlev);
885
886     vec[I_LCM] -= lcm;
887     vec[I_HCM] -= hcm;
888     vec[I_MILIT] -= mil;
889
890     makenotlost(EF_PLANE, plane.pln_own, plane.pln_uid, plane.pln_x,
891                 plane.pln_y);
892     putplane(plane.pln_uid, &plane);
893     pr("%s built in sector %s\n", prplane(&plane),
894        xyas(sp->sct_x, sp->sct_y, player->cnum));
895     return 1;
896 }
897
898 static int
899 build_tower(register struct sctstr *sp, short *vec)
900 {
901     struct sctstr sect;
902     int val;
903     int newx, newy;
904     int avail;
905     s_char *p;
906     s_char buf[1024];
907     int good;
908     int i;
909     int nx;
910     int ny;
911
912     if (sp->sct_type != SCT_BSPAN) {
913         pr("Bridge towers can only be built from bridge spans.\n");
914         return 0;
915     }
916
917     if (sp->sct_effic < 60 && !player->god) {
918         pr("Sector %s is not 60%% efficient.\n",
919            xyas(sp->sct_x, sp->sct_y, player->cnum));
920         return 0;
921     }
922
923     if (!opt_NO_HCMS) {
924         if (vec[I_HCM] < buil_tower_bh) {
925             pr("%s only has %d unit%s of hcm,\n",
926                xyas(sp->sct_x, sp->sct_y, player->cnum),
927                vec[I_HCM], vec[I_HCM] > 1 ? "s" : "");
928             pr("(a bridge tower requires %d)\n", buil_tower_bh);
929             return 0;
930         }
931     } else if (!opt_NO_LCMS) {
932         if (vec[I_LCM] < buil_tower_bh) {
933             pr("%s only has %d unit%s of lcm,\n",
934                xyas(sp->sct_x, sp->sct_y, player->cnum),
935                vec[I_LCM], vec[I_LCM] > 1 ? "s" : "");
936             pr("(a bridge tower requires %d)\n", buil_tower_bh);
937             return 0;
938         }
939     }
940     if (cash < buil_tower_bc) {
941         pr("A bridge tower costs $%.2f to build; ", buil_tower_bc);
942         pr("you only have %d.\n", cash);
943         return 0;
944     }
945     avail = (SCT_BLD_WORK(0, buil_tower_bh) * SCT_MINEFF + 99) / 100;
946     if (sp->sct_avail < avail) {
947         pr("Not enough available work in %s to build a bridge tower\n",
948            xyas(sp->sct_x, sp->sct_y, player->cnum));
949         pr(" (%d available work required)\n", avail);
950         return 0;
951     }
952     if (!player->argp[3]) {
953         pr("Building from %s\n", xyas(sp->sct_x, sp->sct_y, player->cnum));
954         nav_map(sp->sct_x, sp->sct_y, 1);
955     }
956     if (!(p = getstarg(player->argp[3], "build tower in what direction? ", buf))
957         || !*p) {
958         return 0;
959     }
960     /* Sanity check time */
961     if (!check_sect_ok(sp))
962         return 0;
963
964     if ((val = chkdir(*p, DIR_FIRST, DIR_LAST)) < 0) {
965         pr("'%c' is not a valid direction...\n", *p);
966         direrr(0, 0, 0);
967         return 0;
968     }
969     newx = sp->sct_x + diroff[val][0];
970     newy = sp->sct_y + diroff[val][1];
971     if (getsect(newx, newy, &sect) == 0 || sect.sct_type != SCT_WATER) {
972         pr("%s is not a water sector\n", xyas(newx, newy, player->cnum));
973         return 0;
974     }
975
976     /* Now, check.  You aren't allowed to build bridge towers
977        next to land. */
978     good = 0;
979     for (i = 1; i <= 6; i++) {
980         struct sctstr s2;
981         nx = sect.sct_x + diroff[i][0];
982         ny = sect.sct_y + diroff[i][1];
983         getsect(nx, ny, &s2);
984         if ((s2.sct_type != SCT_WATER) &&
985             (s2.sct_type != SCT_BTOWER) && (s2.sct_type != SCT_BSPAN)) {
986             good = 1;
987             break;
988         }
989     }
990     if (good) {
991         pr("Bridge towers cannot be built adjacent to land.\n");
992         pr("That sector is adjacent to land.\n");
993         return 0;
994     }
995
996     sp->sct_avail -= avail;
997     player->dolcost += buil_tower_bc;
998     cash -= buil_tower_bc;
999     sect.sct_type = SCT_BTOWER;
1000     sect.sct_newtype = SCT_BTOWER;
1001     sect.sct_effic = SCT_MINEFF;
1002     sect.sct_road = 0;
1003     sect.sct_rail = 0;
1004     sect.sct_defense = 0;
1005     if (opt_MOB_ACCESS) {
1006         time(&sect.sct_access);
1007         sect.sct_mobil = -(etu_per_update / sect_mob_neg_factor);
1008     } else {
1009         sect.sct_mobil = 0;
1010     }
1011     if (!opt_DEFENSE_INFRA)
1012         sect.sct_defense = sect.sct_effic;
1013     sect.sct_mines = 0;
1014     putsect(&sect);
1015     pr("Bridge tower built in %s\n",
1016        xyas(sect.sct_x, sect.sct_y, player->cnum));
1017     if (!opt_NO_HCMS)
1018         vec[I_HCM] -= buil_tower_bh;
1019     else if (!opt_NO_LCMS)
1020         vec[I_LCM] -= buil_tower_bh;
1021     return 1;
1022 }