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