]> git.pond.sub.org Git - empserver/blob - src/lib/commands/new.c
Update copyright notice.
[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     if (players_at_00) {
192         natp->nat_xorg = 0;
193         natp->nat_yorg = 0;
194     } else {
195         natp->nat_xorg = x;
196         natp->nat_yorg = y;
197     }
198     xyabsrange(natp, &defrealm, &absrealm);
199     newrealms.b_xl = absrealm.lx;
200     newrealms.b_xh = absrealm.hx;
201     newrealms.b_yl = absrealm.ly;
202     newrealms.b_yh = absrealm.hy;
203     natp->nat_money = START_CASH;
204     natp->nat_level[NAT_HLEV] = start_happiness;
205     natp->nat_level[NAT_RLEV] = start_research;
206     natp->nat_level[NAT_TLEV] = start_technology;
207     natp->nat_level[NAT_ELEV] = start_education;
208     for (i = 0; i < MAXNOR; i++)
209         natp->nat_b[i] = newrealms;
210     natp->nat_tgms = 0;
211     (void)close(open(mailbox(buf, num), O_RDWR | O_TRUNC | O_CREAT, 0660));
212 #ifdef START_UNITS
213     for (n = 0; n < START_UNITS; n++)
214         deity_build_land(start_unit_type[n], x, y, num, 0);
215 #endif /* START_UNITS */
216     putnat(natp);
217     return 0;
218 }
219
220 static int nmin, ngold, noil, nur;
221 static int nfree, navail, nowned;
222
223 static int
224 isok(int x, int y)
225 {
226     s_char *map;
227     s_char *p;
228     s_char buf[1024];
229
230     nmin = ngold = noil = nur = 0;
231     navail = nfree = nowned = 0;
232     if ((map = (s_char *)malloc((WORLD_X * WORLD_Y) / 2)) == 0) {
233         logerror("malloc failed in isok\n");
234         pr("Memory error.  Tell the deity.\n");
235         return 0;
236     }
237     memset(map, 0, (WORLD_X * WORLD_Y) / 2);
238     ok(map, x, y);
239     free((s_char *)map);
240     if (nfree < 5)
241         return 0;
242     pr("Cap at %s; owned sectors: %d, free sectors: %d, avail: %d\n",
243        xyas(x, y, player->cnum), nowned, nfree, navail);
244     pr("min: %d, oil: %d, gold: %d, uranium: %d\n",
245        nmin, noil, ngold, nur);
246     p = getstring("This setup ok? ", buf);
247     if (p == 0 || *p != 'y')
248         return 0;
249     return 1;
250 }
251
252 static void
253 ok(s_char *map, int x, int y)
254 {
255     struct sctstr sect;
256     int dir;
257     int id;
258
259     if (navail > MAXAVAIL)
260         return;
261     id = sctoff(x, y);
262     if (map[id])
263         return;
264     if (!ef_read(EF_SECTOR, id, (s_char *)&sect))
265         return;
266     if (sect.sct_type == SCT_WATER || sect.sct_type == SCT_BSPAN)
267         return;
268     navail++;
269     if (navail >= MAXAVAIL) {
270         pr("At least %d...\n", MAXAVAIL);
271         return;
272     }
273     if (sect.sct_type != SCT_MOUNT && sect.sct_type != SCT_PLAINS) {
274         if (sect.sct_own == 0)
275             nfree++;
276         else
277             nowned++;
278         if (sect.sct_min > 9)
279             nmin++;
280         if (sect.sct_gmin > 9)
281             ngold++;
282         if (sect.sct_uran > 9)
283             nur++;
284         if (sect.sct_oil > 9)
285             noil++;
286     }
287     map[id] = 1;
288     for (dir = DIR_FIRST; dir <= DIR_LAST; dir++)
289         ok(map, diroff[dir][0] + x, diroff[dir][1] + y);
290 }
291
292 #ifdef START_UNITS
293 static int
294 deity_build_land(int type, coord x, coord y, natid own, int tlev)
295 {
296     struct lndstr land;
297     struct lchrstr *lp;
298     struct nstr_item nstr;
299     struct natstr *natp;
300     int extend = 1;
301
302     natp = getnatp(own);
303
304     snxtitem_all(&nstr, EF_LAND);
305     while (nxtitem(&nstr, (s_char *)&land)) {
306         if (land.lnd_own == 0) {
307             extend = 0;
308             break;
309         }
310     }
311     if (extend)
312         ef_extend(EF_LAND, 50);
313     land.lnd_x = x;
314     land.lnd_y = y;
315     land.lnd_own = own;
316     land.lnd_mission = 0;
317     land.lnd_type = type;
318     land.lnd_effic = 100;
319     land.lnd_mobil = land_mob_max;
320     land.lnd_tech = tlev;
321     land.lnd_uid = nstr.cur;
322     land.lnd_army = ' ';
323     land.lnd_flags = 0;
324     land.lnd_ship = -1;
325     land.lnd_land = -1;
326     land.lnd_nland = 0;
327     land.lnd_harden = 0;
328     time(&land.lnd_access);
329
330     land.lnd_retreat = morale_base;
331
332     lp = &lchr[type];
333     land.lnd_fuel = lp->l_fuelc;
334     land.lnd_nxlight = 0;
335     land.lnd_rflags = 0;
336     memset(land.lnd_rpath, 0, sizeof(land.lnd_rpath));
337     land.lnd_rad_max = lp->l_rad;
338     memset(land.lnd_item, 0, sizeof(land.lnd_item));
339
340     land.lnd_att = (float)LND_ATTDEF(lp->l_att, tlev - lp->l_tech);
341     land.lnd_def = (float)LND_ATTDEF(lp->l_def, tlev - lp->l_tech);
342     land.lnd_vul = (int)LND_VUL(lp->l_vul, tlev - lp->l_tech);
343     land.lnd_spd = (int)LND_SPD(lp->l_spd, tlev - lp->l_tech);
344     land.lnd_vis = (int)LND_VIS(lp->l_vis, tlev - lp->l_tech);
345     land.lnd_spy = (int)LND_SPY(lp->l_spy, tlev - lp->l_tech);
346     land.lnd_rad = (int)LND_RAD(lp->l_rad, tlev - lp->l_tech);
347     land.lnd_frg = (int)LND_FRG(lp->l_frg, tlev - lp->l_tech);
348     land.lnd_acc = (int)LND_ACC(lp->l_acc, tlev - lp->l_tech);
349     land.lnd_dam = (int)LND_DAM(lp->l_dam, tlev - lp->l_tech);
350     land.lnd_ammo = (int)LND_AMM(lp->l_ammo, lp->l_dam, tlev - lp->l_tech);
351     land.lnd_aaf = (int)LND_AAF(lp->l_aaf, tlev - lp->l_tech);
352     land.lnd_fuelc = (int)LND_FC(lp->l_fuelc, tlev - lp->l_tech);
353     land.lnd_fuelu = (int)LND_FU(lp->l_fuelu, tlev - lp->l_tech);
354     land.lnd_maxlight = (int)LND_XPL(lp->l_nxlight, tlev - lp->l_tech);
355     land.lnd_maxland = (int)LND_MXL(lp->l_mxland, tlev - lp->l_tech);
356     land.lnd_item[I_FOOD] = lp->l_item[I_FOOD];
357
358     putland(land.lnd_uid, &land);
359     makenotlost(EF_LAND, land.lnd_own, land.lnd_uid, land.lnd_x,
360                 land.lnd_y);
361     pr("%s", prland(&land));
362     pr(" built in sector %s\n", xyas(x, y, player->cnum));
363     return 1;
364 }
365 #endif /* START_UNITS */