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