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