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