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