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