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