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