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