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