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