]> git.pond.sub.org Git - empserver/blob - src/lib/commands/new.c
Import of Empire 4.2.12
[empserver] / src / lib / commands / new.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  *  new.c: Create a new capital for a player
29  * 
30  *  Known contributors to this file:
31  *     Dave Pare, 1986
32  */
33
34 #include "misc.h"
35 #include "player.h"
36 #include "var.h"
37 #include "nat.h"
38 #include "sect.h"
39 #include "path.h"
40 #include "file.h"
41 #include "xy.h"
42 #include "tel.h"
43 #include "land.h"
44 #include "nsc.h"
45 #include "options.h"
46 #include "optlist.h"
47 #include "commands.h"
48
49
50 #include <fcntl.h>
51
52 extern float start_education, start_happiness;
53 extern float start_technology, start_research;
54 extern int morale_base;
55
56 static  void ok(s_char *map, int x, int y);
57 static  int isok(int x, int y);
58
59 static  struct range defrealm = {-8, -5, 10, 5, 0, 0};
60
61 #define MAXAVAIL        300
62
63 int
64 new(void)
65 {
66         extern  int max_btus;
67         extern  int players_at_00;
68         extern  int at_least_one_100;
69 #ifdef START_UNITS
70         extern  int start_unit_type[START_UNITS];
71 #endif /* START_UNITS */
72         struct  sctstr sect;
73         struct  natstr *natp;
74         struct  boundstr newrealms;
75         struct  range absrealm;
76         natid   num;
77         time_t  now;
78         coord   x, y;
79         int     i;
80         s_char  *p;
81         int     n;
82         extern int startmob;
83         s_char  buf[1024];
84
85         natp = getnatp(player->cnum);
86         if (natp->nat_xorg != 0 || natp->nat_yorg != 0) {
87                 pr("Must be at 0,0 to add a new country\n");
88                 return 0;
89         }
90         if ((n = natarg(player->argp[1], "Country? ")) < 0) {
91                 pr("Bad country number\n");
92                 return 0;
93         }
94         num = n;
95         natp = getnatp(num);
96         if ((natp->nat_stat & STAT_NEW) == 0) {
97                 pr("Country #%d (%s) isn't a new country!\n",
98                        num, cname(num));
99                 return RET_SYN;
100         }
101         if (player->argp[2] != 0) {
102                 if ((p = getstarg(player->argp[2], "sanctuary pair : ", buf)) == 0)
103                         return RET_SYN;
104                 if (!sarg_xy(p, &x, &y) || !getsect(x, y, &sect))
105                         return RET_SYN;
106                 if (sect.sct_type != SCT_RURAL) {
107                         pr("%s is a %s; try again...\n",
108                                 xyas(x, y, player->cnum), dchr[sect.sct_type].d_name);
109                         return RET_SYN;
110                 }
111                 getsect(x + 2, y, &sect);
112                 if (sect.sct_type != SCT_RURAL) {
113                         pr("%s is a %s; try again...\n",
114                                 xyas(x, y, player->cnum), dchr[sect.sct_type].d_name);
115                         return RET_SYN;
116                 }
117         } else {
118                 (void) time(&now);
119 #if !defined(_WIN32)
120                 (void) srandom(now);
121 #else
122                 (void) srand(now);
123 #endif
124                 for (i = 0; i < 300 && !player->aborted; i++) {
125                         /* Both x and y should be either odd or even */
126                         x = (random() % WORLD_X)-(WORLD_X/2);
127                         y = (((random() % WORLD_Y)-(WORLD_Y/2)) & ~1) | (x & 1);
128                         /*
129                          * If either of the two potential
130                          * sanctuary sectors are already
131                          * owned by someone else, pick
132                          * another place on the map.
133                          */
134                         getsect(x, y, &sect);
135                         if (sect.sct_type == SCT_WATER || sect.sct_own != 0)
136                                 continue;
137                         getsect(x+2, y, &sect);
138                         if (sect.sct_type == SCT_WATER || sect.sct_own != 0)
139                                 continue;
140                         if (isok(x, y))
141                                 break;
142                 }
143                 if (i == 300) {
144                         pr("couldn't find an empty slot!\n");
145                         return RET_FAIL;
146                 }
147         }
148         
149         if (player->aborted)
150                 return RET_FAIL;
151         pr("added country %d at %s\n", num, xyas(x, y, player->cnum));
152         getsect(x, y, &sect);
153         sect.sct_own = num;
154         sect.sct_type = SCT_SANCT;
155         sect.sct_newtype = SCT_SANCT;
156         sect.sct_effic = 100;
157         sect.sct_road = 0;
158         sect.sct_rail = 0;
159         sect.sct_defense = 0;
160         if (!opt_DEFENSE_INFRA)
161           sect.sct_defense = sect.sct_effic;
162         sect.sct_mobil = startmob;
163         sect.sct_work = 100;
164         sect.sct_oldown = num;
165         if (at_least_one_100) {
166                 sect.sct_oil = 100;
167                 sect.sct_fertil = 100;
168                 sect.sct_uran = 100;
169                 sect.sct_min = 100;
170                 sect.sct_gmin = 100;
171         }
172         if (opt_RES_POP)
173           putvar(V_CIVIL, 550, (s_char *)&sect, EF_SECTOR);
174         else
175           putvar(V_CIVIL, 999, (s_char *)&sect, EF_SECTOR);
176         putvar(V_MILIT, 55, (s_char *)&sect, EF_SECTOR);
177         putvar(V_FOOD, 1000, (s_char *)&sect, EF_SECTOR);
178         putvar(V_UW, 75, (s_char *)&sect, EF_SECTOR);
179         putsect(&sect);
180         getsect(x + 2, y, &sect);
181         sect.sct_own = num;
182         sect.sct_type = SCT_SANCT;
183         sect.sct_newtype = SCT_SANCT;
184         sect.sct_effic = 100;
185         sect.sct_road = 0;
186         sect.sct_rail = 0;
187         sect.sct_defense = 0;
188         if (!opt_DEFENSE_INFRA)
189           sect.sct_defense = sect.sct_effic;
190         sect.sct_work = 100;
191         sect.sct_oldown = num;
192         sect.sct_mobil = startmob;
193         if (at_least_one_100) {
194                 sect.sct_oil = 100;
195                 sect.sct_fertil = 100;
196                 sect.sct_uran = 100;
197                 sect.sct_min = 100;
198                 sect.sct_gmin = 100;
199         }
200         if (opt_RES_POP)
201           putvar(V_CIVIL, 550, (s_char *)&sect, EF_SECTOR);
202         else
203           putvar(V_CIVIL, 999, (s_char *)&sect, EF_SECTOR);
204         putvar(V_FOOD, 100, (s_char *)&sect, EF_SECTOR);
205         putvar(V_MILIT, 55, (s_char *)&sect, EF_SECTOR);
206         putvar(V_UW, 75, (s_char *)&sect, EF_SECTOR);
207         putsect(&sect);
208         natp->nat_btu = max_btus;
209         natp->nat_stat &= ~STAT_NEW;
210         natp->nat_stat |= STAT_SANCT;
211         natp->nat_xstart = x;
212         natp->nat_ystart = y;
213         natp->nat_xcap = x;
214         natp->nat_ycap = y;
215         if (players_at_00) {
216                 natp->nat_xorg = 0;
217                 natp->nat_yorg = 0;
218         } else {
219                 natp->nat_xorg = x;
220                 natp->nat_yorg = y;
221         }
222         xyabsrange(natp, &defrealm, &absrealm);
223         newrealms.b_xl = absrealm.lx;
224         newrealms.b_xh = absrealm.hx;
225         newrealms.b_yl = absrealm.ly;
226         newrealms.b_yh = absrealm.hy;
227         natp->nat_money = START_CASH;
228         natp->nat_level[NAT_HLEV] = start_happiness;
229         natp->nat_level[NAT_RLEV] = start_research;
230         natp->nat_level[NAT_TLEV] = start_technology;
231         natp->nat_level[NAT_ELEV] = start_education;
232         for (i = 0; i < MAXNOR; i++)
233                 natp->nat_b[i] = newrealms;
234         natp->nat_tgms = 0;
235         (void) close(open(mailbox(buf, num), O_RDWR|O_TRUNC|O_CREAT, 0660));
236 #ifdef START_UNITS
237         for(n=0;n<START_UNITS;n++)
238                 deity_build_land(start_unit_type[n], x, y, num, 0);
239 #endif /* START_UNITS */
240         putnat(natp);
241         return 0;
242 }
243
244 static int nmin, ngold, noil, nur;
245 static int nfree, navail, nowned;
246
247 static int
248 isok(int x, int y)
249 {
250         s_char  *map;
251         s_char   *p;
252         s_char  buf[1024];
253
254         nmin = ngold = noil = nur = 0;
255         navail = nfree = nowned = 0;
256         if ((map = (s_char *)malloc((WORLD_X * WORLD_Y) / 2)) == 0) {
257             logerror("malloc failed in isok\n");
258             pr("Memory error.  Tell the deity.\n");
259             return 0;
260         }
261         bzero((s_char *)map, (WORLD_X * WORLD_Y) / 2);
262         ok(map, x, y);
263         free((s_char *)map);
264         if (nfree < 5)
265             return 0;
266         pr("Cap at %s; owned sectors: %d, free sectors: %d, avail: %d\n",
267                xyas(x, y, player->cnum), nowned, nfree, navail);
268         pr("min: %d, oil: %d, gold: %d, uranium: %d\n",
269                nmin, noil, ngold, nur);
270         p = getstring("This setup ok? ", buf);
271         if (p == 0 || *p != 'y')
272                 return 0;
273         return 1;
274 }
275
276 static void
277 ok(s_char *map, int x, int y)
278 {
279         struct  sctstr sect;
280         int     dir;
281         int     id;
282
283         if (navail > MAXAVAIL)
284                 return;
285         id = sctoff(x, y);
286         if (map[id])
287                 return;
288         if (!ef_read(EF_SECTOR, id, (s_char *)&sect))
289                 return;
290         if (sect.sct_type == SCT_WATER || sect.sct_type == SCT_BSPAN)
291                 return;
292         navail++;
293         if (navail >= MAXAVAIL) {
294                 pr("At least %d...\n", MAXAVAIL);
295                 return;
296         }
297         if (sect.sct_type != SCT_MOUNT &&
298             sect.sct_type != SCT_PLAINS) {
299                 if (sect.sct_own == 0)
300                         nfree++;
301                 else
302                         nowned++;
303                 if (sect.sct_min > 9)
304                         nmin++;
305                 if (sect.sct_gmin > 9)
306                         ngold++;
307                 if (sect.sct_uran > 9)
308                         nur++;
309                 if (sect.sct_oil > 9)
310                         noil++;
311         }
312         map[id] = 1;
313         for (dir = DIR_FIRST; dir <= DIR_LAST; dir++)
314                 ok(map, diroff[dir][0] + x, diroff[dir][1] + y);
315 }
316
317 int
318 deity_build_land(int type, coord x, coord y, natid own, int tlev)
319 {
320         extern  int land_mob_max;
321         struct  lndstr land;
322         struct  lchrstr *lp;
323         struct  nstr_item nstr;
324         struct  natstr *natp;
325         double  techfact(int, double);
326         int     lvec[I_MAX+1];
327         int     extend = 1;
328
329         natp = getnatp(own);
330
331         snxtitem_all(&nstr, EF_LAND);
332         while (nxtitem(&nstr, (s_char *)&land)) {
333                 if (land.lnd_own == 0) {
334                         extend = 0;
335                         break;
336                 }
337         }
338         if (extend)
339                 ef_extend(EF_LAND, 50);
340         land.lnd_x = x;
341         land.lnd_y = y;
342         land.lnd_own = own;
343         land.lnd_mission = 0;
344         land.lnd_type = type;
345         land.lnd_effic = 100;
346         land.lnd_mobil = land_mob_max;
347         land.lnd_sell = 0;
348         land.lnd_tech = tlev;
349         land.lnd_uid = nstr.cur;
350         land.lnd_army = ' ';
351         land.lnd_flags = 0;
352         land.lnd_ship = -1;
353         land.lnd_land = -1;
354         land.lnd_nland = 0;
355         land.lnd_harden = 0;
356         time(&land.lnd_access);
357
358         land.lnd_retreat = morale_base;
359
360         lp = &lchr[type];
361         land.lnd_fuel = lp->l_fuelc;
362         land.lnd_nxlight = 0;
363         land.lnd_rflags = 0;
364         bzero((s_char *)land.lnd_rpath,10);
365         land.lnd_rad_max = lp->l_rad;
366         land.lnd_nv = 0;
367
368         land.lnd_att = (float)LND_ATTDEF(lp->l_att, tlev - lp->l_tech);
369         land.lnd_def = (float)LND_ATTDEF(lp->l_def, tlev - lp->l_tech);
370         land.lnd_vul = (int)LND_VUL(lp->l_vul, tlev - lp->l_tech);
371         land.lnd_spd = (int)LND_SPD(lp->l_spd, tlev - lp->l_tech);
372         land.lnd_vis = (int)LND_VIS(lp->l_vis, tlev - lp->l_tech);
373         land.lnd_spy = (int)LND_SPY(lp->l_spy, tlev - lp->l_tech);
374         land.lnd_rad = (int)LND_RAD(lp->l_rad, tlev - lp->l_tech);
375         land.lnd_frg = (int)LND_FRG(lp->l_frg, tlev - lp->l_tech);
376         land.lnd_acc = (int)LND_ACC(lp->l_acc, tlev - lp->l_tech);
377         land.lnd_dam = (int)LND_DAM(lp->l_dam, tlev - lp->l_tech);
378         land.lnd_ammo = (int)LND_AMM(lp->l_ammo, lp->l_dam, tlev - lp->l_tech);
379         land.lnd_aaf = (int)LND_AAF(lp->l_aaf, tlev - lp->l_tech);
380         land.lnd_fuelc = (int)LND_FC(lp->l_fuelc, tlev - lp->l_tech);
381         land.lnd_fuelu = (int)LND_FU(lp->l_fuelu, tlev - lp->l_tech);
382         land.lnd_maxlight = (int)LND_XPL(lp->l_nxlight, tlev - lp->l_tech);
383         land.lnd_maxland = (int)LND_MXL(lp->l_mxland, tlev - lp->l_tech);
384
385         bzero((s_char *)lvec, sizeof(lvec));
386         getvec(VT_ITEM, lvec, (s_char *)&land, EF_LAND);
387         lvec[I_FOOD] += vl_find(V_FOOD,lp->l_vtype,lp->l_vamt,(int)lp->l_nv);
388         lvec[I_MILIT] = lp->l_mil;
389         putvec(VT_ITEM, lvec, (s_char *)&land, EF_LAND);
390
391         putland(land.lnd_uid, &land);
392         makenotlost(EF_LAND, land.lnd_own, land.lnd_uid, land.lnd_x, land.lnd_y);
393         pr("%s", prland(&land));
394         pr(" built in sector %s\n", xyas(x, y, player->cnum));
395         return 1;
396 }