author | koda |
Sat, 31 Jul 2010 10:42:40 +0200 | |
changeset 3693 | 09892cdb8f95 |
parent 3670 | 4c673e57f0d7 |
child 3697 | d5b30d6373fc |
permissions | -rw-r--r-- |
3547 | 1 |
// |
2 |
// gameSetup.m |
|
3 |
// hwengine |
|
4 |
// |
|
5 |
// Created by Vittorio on 10/01/10. |
|
6 |
// Copyright 2010 __MyCompanyName__. All rights reserved. |
|
7 |
// |
|
8 |
||
9 |
#import "GameSetup.h" |
|
10 |
#import "SDL_uikitappdelegate.h" |
|
11 |
#import "SDL_net.h" |
|
12 |
#import "PascalImports.h" |
|
13 |
#import "CommodityFunctions.h" |
|
14 |
||
15 |
#define BUFFER_SIZE 256 |
|
16 |
||
17 |
@implementation GameSetup |
|
18 |
||
19 |
@synthesize systemSettings, gameConfig; |
|
20 |
||
3616
85d69ddb41b6
tackling the iphoneos todo (another trial for the beginning sporadic bug, remove curl animation, don't need to go to detail page)
koda
parents:
3613
diff
changeset
|
21 |
-(id) initWithDictionary:(NSDictionary *)gameDictionary { |
3547 | 22 |
if (self = [super init]) { |
23 |
ipcPort = randomPort(); |
|
24 |
||
25 |
// should check they exist and throw and exection if not |
|
26 |
NSDictionary *dictSett = [[NSDictionary alloc] initWithContentsOfFile:SETTINGS_FILE()]; |
|
27 |
self.systemSettings = dictSett; |
|
28 |
[dictSett release]; |
|
29 |
||
3616
85d69ddb41b6
tackling the iphoneos todo (another trial for the beginning sporadic bug, remove curl animation, don't need to go to detail page)
koda
parents:
3613
diff
changeset
|
30 |
self.gameConfig = gameDictionary; |
3547 | 31 |
} |
32 |
return self; |
|
33 |
} |
|
34 |
||
35 |
-(void) dealloc { |
|
36 |
[gameConfig release]; |
|
37 |
[systemSettings release]; |
|
38 |
[super dealloc]; |
|
39 |
} |
|
40 |
||
41 |
#pragma mark - |
|
42 |
#pragma mark Provider functions |
|
43 |
// unpacks team data from the selected team.plist to a sequence of engine commands |
|
44 |
-(void) provideTeamData:(NSString *)teamName forHogs:(NSInteger) numberOfPlayingHogs withHealth:(NSInteger) initialHealth ofColor:(NSNumber *)teamColor { |
|
45 |
/* |
|
46 |
addteam <32charsMD5hash> <color> <team name> |
|
47 |
addhh <level> <health> <hedgehog name> |
|
48 |
<level> is 0 for human, 1-5 for bots (5 is the most stupid) |
|
49 |
*/ |
|
50 |
||
51 |
NSString *teamFile = [[NSString alloc] initWithFormat:@"%@/%@", TEAMS_DIRECTORY(), teamName]; |
|
52 |
NSDictionary *teamData = [[NSDictionary alloc] initWithContentsOfFile:teamFile]; |
|
53 |
[teamFile release]; |
|
54 |
||
55 |
NSString *teamHashColorAndName = [[NSString alloc] initWithFormat:@"eaddteam %@ %@ %@", |
|
3660 | 56 |
[teamData objectForKey:@"hash"], [teamColor stringValue], [teamName stringByDeletingPathExtension]]; |
3547 | 57 |
[self sendToEngine: teamHashColorAndName]; |
58 |
[teamHashColorAndName release]; |
|
59 |
||
60 |
NSString *grave = [[NSString alloc] initWithFormat:@"egrave %@", [teamData objectForKey:@"grave"]]; |
|
61 |
[self sendToEngine: grave]; |
|
62 |
[grave release]; |
|
63 |
||
64 |
NSString *fort = [[NSString alloc] initWithFormat:@"efort %@", [teamData objectForKey:@"fort"]]; |
|
65 |
[self sendToEngine: fort]; |
|
66 |
[fort release]; |
|
67 |
||
68 |
NSString *voicepack = [[NSString alloc] initWithFormat:@"evoicepack %@", [teamData objectForKey:@"voicepack"]]; |
|
69 |
[self sendToEngine: voicepack]; |
|
70 |
[voicepack release]; |
|
71 |
||
72 |
NSString *flag = [[NSString alloc] initWithFormat:@"eflag %@", [teamData objectForKey:@"flag"]]; |
|
73 |
[self sendToEngine: flag]; |
|
74 |
[flag release]; |
|
75 |
||
76 |
NSArray *hogs = [teamData objectForKey:@"hedgehogs"]; |
|
77 |
for (int i = 0; i < numberOfPlayingHogs; i++) { |
|
78 |
NSDictionary *hog = [hogs objectAtIndex:i]; |
|
79 |
||
80 |
NSString *hogLevelHealthAndName = [[NSString alloc] initWithFormat:@"eaddhh %@ %d %@", |
|
81 |
[hog objectForKey:@"level"], initialHealth, [hog objectForKey:@"hogname"]]; |
|
82 |
[self sendToEngine: hogLevelHealthAndName]; |
|
83 |
[hogLevelHealthAndName release]; |
|
84 |
||
85 |
NSString *hogHat = [[NSString alloc] initWithFormat:@"ehat %@", [hog objectForKey:@"hat"]]; |
|
86 |
[self sendToEngine: hogHat]; |
|
87 |
[hogHat release]; |
|
88 |
} |
|
89 |
||
90 |
[teamData release]; |
|
91 |
} |
|
92 |
||
93 |
// unpacks ammostore data from the selected ammo.plist to a sequence of engine commands |
|
94 |
-(void) provideAmmoData:(NSString *)ammostoreName forPlayingTeams:(NSInteger) numberOfTeams { |
|
95 |
NSString *weaponPath = [[NSString alloc] initWithFormat:@"%@/%@",WEAPONS_DIRECTORY(),ammostoreName]; |
|
96 |
NSDictionary *ammoData = [[NSDictionary alloc] initWithContentsOfFile:weaponPath]; |
|
97 |
[weaponPath release]; |
|
3621 | 98 |
NSString *update = @""; |
3547 | 99 |
|
3621 | 100 |
// if we're loading an older version of ammos fill the engine message with 0s |
101 |
int diff = CURRENT_AMMOSIZE - [[ammoData objectForKey:@"version"] intValue]; |
|
102 |
if (diff != 0) |
|
103 |
update = [NSString stringWithCharacters:(const unichar*)"0000000000000000000000000000000000" length:diff]; |
|
104 |
||
105 |
NSString *ammloadt = [[NSString alloc] initWithFormat:@"eammloadt %@%@", [ammoData objectForKey:@"ammostore_initialqt"], update]; |
|
3547 | 106 |
[self sendToEngine: ammloadt]; |
107 |
[ammloadt release]; |
|
108 |
||
3621 | 109 |
NSString *ammprob = [[NSString alloc] initWithFormat:@"eammprob %@%@", [ammoData objectForKey:@"ammostore_probability"], update]; |
3547 | 110 |
[self sendToEngine: ammprob]; |
111 |
[ammprob release]; |
|
112 |
||
3621 | 113 |
NSString *ammdelay = [[NSString alloc] initWithFormat:@"eammdelay %@%@", [ammoData objectForKey:@"ammostore_delay"], update]; |
3547 | 114 |
[self sendToEngine: ammdelay]; |
115 |
[ammdelay release]; |
|
116 |
||
3621 | 117 |
NSString *ammreinf = [[NSString alloc] initWithFormat:@"eammreinf %@%@", [ammoData objectForKey:@"ammostore_crate"], update]; |
3547 | 118 |
[self sendToEngine: ammreinf]; |
119 |
[ammreinf release]; |
|
120 |
||
121 |
// sent twice so it applies to both teams |
|
122 |
NSString *ammstore = [[NSString alloc] initWithString:@"eammstore"]; |
|
123 |
for (int i = 0; i < numberOfTeams; i++) |
|
124 |
[self sendToEngine: ammstore]; |
|
125 |
[ammstore release]; |
|
126 |
||
127 |
[ammoData release]; |
|
128 |
} |
|
129 |
||
130 |
// unpacks scheme data from the selected scheme.plist to a sequence of engine commands |
|
131 |
-(NSInteger) provideScheme:(NSString *)schemeName { |
|
132 |
NSString *schemePath = [[NSString alloc] initWithFormat:@"%@/%@",SCHEMES_DIRECTORY(),schemeName]; |
|
133 |
NSArray *scheme = [[NSArray alloc] initWithContentsOfFile:schemePath]; |
|
134 |
[schemePath release]; |
|
135 |
int result = 0; |
|
136 |
int i = 0; |
|
137 |
||
138 |
if ([[scheme objectAtIndex:i++] boolValue]) |
|
139 |
result |= 0x01; |
|
140 |
if ([[scheme objectAtIndex:i++] boolValue]) |
|
141 |
result |= 0x10; |
|
142 |
if ([[scheme objectAtIndex:i++] boolValue]) |
|
143 |
result |= 0x04; |
|
144 |
if ([[scheme objectAtIndex:i++] boolValue]) |
|
145 |
result |= 0x08; |
|
146 |
if ([[scheme objectAtIndex:i++] boolValue]) |
|
147 |
result |= 0x20; |
|
148 |
if ([[scheme objectAtIndex:i++] boolValue]) |
|
149 |
result |= 0x40; |
|
150 |
if ([[scheme objectAtIndex:i++] boolValue]) |
|
151 |
result |= 0x80; |
|
152 |
if ([[scheme objectAtIndex:i++] boolValue]) |
|
153 |
result |= 0x100; |
|
154 |
if ([[scheme objectAtIndex:i++] boolValue]) |
|
155 |
result |= 0x200; |
|
156 |
if ([[scheme objectAtIndex:i++] boolValue]) |
|
157 |
result |= 0x400; |
|
158 |
if ([[scheme objectAtIndex:i++] boolValue]) |
|
159 |
result |= 0x800; |
|
160 |
if ([[scheme objectAtIndex:i++] boolValue]) |
|
161 |
result |= 0x2000; |
|
162 |
if ([[scheme objectAtIndex:i++] boolValue]) |
|
163 |
result |= 0x4000; |
|
164 |
if ([[scheme objectAtIndex:i++] boolValue]) |
|
165 |
result |= 0x8000; |
|
166 |
if ([[scheme objectAtIndex:i++] boolValue]) |
|
167 |
result |= 0x10000; |
|
168 |
if ([[scheme objectAtIndex:i++] boolValue]) |
|
169 |
result |= 0x20000; |
|
170 |
if ([[scheme objectAtIndex:i++] boolValue]) |
|
171 |
result |= 0x80000; |
|
172 |
||
173 |
NSString *flags = [[NSString alloc] initWithFormat:@"e$gmflags %d",result]; |
|
174 |
[self sendToEngine:flags]; |
|
175 |
[flags release]; |
|
176 |
||
177 |
NSString *dmgMod = [[NSString alloc] initWithFormat:@"e$damagepct %d",[[scheme objectAtIndex:i++] intValue]]; |
|
178 |
[self sendToEngine:dmgMod]; |
|
179 |
[dmgMod release]; |
|
180 |
||
181 |
NSString *turnTime = [[NSString alloc] initWithFormat:@"e$turntime %d",[[scheme objectAtIndex:i++] intValue] * 1000]; |
|
182 |
[self sendToEngine:turnTime]; |
|
183 |
[turnTime release]; |
|
184 |
||
185 |
result = [[scheme objectAtIndex:i++] intValue]; // initial health |
|
186 |
||
187 |
NSString *sdTime = [[NSString alloc] initWithFormat:@"e$sd_turns %d",[[scheme objectAtIndex:i++] intValue]]; |
|
188 |
[self sendToEngine:sdTime]; |
|
189 |
[sdTime release]; |
|
190 |
||
191 |
NSString *crateDrops = [[NSString alloc] initWithFormat:@"e$casefreq %d",[[scheme objectAtIndex:i++] intValue]]; |
|
192 |
[self sendToEngine:crateDrops]; |
|
193 |
[crateDrops release]; |
|
194 |
||
195 |
NSString *minesTime = [[NSString alloc] initWithFormat:@"e$minestime %d",[[scheme objectAtIndex:i++] intValue] * 1000]; |
|
196 |
[self sendToEngine:minesTime]; |
|
197 |
[minesTime release]; |
|
198 |
||
199 |
NSString *minesNumber = [[NSString alloc] initWithFormat:@"e$landadds %d",[[scheme objectAtIndex:i++] intValue]]; |
|
200 |
[self sendToEngine:minesNumber]; |
|
201 |
[minesNumber release]; |
|
202 |
||
203 |
NSString *dudMines = [[NSString alloc] initWithFormat:@"e$minedudpct %d",[[scheme objectAtIndex:i++] intValue]]; |
|
204 |
[self sendToEngine:dudMines]; |
|
205 |
[dudMines release]; |
|
206 |
||
207 |
NSString *explosives = [[NSString alloc] initWithFormat:@"e$explosives %d",[[scheme objectAtIndex:i++] intValue]]; |
|
208 |
[self sendToEngine:explosives]; |
|
209 |
[explosives release]; |
|
210 |
||
211 |
[scheme release]; |
|
212 |
return result; |
|
213 |
} |
|
214 |
||
215 |
#pragma mark - |
|
216 |
#pragma mark Thread/Network relevant code |
|
217 |
// select one of GameSetup method and execute it in a seprate thread |
|
218 |
-(void) startThread: (NSString *) selector { |
|
219 |
SEL usage = NSSelectorFromString(selector); |
|
220 |
[NSThread detachNewThreadSelector:usage toTarget:self withObject:nil]; |
|
221 |
} |
|
222 |
||
223 |
// wrapper that computes the length of the message and then sends the command string |
|
224 |
-(int) sendToEngine: (NSString *)string { |
|
225 |
uint8_t length = [string length]; |
|
226 |
||
227 |
SDLNet_TCP_Send(csd, &length , 1); |
|
228 |
return SDLNet_TCP_Send(csd, [string UTF8String], length); |
|
229 |
} |
|
230 |
||
231 |
// method that handles net setup with engine and keeps connection alive |
|
232 |
-(void) engineProtocol { |
|
233 |
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; |
|
3548
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
234 |
TCPsocket sd; |
3547 | 235 |
IPaddress ip; |
236 |
int eProto; |
|
3548
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
237 |
BOOL clientQuit; |
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
238 |
char buffer[BUFFER_SIZE]; |
3547 | 239 |
uint8_t msgSize; |
240 |
uint16_t gameTicks; |
|
3548
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
241 |
|
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
242 |
clientQuit = NO; |
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
243 |
csd = NULL; |
3547 | 244 |
|
245 |
if (SDLNet_Init() < 0) { |
|
246 |
DLog(@"SDLNet_Init: %s", SDLNet_GetError()); |
|
3548
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
247 |
clientQuit = YES; |
3547 | 248 |
} |
249 |
||
250 |
// Resolving the host using NULL make network interface to listen |
|
3548
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
251 |
if (SDLNet_ResolveHost(&ip, NULL, ipcPort) < 0 && !clientQuit) { |
3547 | 252 |
DLog(@"SDLNet_ResolveHost: %s\n", SDLNet_GetError()); |
3548
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
253 |
clientQuit = YES; |
3547 | 254 |
} |
255 |
||
256 |
// Open a connection with the IP provided (listen on the host's port) |
|
3548
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
257 |
if (!(sd = SDLNet_TCP_Open(&ip)) && !clientQuit) { |
3547 | 258 |
DLog(@"SDLNet_TCP_Open: %s %\n", SDLNet_GetError(), ipcPort); |
3548
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
259 |
clientQuit = YES; |
3547 | 260 |
} |
261 |
||
262 |
DLog(@"Waiting for a client on port %d", ipcPort); |
|
3548
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
263 |
while (csd == NULL) |
3547 | 264 |
csd = SDLNet_TCP_Accept(sd); |
3548
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
265 |
SDLNet_TCP_Close(sd); |
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
266 |
|
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
267 |
while (!clientQuit) { |
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
268 |
msgSize = 0; |
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
269 |
memset(buffer, 0, BUFFER_SIZE); |
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
270 |
if (SDLNet_TCP_Recv(csd, &msgSize, sizeof(uint8_t)) <= 0) |
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
271 |
clientQuit = YES; |
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
272 |
if (SDLNet_TCP_Recv(csd, buffer, msgSize) <=0) |
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
273 |
clientQuit = YES; |
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
274 |
|
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
275 |
switch (buffer[0]) { |
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
276 |
case 'C': |
3626 | 277 |
DLog(@"sending game config...\n%@",self.gameConfig); |
3547 | 278 |
|
279 |
// local game |
|
280 |
[self sendToEngine:@"TL"]; |
|
281 |
||
282 |
// seed info |
|
283 |
[self sendToEngine:[self.gameConfig objectForKey:@"seed_command"]]; |
|
3548
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
284 |
|
3547 | 285 |
// dimension of the map |
286 |
[self sendToEngine:[self.gameConfig objectForKey:@"templatefilter_command"]]; |
|
287 |
[self sendToEngine:[self.gameConfig objectForKey:@"mapgen_command"]]; |
|
288 |
[self sendToEngine:[self.gameConfig objectForKey:@"mazesize_command"]]; |
|
3548
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
289 |
|
3642 | 290 |
// static land (if set) |
291 |
NSString *staticMap = [self.gameConfig objectForKey:@"staticmap_command"]; |
|
292 |
if ([staticMap length] != 0) |
|
293 |
[self sendToEngine:staticMap]; |
|
294 |
||
3547 | 295 |
// theme info |
296 |
[self sendToEngine:[self.gameConfig objectForKey:@"theme_command"]]; |
|
297 |
||
298 |
// scheme (returns initial health) |
|
299 |
NSInteger health = [self provideScheme:[self.gameConfig objectForKey:@"scheme"]]; |
|
300 |
||
301 |
NSArray *teamsConfig = [self.gameConfig objectForKey:@"teams_list"]; |
|
302 |
for (NSDictionary *teamData in teamsConfig) { |
|
303 |
[self provideTeamData:[teamData objectForKey:@"team"] |
|
304 |
forHogs:[[teamData objectForKey:@"number"] intValue] |
|
305 |
withHealth:health |
|
306 |
ofColor:[teamData objectForKey:@"color"]]; |
|
307 |
} |
|
308 |
||
3548
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
309 |
[self provideAmmoData:[self.gameConfig objectForKey:@"weapon"] forPlayingTeams:[teamsConfig count]]; |
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
310 |
break; |
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
311 |
case '?': |
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
312 |
DLog(@"Ping? Pong!"); |
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
313 |
[self sendToEngine:@"!"]; |
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
314 |
break; |
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
315 |
case 'E': |
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
316 |
DLog(@"ERROR - last console line: [%s]", &buffer[1]); |
3547 | 317 |
clientQuit = YES; |
3548
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
318 |
break; |
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
319 |
case 'e': |
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
320 |
sscanf(buffer, "%*s %d", &eProto); |
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
321 |
short int netProto = 0; |
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
322 |
char *versionStr; |
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
323 |
|
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
324 |
HW_versionInfo(&netProto, &versionStr); |
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
325 |
if (netProto == eProto) { |
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
326 |
DLog(@"Setting protocol version %d (%s)", eProto, versionStr); |
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
327 |
} else { |
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
328 |
DLog(@"ERROR - wrong protocol number: [%s] - expecting %d", &buffer[1], eProto); |
3547 | 329 |
clientQuit = YES; |
3548
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
330 |
} |
3547 | 331 |
|
3548
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
332 |
break; |
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
333 |
case 'i': |
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
334 |
switch (buffer[1]) { |
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
335 |
case 'r': |
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
336 |
NSLog(@"Winning team: %s", &buffer[2]); |
3547 | 337 |
break; |
3548
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
338 |
case 'k': |
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
339 |
NSLog(@"Best Hedgehog: %s", &buffer[2]); |
3547 | 340 |
break; |
341 |
} |
|
3548
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
342 |
break; |
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
343 |
default: |
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
344 |
// empty packet or just statistics -- in either cases gameTicks is sent |
3616
85d69ddb41b6
tackling the iphoneos todo (another trial for the beginning sporadic bug, remove curl animation, don't need to go to detail page)
koda
parents:
3613
diff
changeset
|
345 |
gameTicks = SDLNet_Read16 (&buffer[msgSize - 2]); |
3548
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
346 |
//DLog(@"engineProtocol - %d: received [%s]", gameTicks, buffer); |
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
347 |
break; |
3547 | 348 |
} |
349 |
} |
|
3548
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
350 |
DLog(@"Engine exited, closing server"); |
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
351 |
// wait a little to let the client close cleanly |
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
352 |
[NSThread sleepForTimeInterval:2]; |
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
353 |
// Close the client socket |
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
354 |
SDLNet_TCP_Close(csd); |
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
355 |
SDLNet_Quit(); |
3616
85d69ddb41b6
tackling the iphoneos todo (another trial for the beginning sporadic bug, remove curl animation, don't need to go to detail page)
koda
parents:
3613
diff
changeset
|
356 |
|
3547 | 357 |
[pool release]; |
358 |
//Invoking this method should be avoided as it does not give your thread a chance to clean up any resources it allocated during its execution. |
|
359 |
//[NSThread exit]; |
|
360 |
} |
|
361 |
||
362 |
#pragma mark - |
|
363 |
#pragma mark Setting methods |
|
364 |
// returns an array of c-strings that are read by engine at startup |
|
365 |
-(const char **)getSettings { |
|
366 |
NSString *ipcString = [[NSString alloc] initWithFormat:@"%d", ipcPort]; |
|
367 |
NSString *localeString = [[NSString alloc] initWithFormat:@"%@.txt", [[NSLocale currentLocale] objectForKey:NSLocaleLanguageCode]]; |
|
368 |
CGRect screenBounds = [[UIScreen mainScreen] bounds]; |
|
369 |
NSString *wSize = [[NSString alloc] initWithFormat:@"%d", (int) screenBounds.size.width]; |
|
370 |
NSString *hSize = [[NSString alloc] initWithFormat:@"%d", (int) screenBounds.size.height]; |
|
3613 | 371 |
const char **gameArgs = (const char**) malloc(sizeof(char *) * 10); |
3634 | 372 |
NSInteger tmpQuality; |
373 |
||
3670
4c673e57f0d7
use llvm to compile, don't preview map on wimpier devices, merge vsync, fix iphone launch image
koda
parents:
3660
diff
changeset
|
374 |
NSString *modelId = modelType(); |
3613 | 375 |
if ([modelId hasPrefix:@"iPhone1"] || // = iPhone or iPhone 3G |
376 |
[modelId hasPrefix:@"iPod1,1"] || [modelId hasPrefix:@"iPod2,1"]) // = iPod Touch or iPod Touch 2G |
|
3634 | 377 |
tmpQuality = 0x00000001 | 0x00000002 | 0x00000040; // rqLowRes | rqBlurryLand | rqKillFlakes |
3613 | 378 |
else if ([modelId hasPrefix:@"iPhone2"] || // = iPhone 3GS |
379 |
[modelId hasPrefix:@"iPod3"]) // = iPod Touch 3G |
|
3634 | 380 |
tmpQuality = 0x00000002 | 0x00000040; // rqBlurryLand | rqKillFlakes |
3624 | 381 |
else if ([modelId hasPrefix:@"iPad1"]) // = iPad |
3634 | 382 |
tmpQuality = 0x00000002; // rqBlurryLand |
3624 | 383 |
else // = everything else |
3634 | 384 |
tmpQuality = 0; // full quality |
385 |
if (![modelId hasPrefix:@"iPad"]) // = disable tooltips unless iPad |
|
386 |
tmpQuality = tmpQuality | 0x00000400; |
|
3613 | 387 |
|
3634 | 388 |
gameArgs[9] = [[[NSNumber numberWithInteger:tmpQuality] stringValue] UTF8String]; |
389 |
||
3547 | 390 |
// prevents using an empty nickname |
391 |
NSString *username; |
|
392 |
NSString *originalUsername = [self.systemSettings objectForKey:@"username"]; |
|
393 |
if ([originalUsername length] == 0) |
|
394 |
username = [[NSString alloc] initWithFormat:@"MobileUser-%@",ipcString]; |
|
395 |
else |
|
396 |
username = [[NSString alloc] initWithString:originalUsername]; |
|
397 |
||
398 |
gameArgs[0] = [username UTF8String]; //UserNick |
|
399 |
gameArgs[1] = [ipcString UTF8String]; //ipcPort |
|
400 |
gameArgs[2] = [[[self.systemSettings objectForKey:@"sound"] stringValue] UTF8String]; //isSoundEnabled |
|
401 |
gameArgs[3] = [[[self.systemSettings objectForKey:@"music"] stringValue] UTF8String]; //isMusicEnabled |
|
402 |
gameArgs[4] = [localeString UTF8String]; //cLocaleFName |
|
403 |
gameArgs[5] = [[[self.systemSettings objectForKey:@"alternate"] stringValue] UTF8String]; //cAltDamage |
|
404 |
gameArgs[6] = [wSize UTF8String]; //cScreenHeight |
|
405 |
gameArgs[7] = [hSize UTF8String]; //cScreenWidth |
|
406 |
gameArgs[8] = NULL; //recordFileName |
|
407 |
||
408 |
[wSize release]; |
|
409 |
[hSize release]; |
|
410 |
[localeString release]; |
|
411 |
[ipcString release]; |
|
412 |
[username release]; |
|
413 |
return gameArgs; |
|
414 |
} |
|
415 |
||
416 |
||
417 |
@end |