]> git.pond.sub.org Git - empserver/blob - src/lib/commands/new.c
Consider only wilderness for randomly placed sanctuaries
[empserver] / src / lib / commands / new.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2008, 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 files README, COPYING and CREDITS in the root of the source
23  *  tree for related information and legal notices.  It is expected
24  *  that future 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 <fcntl.h>
37 #include <sys/stat.h>
38 #include <unistd.h>
39 #include "commands.h"
40 #include "land.h"
41 #include "optlist.h"
42 #include "path.h"
43 #include "prototypes.h"
44 #include "tel.h"
45
46 static int isok(int x, int y);
47 static void ok(signed char *map, int x, int y);
48 static void init_sanct(struct natstr *, coord, coord);
49
50 static struct range defrealm = { -8, -5, 10, 5, 0, 0 };
51
52 #define MAXAVAIL        300
53
54 int
55 new(void)
56 {
57     struct sctstr sect;
58     struct natstr *natp;
59     struct realmstr newrealm;
60     struct range absrealm;
61     natid num;
62     coord x, y;
63     int i;
64     char *p;
65     char buf[1024];
66     time_t current_time = time(NULL);
67
68     natp = getnatp(player->cnum);
69     if (natp->nat_xorg != 0 || natp->nat_yorg != 0) {
70         pr("Must be at 0,0 to add a new country\n");
71         return RET_FAIL;
72     }
73     if (!(natp = natargp(player->argp[1], "Country? ")))
74         return RET_SYN;
75     num = natp->nat_cnum;
76     if (natp->nat_stat != STAT_NEW) {
77         pr("Country #%d (%s) isn't a new country!\n", num, cname(num));
78         return RET_SYN;
79     }
80     if (player->argp[2] != 0) {
81         if ((p = getstarg(player->argp[2], "sanctuary pair : ", buf)) == 0)
82             return RET_SYN;
83         if (!sarg_xy(p, &x, &y) || !getsect(x, y, &sect))
84             return RET_SYN;
85         if (sect.sct_type != SCT_RURAL) {
86             pr("%s is a %s; try again...\n",
87                xyas(x, y, player->cnum), dchr[sect.sct_type].d_name);
88             return RET_SYN;
89         }
90         getsect(x + 2, y, &sect);
91         if (sect.sct_type != SCT_RURAL) {
92             pr("%s is a %s; try again...\n",
93                xyas(x + 2, y, player->cnum), dchr[sect.sct_type].d_name);
94             return RET_SYN;
95         }
96     } else {
97         for (i = 0; i < 300 && !player->aborted; i++) {
98             /* Both x and y should be either odd or even */
99             x = (random() % WORLD_X) - (WORLD_X / 2);
100             y = (((random() % WORLD_Y) - (WORLD_Y / 2)) & ~1) | (x & 1);
101             /*
102              * If either of the two potential
103              * sanctuary sectors are already
104              * owned by someone else, pick
105              * another place on the map.
106              */
107             getsect(x, y, &sect);
108             if (sect.sct_type != SCT_RURAL || sect.sct_own != 0)
109                 continue;
110             getsect(x + 2, y, &sect);
111             if (sect.sct_type != SCT_RURAL || sect.sct_own != 0)
112                 continue;
113             if (isok(x, y))
114                 break;
115         }
116         if (i == 300) {
117             pr("couldn't find an empty slot!\n");
118             return RET_FAIL;
119         }
120     }
121
122     if (player->aborted)
123         return RET_FAIL;
124     pr("added country %d at %s\n", num, xyas(x, y, player->cnum));
125     natp->nat_btu = max_btus;
126     natp->nat_stat = STAT_SANCT;
127     natp->nat_xcap = x;
128     natp->nat_ycap = y;
129     natp->nat_xorg = x;
130     natp->nat_yorg = y;
131     xyabsrange(natp, &defrealm, &absrealm);
132     for (i = 0; i < MAXNOR; i++) {
133         getrealm(i, num, &newrealm);
134         newrealm.r_xl = absrealm.lx;
135         newrealm.r_xh = absrealm.hx;
136         newrealm.r_yl = absrealm.ly;
137         newrealm.r_yh = absrealm.hy;
138         newrealm.r_timestamp = current_time;
139         putrealm(&newrealm);
140     }
141     if (players_at_00) {
142         natp->nat_xorg = 0;
143         natp->nat_yorg = 0;
144     }
145     natp->nat_money = start_cash;
146     natp->nat_level[NAT_HLEV] = start_happiness;
147     natp->nat_level[NAT_RLEV] = start_research;
148     natp->nat_level[NAT_TLEV] = start_technology;
149     natp->nat_level[NAT_ELEV] = start_education;
150     natp->nat_tgms = 0;
151     close(creat(mailbox(buf, num), S_IRWUG));
152     init_sanct(natp, x, y);
153     init_sanct(natp, x + 2, y);
154     putnat(natp);
155     return RET_OK;
156 }
157
158 static void
159 init_sanct(struct natstr *natp, coord x, coord y)
160 {
161     struct sctstr sect;
162
163     getsect(x, y, &sect);
164     sect.sct_own = natp->nat_cnum;
165     sect.sct_type = SCT_SANCT;
166     sect.sct_newtype = SCT_SANCT;
167     sect.sct_effic = 100;
168     sect.sct_road = 0;
169     sect.sct_rail = 0;
170     sect.sct_defense = 0;
171     sect.sct_mobil = startmob;
172     sect.sct_work = 100;
173     sect.sct_oldown = natp->nat_cnum;
174     if (at_least_one_100) {
175         sect.sct_oil = 100;
176         sect.sct_fertil = 100;
177         sect.sct_uran = 100;
178         sect.sct_min = 100;
179         sect.sct_gmin = 100;
180     }
181     sect.sct_item[I_CIVIL] = max_pop(start_research, &sect);
182     sect.sct_item[I_MILIT] = 55;
183     sect.sct_item[I_FOOD] = opt_NOFOOD ? 0 : 550;
184     sect.sct_item[I_UW] = 75;
185     putsect(&sect);
186 }
187
188 static int nmin, ngold, noil, nur;
189 static int nfree, navail, nowned;
190
191 static int
192 isok(int x, int y)
193 {
194     signed char *map;
195     char *p;
196     char buf[1024];
197
198     nmin = ngold = noil = nur = 0;
199     navail = nfree = nowned = 0;
200     if (!(map = malloc(WORLD_SZ()))) {
201         logerror("malloc failed in isok\n");
202         pr("Memory error.  Tell the deity.\n");
203         return 0;
204     }
205     memset(map, 0, WORLD_SZ());
206     ok(map, x, y);
207     free(map);
208     if (nfree < 5)
209         return 0;
210     pr("Cap at %s; owned sectors: %d, free sectors: %d, avail: %d\n",
211        xyas(x, y, player->cnum), nowned, nfree, navail);
212     pr("min: %d, oil: %d, gold: %d, uranium: %d\n",
213        nmin, noil, ngold, nur);
214     p = getstring("This setup ok? ", buf);
215     if (p == 0 || *p != 'y')
216         return 0;
217     return 1;
218 }
219
220 static void
221 ok(signed char *map, int x, int y)
222 {
223     struct sctstr sect;
224     int dir;
225     int id;
226
227     if (navail > MAXAVAIL)
228         return;
229     id = sctoff(x, y);
230     if (map[id])
231         return;
232     if (!ef_read(EF_SECTOR, id, &sect))
233         return;
234     if (sect.sct_type == SCT_WATER || sect.sct_type == SCT_BSPAN)
235         return;
236     navail++;
237     if (navail >= MAXAVAIL) {
238         pr("At least %d...\n", MAXAVAIL);
239         return;
240     }
241     if (sect.sct_type != SCT_MOUNT && sect.sct_type != SCT_PLAINS) {
242         if (sect.sct_own == 0)
243             nfree++;
244         else
245             nowned++;
246         if (sect.sct_min > 9)
247             nmin++;
248         if (sect.sct_gmin > 9)
249             ngold++;
250         if (sect.sct_uran > 9)
251             nur++;
252         if (sect.sct_oil > 9)
253             noil++;
254     }
255     map[id] = 1;
256     for (dir = DIR_FIRST; dir <= DIR_LAST; dir++)
257         ok(map, diroff[dir][0] + x, diroff[dir][1] + y);
258 }