]> git.pond.sub.org Git - empserver/blob - src/lib/commands/new.c
(natstr): Members nat_xstart, nat_ystart are used write-only. Remove.
[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 <config.h>
35
36 #include "misc.h"
37 #include "player.h"
38 #include "nat.h"
39 #include "sect.h"
40 #include "path.h"
41 #include "file.h"
42 #include "xy.h"
43 #include "tel.h"
44 #include "land.h"
45 #include "nsc.h"
46 #include "optlist.h"
47 #include "commands.h"
48
49 #include <fcntl.h>
50
51 static int isok(int x, int y);
52 static void ok(s_char *map, int x, int y);
53
54 static struct range defrealm = { -8, -5, 10, 5, 0, 0 };
55
56 #define MAXAVAIL        300
57
58 int
59 new(void)
60 {
61     struct sctstr sect;
62     struct natstr *natp;
63     struct boundstr newrealms;
64     struct range absrealm;
65     natid num;
66     coord x, y;
67     int i;
68     s_char *p;
69     int n;
70     s_char buf[1024];
71
72     natp = getnatp(player->cnum);
73     if (natp->nat_xorg != 0 || natp->nat_yorg != 0) {
74         pr("Must be at 0,0 to add a new country\n");
75         return 0;
76     }
77     if ((n = natarg(player->argp[1], "Country? ")) < 0) {
78         pr("Bad country number\n");
79         return 0;
80     }
81     num = n;
82     natp = getnatp(num);
83     if ((natp->nat_stat & STAT_NEW) == 0) {
84         pr("Country #%d (%s) isn't a new country!\n", num, cname(num));
85         return RET_SYN;
86     }
87     if (player->argp[2] != 0) {
88         if ((p = getstarg(player->argp[2], "sanctuary pair : ", buf)) == 0)
89             return RET_SYN;
90         if (!sarg_xy(p, &x, &y) || !getsect(x, y, &sect))
91             return RET_SYN;
92         if (sect.sct_type != SCT_RURAL) {
93             pr("%s is a %s; try again...\n",
94                xyas(x, y, player->cnum), dchr[sect.sct_type].d_name);
95             return RET_SYN;
96         }
97         getsect(x + 2, y, &sect);
98         if (sect.sct_type != SCT_RURAL) {
99             pr("%s is a %s; try again...\n",
100                xyas(x + 2, y, player->cnum), dchr[sect.sct_type].d_name);
101             return RET_SYN;
102         }
103     } else {
104         for (i = 0; i < 300 && !player->aborted; i++) {
105             /* Both x and y should be either odd or even */
106             x = (random() % WORLD_X) - (WORLD_X / 2);
107             y = (((random() % WORLD_Y) - (WORLD_Y / 2)) & ~1) | (x & 1);
108             /*
109              * If either of the two potential
110              * sanctuary sectors are already
111              * owned by someone else, pick
112              * another place on the map.
113              */
114             getsect(x, y, &sect);
115             if (sect.sct_type == SCT_WATER || sect.sct_own != 0)
116                 continue;
117             getsect(x + 2, y, &sect);
118             if (sect.sct_type == SCT_WATER || sect.sct_own != 0)
119                 continue;
120             if (isok(x, y))
121                 break;
122         }
123         if (i == 300) {
124             pr("couldn't find an empty slot!\n");
125             return RET_FAIL;
126         }
127     }
128
129     if (player->aborted)
130         return RET_FAIL;
131     pr("added country %d at %s\n", num, xyas(x, y, player->cnum));
132     getsect(x, y, &sect);
133     sect.sct_own = num;
134     sect.sct_type = SCT_SANCT;
135     sect.sct_newtype = SCT_SANCT;
136     sect.sct_effic = 100;
137     sect.sct_road = 0;
138     sect.sct_rail = 0;
139     sect.sct_defense = 0;
140     if (!opt_DEFENSE_INFRA)
141         sect.sct_defense = sect.sct_effic;
142     sect.sct_mobil = startmob;
143     sect.sct_work = 100;
144     sect.sct_oldown = num;
145     if (at_least_one_100) {
146         sect.sct_oil = 100;
147         sect.sct_fertil = 100;
148         sect.sct_uran = 100;
149         sect.sct_min = 100;
150         sect.sct_gmin = 100;
151     }
152     sect.sct_item[I_CIVIL] = opt_RES_POP ? 550 : 999;
153     sect.sct_item[I_MILIT] = 55;
154     sect.sct_item[I_FOOD] = 1000;
155     sect.sct_item[I_UW] = 75;
156     putsect(&sect);
157     getsect(x + 2, y, &sect);
158     sect.sct_own = num;
159     sect.sct_type = SCT_SANCT;
160     sect.sct_newtype = SCT_SANCT;
161     sect.sct_effic = 100;
162     sect.sct_road = 0;
163     sect.sct_rail = 0;
164     sect.sct_defense = 0;
165     if (!opt_DEFENSE_INFRA)
166         sect.sct_defense = sect.sct_effic;
167     sect.sct_work = 100;
168     sect.sct_oldown = num;
169     sect.sct_mobil = startmob;
170     if (at_least_one_100) {
171         sect.sct_oil = 100;
172         sect.sct_fertil = 100;
173         sect.sct_uran = 100;
174         sect.sct_min = 100;
175         sect.sct_gmin = 100;
176     }
177     sect.sct_item[I_CIVIL] = opt_RES_POP ? 550 : 999;
178     sect.sct_item[I_MILIT] = 55;
179     sect.sct_item[I_FOOD] = 100;
180     sect.sct_item[I_UW] = 75;
181     putsect(&sect);
182     natp->nat_btu = max_btus;
183     natp->nat_stat &= ~STAT_NEW;
184     natp->nat_stat |= STAT_SANCT;
185     natp->nat_xcap = x;
186     natp->nat_ycap = y;
187     natp->nat_xorg = x;
188     natp->nat_yorg = y;
189     xyabsrange(natp, &defrealm, &absrealm);
190     newrealms.b_xl = absrealm.lx;
191     newrealms.b_xh = absrealm.hx;
192     newrealms.b_yl = absrealm.ly;
193     newrealms.b_yh = absrealm.hy;
194     if (players_at_00) {
195         natp->nat_xorg = 0;
196         natp->nat_yorg = 0;
197     }
198     natp->nat_money = start_cash;
199     natp->nat_level[NAT_HLEV] = start_happiness;
200     natp->nat_level[NAT_RLEV] = start_research;
201     natp->nat_level[NAT_TLEV] = start_technology;
202     natp->nat_level[NAT_ELEV] = start_education;
203     for (i = 0; i < MAXNOR; i++)
204         natp->nat_b[i] = newrealms;
205     natp->nat_tgms = 0;
206     (void)close(open(mailbox(buf, num), O_RDWR | O_TRUNC | O_CREAT, 0660));
207     putnat(natp);
208     return 0;
209 }
210
211 static int nmin, ngold, noil, nur;
212 static int nfree, navail, nowned;
213
214 static int
215 isok(int x, int y)
216 {
217     s_char *map;
218     s_char *p;
219     s_char buf[1024];
220
221     nmin = ngold = noil = nur = 0;
222     navail = nfree = nowned = 0;
223     if ((map = malloc((WORLD_X * WORLD_Y) / 2)) == 0) {
224         logerror("malloc failed in isok\n");
225         pr("Memory error.  Tell the deity.\n");
226         return 0;
227     }
228     memset(map, 0, (WORLD_X * WORLD_Y) / 2);
229     ok(map, x, y);
230     free(map);
231     if (nfree < 5)
232         return 0;
233     pr("Cap at %s; owned sectors: %d, free sectors: %d, avail: %d\n",
234        xyas(x, y, player->cnum), nowned, nfree, navail);
235     pr("min: %d, oil: %d, gold: %d, uranium: %d\n",
236        nmin, noil, ngold, nur);
237     p = getstring("This setup ok? ", buf);
238     if (p == 0 || *p != 'y')
239         return 0;
240     return 1;
241 }
242
243 static void
244 ok(s_char *map, int x, int y)
245 {
246     struct sctstr sect;
247     int dir;
248     int id;
249
250     if (navail > MAXAVAIL)
251         return;
252     id = sctoff(x, y);
253     if (map[id])
254         return;
255     if (!ef_read(EF_SECTOR, id, &sect))
256         return;
257     if (sect.sct_type == SCT_WATER || sect.sct_type == SCT_BSPAN)
258         return;
259     navail++;
260     if (navail >= MAXAVAIL) {
261         pr("At least %d...\n", MAXAVAIL);
262         return;
263     }
264     if (sect.sct_type != SCT_MOUNT && sect.sct_type != SCT_PLAINS) {
265         if (sect.sct_own == 0)
266             nfree++;
267         else
268             nowned++;
269         if (sect.sct_min > 9)
270             nmin++;
271         if (sect.sct_gmin > 9)
272             ngold++;
273         if (sect.sct_uran > 9)
274             nur++;
275         if (sect.sct_oil > 9)
276             noil++;
277     }
278     map[id] = 1;
279     for (dir = DIR_FIRST; dir <= DIR_LAST; dir++)
280         ok(map, diroff[dir][0] + x, diroff[dir][1] + y);
281 }