author | koda |
Thu, 02 Sep 2010 01:49:16 +0200 | |
changeset 3803 | e4a8a4e378de |
parent 3781 | 2bfda544ae48 |
child 3825 | fd6c20cd90e3 |
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(); |
|
3697 | 24 |
|
3547 | 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]; |
|
3697 | 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; |
3697 | 31 |
} |
3547 | 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 |
*/ |
|
3697 | 50 |
|
3547 | 51 |
NSString *teamFile = [[NSString alloc] initWithFormat:@"%@/%@", TEAMS_DIRECTORY(), teamName]; |
52 |
NSDictionary *teamData = [[NSDictionary alloc] initWithContentsOfFile:teamFile]; |
|
53 |
[teamFile release]; |
|
3697 | 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]; |
|
3697 | 59 |
|
3547 | 60 |
NSString *grave = [[NSString alloc] initWithFormat:@"egrave %@", [teamData objectForKey:@"grave"]]; |
61 |
[self sendToEngine: grave]; |
|
62 |
[grave release]; |
|
3697 | 63 |
|
3547 | 64 |
NSString *fort = [[NSString alloc] initWithFormat:@"efort %@", [teamData objectForKey:@"fort"]]; |
65 |
[self sendToEngine: fort]; |
|
66 |
[fort release]; |
|
3697 | 67 |
|
3547 | 68 |
NSString *voicepack = [[NSString alloc] initWithFormat:@"evoicepack %@", [teamData objectForKey:@"voicepack"]]; |
69 |
[self sendToEngine: voicepack]; |
|
70 |
[voicepack release]; |
|
3697 | 71 |
|
3547 | 72 |
NSString *flag = [[NSString alloc] initWithFormat:@"eflag %@", [teamData objectForKey:@"flag"]]; |
73 |
[self sendToEngine: flag]; |
|
74 |
[flag release]; |
|
3697 | 75 |
|
3547 | 76 |
NSArray *hogs = [teamData objectForKey:@"hedgehogs"]; |
77 |
for (int i = 0; i < numberOfPlayingHogs; i++) { |
|
78 |
NSDictionary *hog = [hogs objectAtIndex:i]; |
|
3697 | 79 |
|
80 |
NSString *hogLevelHealthAndName = [[NSString alloc] initWithFormat:@"eaddhh %@ %d %@", |
|
3547 | 81 |
[hog objectForKey:@"level"], initialHealth, [hog objectForKey:@"hogname"]]; |
82 |
[self sendToEngine: hogLevelHealthAndName]; |
|
83 |
[hogLevelHealthAndName release]; |
|
3697 | 84 |
|
3547 | 85 |
NSString *hogHat = [[NSString alloc] initWithFormat:@"ehat %@", [hog objectForKey:@"hat"]]; |
86 |
[self sendToEngine: hogHat]; |
|
87 |
[hogHat release]; |
|
88 |
} |
|
3697 | 89 |
|
3547 | 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 = @""; |
3697 | 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]; |
|
3697 | 104 |
|
3621 | 105 |
NSString *ammloadt = [[NSString alloc] initWithFormat:@"eammloadt %@%@", [ammoData objectForKey:@"ammostore_initialqt"], update]; |
3547 | 106 |
[self sendToEngine: ammloadt]; |
107 |
[ammloadt release]; |
|
3697 | 108 |
|
3621 | 109 |
NSString *ammprob = [[NSString alloc] initWithFormat:@"eammprob %@%@", [ammoData objectForKey:@"ammostore_probability"], update]; |
3547 | 110 |
[self sendToEngine: ammprob]; |
111 |
[ammprob release]; |
|
3697 | 112 |
|
3621 | 113 |
NSString *ammdelay = [[NSString alloc] initWithFormat:@"eammdelay %@%@", [ammoData objectForKey:@"ammostore_delay"], update]; |
3547 | 114 |
[self sendToEngine: ammdelay]; |
115 |
[ammdelay release]; |
|
3697 | 116 |
|
3621 | 117 |
NSString *ammreinf = [[NSString alloc] initWithFormat:@"eammreinf %@%@", [ammoData objectForKey:@"ammostore_crate"], update]; |
3547 | 118 |
[self sendToEngine: ammreinf]; |
119 |
[ammreinf release]; |
|
3697 | 120 |
|
3547 | 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]; |
|
3697 | 126 |
|
3547 | 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]; |
|
3781
2bfda544ae48
modified file format for schemes files (which is going to introduce a lot of pre-release bugs, i'm sure)
koda
parents:
3779
diff
changeset
|
133 |
NSDictionary *schemeDictionary = [[NSDictionary alloc] initWithContentsOfFile:schemePath]; |
3547 | 134 |
[schemePath release]; |
3781
2bfda544ae48
modified file format for schemes files (which is going to introduce a lot of pre-release bugs, i'm sure)
koda
parents:
3779
diff
changeset
|
135 |
NSArray *basicArray = [schemeDictionary objectForKey:@"basic"]; |
2bfda544ae48
modified file format for schemes files (which is going to introduce a lot of pre-release bugs, i'm sure)
koda
parents:
3779
diff
changeset
|
136 |
NSArray *gamemodArray = [schemeDictionary objectForKey:@"gamemod"]; |
3547 | 137 |
int result = 0; |
138 |
int i = 0; |
|
139 |
||
3781
2bfda544ae48
modified file format for schemes files (which is going to introduce a lot of pre-release bugs, i'm sure)
koda
parents:
3779
diff
changeset
|
140 |
if ([[gamemodArray objectAtIndex:i++] boolValue]) |
3752
73c2d7d5643b
add the new flag, fix a couple of hicups in creating config files, swap animation between settings and lobby
koda
parents:
3697
diff
changeset
|
141 |
result |= 0x00000001; |
3781
2bfda544ae48
modified file format for schemes files (which is going to introduce a lot of pre-release bugs, i'm sure)
koda
parents:
3779
diff
changeset
|
142 |
if ([[gamemodArray objectAtIndex:i++] boolValue]) |
3752
73c2d7d5643b
add the new flag, fix a couple of hicups in creating config files, swap animation between settings and lobby
koda
parents:
3697
diff
changeset
|
143 |
result |= 0x00000010; |
3781
2bfda544ae48
modified file format for schemes files (which is going to introduce a lot of pre-release bugs, i'm sure)
koda
parents:
3779
diff
changeset
|
144 |
if ([[gamemodArray objectAtIndex:i++] boolValue]) |
3752
73c2d7d5643b
add the new flag, fix a couple of hicups in creating config files, swap animation between settings and lobby
koda
parents:
3697
diff
changeset
|
145 |
result |= 0x00000004; |
3781
2bfda544ae48
modified file format for schemes files (which is going to introduce a lot of pre-release bugs, i'm sure)
koda
parents:
3779
diff
changeset
|
146 |
if ([[gamemodArray objectAtIndex:i++] boolValue]) |
3752
73c2d7d5643b
add the new flag, fix a couple of hicups in creating config files, swap animation between settings and lobby
koda
parents:
3697
diff
changeset
|
147 |
result |= 0x00000008; |
3781
2bfda544ae48
modified file format for schemes files (which is going to introduce a lot of pre-release bugs, i'm sure)
koda
parents:
3779
diff
changeset
|
148 |
if ([[gamemodArray objectAtIndex:i++] boolValue]) |
3752
73c2d7d5643b
add the new flag, fix a couple of hicups in creating config files, swap animation between settings and lobby
koda
parents:
3697
diff
changeset
|
149 |
result |= 0x00000020; |
3781
2bfda544ae48
modified file format for schemes files (which is going to introduce a lot of pre-release bugs, i'm sure)
koda
parents:
3779
diff
changeset
|
150 |
if ([[gamemodArray objectAtIndex:i++] boolValue]) |
3752
73c2d7d5643b
add the new flag, fix a couple of hicups in creating config files, swap animation between settings and lobby
koda
parents:
3697
diff
changeset
|
151 |
result |= 0x00000040; |
3781
2bfda544ae48
modified file format for schemes files (which is going to introduce a lot of pre-release bugs, i'm sure)
koda
parents:
3779
diff
changeset
|
152 |
if ([[gamemodArray objectAtIndex:i++] boolValue]) |
3752
73c2d7d5643b
add the new flag, fix a couple of hicups in creating config files, swap animation between settings and lobby
koda
parents:
3697
diff
changeset
|
153 |
result |= 0x00000080; |
3781
2bfda544ae48
modified file format for schemes files (which is going to introduce a lot of pre-release bugs, i'm sure)
koda
parents:
3779
diff
changeset
|
154 |
if ([[gamemodArray objectAtIndex:i++] boolValue]) |
3752
73c2d7d5643b
add the new flag, fix a couple of hicups in creating config files, swap animation between settings and lobby
koda
parents:
3697
diff
changeset
|
155 |
result |= 0x00000100; |
3781
2bfda544ae48
modified file format for schemes files (which is going to introduce a lot of pre-release bugs, i'm sure)
koda
parents:
3779
diff
changeset
|
156 |
if ([[gamemodArray objectAtIndex:i++] boolValue]) |
3752
73c2d7d5643b
add the new flag, fix a couple of hicups in creating config files, swap animation between settings and lobby
koda
parents:
3697
diff
changeset
|
157 |
result |= 0x00000200; |
3781
2bfda544ae48
modified file format for schemes files (which is going to introduce a lot of pre-release bugs, i'm sure)
koda
parents:
3779
diff
changeset
|
158 |
if ([[gamemodArray objectAtIndex:i++] boolValue]) |
3752
73c2d7d5643b
add the new flag, fix a couple of hicups in creating config files, swap animation between settings and lobby
koda
parents:
3697
diff
changeset
|
159 |
result |= 0x00000400; |
3781
2bfda544ae48
modified file format for schemes files (which is going to introduce a lot of pre-release bugs, i'm sure)
koda
parents:
3779
diff
changeset
|
160 |
if ([[gamemodArray objectAtIndex:i++] boolValue]) |
3752
73c2d7d5643b
add the new flag, fix a couple of hicups in creating config files, swap animation between settings and lobby
koda
parents:
3697
diff
changeset
|
161 |
result |= 0x00000800; |
3781
2bfda544ae48
modified file format for schemes files (which is going to introduce a lot of pre-release bugs, i'm sure)
koda
parents:
3779
diff
changeset
|
162 |
if ([[gamemodArray objectAtIndex:i++] boolValue]) |
3752
73c2d7d5643b
add the new flag, fix a couple of hicups in creating config files, swap animation between settings and lobby
koda
parents:
3697
diff
changeset
|
163 |
result |= 0x00002000; |
3781
2bfda544ae48
modified file format for schemes files (which is going to introduce a lot of pre-release bugs, i'm sure)
koda
parents:
3779
diff
changeset
|
164 |
if ([[gamemodArray objectAtIndex:i++] boolValue]) |
3752
73c2d7d5643b
add the new flag, fix a couple of hicups in creating config files, swap animation between settings and lobby
koda
parents:
3697
diff
changeset
|
165 |
result |= 0x00004000; |
3781
2bfda544ae48
modified file format for schemes files (which is going to introduce a lot of pre-release bugs, i'm sure)
koda
parents:
3779
diff
changeset
|
166 |
if ([[gamemodArray objectAtIndex:i++] boolValue]) |
3752
73c2d7d5643b
add the new flag, fix a couple of hicups in creating config files, swap animation between settings and lobby
koda
parents:
3697
diff
changeset
|
167 |
result |= 0x00008000; |
3781
2bfda544ae48
modified file format for schemes files (which is going to introduce a lot of pre-release bugs, i'm sure)
koda
parents:
3779
diff
changeset
|
168 |
if ([[gamemodArray objectAtIndex:i++] boolValue]) |
3752
73c2d7d5643b
add the new flag, fix a couple of hicups in creating config files, swap animation between settings and lobby
koda
parents:
3697
diff
changeset
|
169 |
result |= 0x00010000; |
3781
2bfda544ae48
modified file format for schemes files (which is going to introduce a lot of pre-release bugs, i'm sure)
koda
parents:
3779
diff
changeset
|
170 |
if ([[gamemodArray objectAtIndex:i++] boolValue]) |
3752
73c2d7d5643b
add the new flag, fix a couple of hicups in creating config files, swap animation between settings and lobby
koda
parents:
3697
diff
changeset
|
171 |
result |= 0x00020000; |
3781
2bfda544ae48
modified file format for schemes files (which is going to introduce a lot of pre-release bugs, i'm sure)
koda
parents:
3779
diff
changeset
|
172 |
if ([[gamemodArray objectAtIndex:i++] boolValue]) |
3752
73c2d7d5643b
add the new flag, fix a couple of hicups in creating config files, swap animation between settings and lobby
koda
parents:
3697
diff
changeset
|
173 |
result |= 0x00080000; |
3781
2bfda544ae48
modified file format for schemes files (which is going to introduce a lot of pre-release bugs, i'm sure)
koda
parents:
3779
diff
changeset
|
174 |
if ([[gamemodArray objectAtIndex:i++] boolValue]) |
2bfda544ae48
modified file format for schemes files (which is going to introduce a lot of pre-release bugs, i'm sure)
koda
parents:
3779
diff
changeset
|
175 |
result |= 0x00100000; |
3547 | 176 |
|
177 |
NSString *flags = [[NSString alloc] initWithFormat:@"e$gmflags %d",result]; |
|
178 |
[self sendToEngine:flags]; |
|
179 |
[flags release]; |
|
3697 | 180 |
|
3781
2bfda544ae48
modified file format for schemes files (which is going to introduce a lot of pre-release bugs, i'm sure)
koda
parents:
3779
diff
changeset
|
181 |
i = 0; |
2bfda544ae48
modified file format for schemes files (which is going to introduce a lot of pre-release bugs, i'm sure)
koda
parents:
3779
diff
changeset
|
182 |
NSString *dmgMod = [[NSString alloc] initWithFormat:@"e$damagepct %d",[[basicArray objectAtIndex:i++] intValue]]; |
3547 | 183 |
[self sendToEngine:dmgMod]; |
184 |
[dmgMod release]; |
|
3697 | 185 |
|
3781
2bfda544ae48
modified file format for schemes files (which is going to introduce a lot of pre-release bugs, i'm sure)
koda
parents:
3779
diff
changeset
|
186 |
NSString *turnTime = [[NSString alloc] initWithFormat:@"e$turntime %d",[[basicArray objectAtIndex:i++] intValue] * 1000]; |
3547 | 187 |
[self sendToEngine:turnTime]; |
188 |
[turnTime release]; |
|
3697 | 189 |
|
3781
2bfda544ae48
modified file format for schemes files (which is going to introduce a lot of pre-release bugs, i'm sure)
koda
parents:
3779
diff
changeset
|
190 |
result = [[basicArray objectAtIndex:i++] intValue]; // initial health |
3697 | 191 |
|
3781
2bfda544ae48
modified file format for schemes files (which is going to introduce a lot of pre-release bugs, i'm sure)
koda
parents:
3779
diff
changeset
|
192 |
NSString *sdTime = [[NSString alloc] initWithFormat:@"e$sd_turns %d",[[basicArray objectAtIndex:i++] intValue]]; |
3547 | 193 |
[self sendToEngine:sdTime]; |
194 |
[sdTime release]; |
|
3697 | 195 |
|
3781
2bfda544ae48
modified file format for schemes files (which is going to introduce a lot of pre-release bugs, i'm sure)
koda
parents:
3779
diff
changeset
|
196 |
NSString *crateDrops = [[NSString alloc] initWithFormat:@"e$casefreq %d",[[basicArray objectAtIndex:i++] intValue]]; |
3547 | 197 |
[self sendToEngine:crateDrops]; |
198 |
[crateDrops release]; |
|
3697 | 199 |
|
3781
2bfda544ae48
modified file format for schemes files (which is going to introduce a lot of pre-release bugs, i'm sure)
koda
parents:
3779
diff
changeset
|
200 |
NSString *minesTime = [[NSString alloc] initWithFormat:@"e$minestime %d",[[basicArray objectAtIndex:i++] intValue] * 1000]; |
3547 | 201 |
[self sendToEngine:minesTime]; |
202 |
[minesTime release]; |
|
3697 | 203 |
|
3781
2bfda544ae48
modified file format for schemes files (which is going to introduce a lot of pre-release bugs, i'm sure)
koda
parents:
3779
diff
changeset
|
204 |
NSString *minesNumber = [[NSString alloc] initWithFormat:@"e$landadds %d",[[basicArray objectAtIndex:i++] intValue]]; |
3547 | 205 |
[self sendToEngine:minesNumber]; |
206 |
[minesNumber release]; |
|
3697 | 207 |
|
3781
2bfda544ae48
modified file format for schemes files (which is going to introduce a lot of pre-release bugs, i'm sure)
koda
parents:
3779
diff
changeset
|
208 |
NSString *dudMines = [[NSString alloc] initWithFormat:@"e$minedudpct %d",[[basicArray objectAtIndex:i++] intValue]]; |
3547 | 209 |
[self sendToEngine:dudMines]; |
210 |
[dudMines release]; |
|
3697 | 211 |
|
3781
2bfda544ae48
modified file format for schemes files (which is going to introduce a lot of pre-release bugs, i'm sure)
koda
parents:
3779
diff
changeset
|
212 |
NSString *explosives = [[NSString alloc] initWithFormat:@"e$explosives %d",[[basicArray objectAtIndex:i++] intValue]]; |
3547 | 213 |
[self sendToEngine:explosives]; |
214 |
[explosives release]; |
|
3697 | 215 |
|
3781
2bfda544ae48
modified file format for schemes files (which is going to introduce a lot of pre-release bugs, i'm sure)
koda
parents:
3779
diff
changeset
|
216 |
[schemeDictionary release]; |
3547 | 217 |
return result; |
218 |
} |
|
219 |
||
220 |
#pragma mark - |
|
221 |
#pragma mark Thread/Network relevant code |
|
222 |
// select one of GameSetup method and execute it in a seprate thread |
|
223 |
-(void) startThread: (NSString *) selector { |
|
224 |
SEL usage = NSSelectorFromString(selector); |
|
225 |
[NSThread detachNewThreadSelector:usage toTarget:self withObject:nil]; |
|
226 |
} |
|
227 |
||
228 |
// wrapper that computes the length of the message and then sends the command string |
|
229 |
-(int) sendToEngine: (NSString *)string { |
|
230 |
uint8_t length = [string length]; |
|
3697 | 231 |
|
3547 | 232 |
SDLNet_TCP_Send(csd, &length , 1); |
233 |
return SDLNet_TCP_Send(csd, [string UTF8String], length); |
|
234 |
} |
|
235 |
||
236 |
// method that handles net setup with engine and keeps connection alive |
|
237 |
-(void) engineProtocol { |
|
238 |
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; |
|
3548
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
239 |
TCPsocket sd; |
3547 | 240 |
IPaddress ip; |
241 |
int eProto; |
|
3548
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
242 |
BOOL clientQuit; |
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
243 |
char buffer[BUFFER_SIZE]; |
3547 | 244 |
uint8_t msgSize; |
245 |
uint16_t gameTicks; |
|
3697 | 246 |
|
3548
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
247 |
clientQuit = NO; |
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
248 |
csd = NULL; |
3547 | 249 |
|
250 |
if (SDLNet_Init() < 0) { |
|
251 |
DLog(@"SDLNet_Init: %s", SDLNet_GetError()); |
|
3548
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
252 |
clientQuit = YES; |
3547 | 253 |
} |
3697 | 254 |
|
3547 | 255 |
// Resolving the host using NULL make network interface to listen |
3548
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
256 |
if (SDLNet_ResolveHost(&ip, NULL, ipcPort) < 0 && !clientQuit) { |
3547 | 257 |
DLog(@"SDLNet_ResolveHost: %s\n", SDLNet_GetError()); |
3548
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
258 |
clientQuit = YES; |
3547 | 259 |
} |
3697 | 260 |
|
261 |
// 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
|
262 |
if (!(sd = SDLNet_TCP_Open(&ip)) && !clientQuit) { |
3547 | 263 |
DLog(@"SDLNet_TCP_Open: %s %\n", SDLNet_GetError(), ipcPort); |
3548
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
264 |
clientQuit = YES; |
3547 | 265 |
} |
3697 | 266 |
|
3547 | 267 |
DLog(@"Waiting for a client on port %d", ipcPort); |
3697 | 268 |
while (csd == NULL) |
3547 | 269 |
csd = SDLNet_TCP_Accept(sd); |
3548
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
270 |
SDLNet_TCP_Close(sd); |
3697 | 271 |
|
3548
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
272 |
while (!clientQuit) { |
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
273 |
msgSize = 0; |
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
274 |
memset(buffer, 0, BUFFER_SIZE); |
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
275 |
if (SDLNet_TCP_Recv(csd, &msgSize, sizeof(uint8_t)) <= 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 |
if (SDLNet_TCP_Recv(csd, buffer, msgSize) <=0) |
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
278 |
clientQuit = YES; |
3697 | 279 |
|
3548
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
280 |
switch (buffer[0]) { |
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
281 |
case 'C': |
3626 | 282 |
DLog(@"sending game config...\n%@",self.gameConfig); |
3697 | 283 |
|
3547 | 284 |
// local game |
285 |
[self sendToEngine:@"TL"]; |
|
3697 | 286 |
|
3547 | 287 |
// seed info |
288 |
[self sendToEngine:[self.gameConfig objectForKey:@"seed_command"]]; |
|
3697 | 289 |
|
3547 | 290 |
// dimension of the map |
291 |
[self sendToEngine:[self.gameConfig objectForKey:@"templatefilter_command"]]; |
|
292 |
[self sendToEngine:[self.gameConfig objectForKey:@"mapgen_command"]]; |
|
293 |
[self sendToEngine:[self.gameConfig objectForKey:@"mazesize_command"]]; |
|
3697 | 294 |
|
3642 | 295 |
// static land (if set) |
296 |
NSString *staticMap = [self.gameConfig objectForKey:@"staticmap_command"]; |
|
297 |
if ([staticMap length] != 0) |
|
298 |
[self sendToEngine:staticMap]; |
|
3697 | 299 |
|
3547 | 300 |
// theme info |
301 |
[self sendToEngine:[self.gameConfig objectForKey:@"theme_command"]]; |
|
3697 | 302 |
|
3547 | 303 |
// scheme (returns initial health) |
304 |
NSInteger health = [self provideScheme:[self.gameConfig objectForKey:@"scheme"]]; |
|
3697 | 305 |
|
3547 | 306 |
NSArray *teamsConfig = [self.gameConfig objectForKey:@"teams_list"]; |
307 |
for (NSDictionary *teamData in teamsConfig) { |
|
3697 | 308 |
[self provideTeamData:[teamData objectForKey:@"team"] |
3547 | 309 |
forHogs:[[teamData objectForKey:@"number"] intValue] |
310 |
withHealth:health |
|
311 |
ofColor:[teamData objectForKey:@"color"]]; |
|
312 |
} |
|
3697 | 313 |
|
3548
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
314 |
[self provideAmmoData:[self.gameConfig objectForKey:@"weapon"] forPlayingTeams:[teamsConfig count]]; |
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
315 |
break; |
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
316 |
case '?': |
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
317 |
DLog(@"Ping? Pong!"); |
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
318 |
[self sendToEngine:@"!"]; |
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
319 |
break; |
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
320 |
case 'E': |
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
321 |
DLog(@"ERROR - last console line: [%s]", &buffer[1]); |
3547 | 322 |
clientQuit = YES; |
3548
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
323 |
break; |
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
324 |
case 'e': |
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
325 |
sscanf(buffer, "%*s %d", &eProto); |
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
326 |
short int netProto = 0; |
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
327 |
char *versionStr; |
3697 | 328 |
|
3548
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
329 |
HW_versionInfo(&netProto, &versionStr); |
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
330 |
if (netProto == eProto) { |
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
331 |
DLog(@"Setting protocol version %d (%s)", eProto, versionStr); |
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
332 |
} else { |
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
333 |
DLog(@"ERROR - wrong protocol number: [%s] - expecting %d", &buffer[1], eProto); |
3547 | 334 |
clientQuit = YES; |
3548
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
335 |
} |
3697 | 336 |
|
3548
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
337 |
break; |
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
338 |
case 'i': |
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
339 |
switch (buffer[1]) { |
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
340 |
case 'r': |
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
341 |
NSLog(@"Winning team: %s", &buffer[2]); |
3547 | 342 |
break; |
3548
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
343 |
case 'k': |
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
344 |
NSLog(@"Best Hedgehog: %s", &buffer[2]); |
3547 | 345 |
break; |
346 |
} |
|
3548
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
347 |
break; |
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
348 |
default: |
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
349 |
// 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
|
350 |
gameTicks = SDLNet_Read16 (&buffer[msgSize - 2]); |
3548
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
351 |
//DLog(@"engineProtocol - %d: received [%s]", gameTicks, buffer); |
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
352 |
break; |
3547 | 353 |
} |
354 |
} |
|
3548
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
355 |
DLog(@"Engine exited, closing server"); |
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
356 |
// wait a little to let the client close cleanly |
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
357 |
[NSThread sleepForTimeInterval:2]; |
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
358 |
// Close the client socket |
3697 | 359 |
SDLNet_TCP_Close(csd); |
3548
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
360 |
SDLNet_Quit(); |
3697 | 361 |
|
3547 | 362 |
[pool release]; |
363 |
//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. |
|
364 |
//[NSThread exit]; |
|
365 |
} |
|
366 |
||
367 |
#pragma mark - |
|
368 |
#pragma mark Setting methods |
|
369 |
// returns an array of c-strings that are read by engine at startup |
|
370 |
-(const char **)getSettings { |
|
371 |
NSString *ipcString = [[NSString alloc] initWithFormat:@"%d", ipcPort]; |
|
372 |
NSString *localeString = [[NSString alloc] initWithFormat:@"%@.txt", [[NSLocale currentLocale] objectForKey:NSLocaleLanguageCode]]; |
|
373 |
CGRect screenBounds = [[UIScreen mainScreen] bounds]; |
|
374 |
NSString *wSize = [[NSString alloc] initWithFormat:@"%d", (int) screenBounds.size.width]; |
|
375 |
NSString *hSize = [[NSString alloc] initWithFormat:@"%d", (int) screenBounds.size.height]; |
|
3613 | 376 |
const char **gameArgs = (const char**) malloc(sizeof(char *) * 10); |
3634 | 377 |
NSInteger tmpQuality; |
3697 | 378 |
|
3670
4c673e57f0d7
use llvm to compile, don't preview map on wimpier devices, merge vsync, fix iphone launch image
koda
parents:
3660
diff
changeset
|
379 |
NSString *modelId = modelType(); |
3613 | 380 |
if ([modelId hasPrefix:@"iPhone1"] || // = iPhone or iPhone 3G |
381 |
[modelId hasPrefix:@"iPod1,1"] || [modelId hasPrefix:@"iPod2,1"]) // = iPod Touch or iPod Touch 2G |
|
3634 | 382 |
tmpQuality = 0x00000001 | 0x00000002 | 0x00000040; // rqLowRes | rqBlurryLand | rqKillFlakes |
3613 | 383 |
else if ([modelId hasPrefix:@"iPhone2"] || // = iPhone 3GS |
384 |
[modelId hasPrefix:@"iPod3"]) // = iPod Touch 3G |
|
3697 | 385 |
tmpQuality = 0x00000002 | 0x00000040; // rqBlurryLand | rqKillFlakes |
3624 | 386 |
else if ([modelId hasPrefix:@"iPad1"]) // = iPad |
3634 | 387 |
tmpQuality = 0x00000002; // rqBlurryLand |
3624 | 388 |
else // = everything else |
3634 | 389 |
tmpQuality = 0; // full quality |
390 |
if (![modelId hasPrefix:@"iPad"]) // = disable tooltips unless iPad |
|
391 |
tmpQuality = tmpQuality | 0x00000400; |
|
3697 | 392 |
|
3547 | 393 |
// prevents using an empty nickname |
394 |
NSString *username; |
|
395 |
NSString *originalUsername = [self.systemSettings objectForKey:@"username"]; |
|
396 |
if ([originalUsername length] == 0) |
|
397 |
username = [[NSString alloc] initWithFormat:@"MobileUser-%@",ipcString]; |
|
398 |
else |
|
399 |
username = [[NSString alloc] initWithString:originalUsername]; |
|
3697 | 400 |
|
3779
3351a017d4ad
tap to dismiss 'get ready', add a toggle to enable/disable it
koda
parents:
3752
diff
changeset
|
401 |
gameArgs[ 0] = [ipcString UTF8String]; //ipcPort |
3351a017d4ad
tap to dismiss 'get ready', add a toggle to enable/disable it
koda
parents:
3752
diff
changeset
|
402 |
gameArgs[ 1] = [wSize UTF8String]; //cScreenHeight |
3351a017d4ad
tap to dismiss 'get ready', add a toggle to enable/disable it
koda
parents:
3752
diff
changeset
|
403 |
gameArgs[ 2] = [hSize UTF8String]; //cScreenWidth |
3351a017d4ad
tap to dismiss 'get ready', add a toggle to enable/disable it
koda
parents:
3752
diff
changeset
|
404 |
gameArgs[ 3] = [[[NSNumber numberWithInteger:tmpQuality] stringValue] UTF8String]; //quality |
3351a017d4ad
tap to dismiss 'get ready', add a toggle to enable/disable it
koda
parents:
3752
diff
changeset
|
405 |
gameArgs[ 4] = [localeString UTF8String]; //cLocaleFName |
3351a017d4ad
tap to dismiss 'get ready', add a toggle to enable/disable it
koda
parents:
3752
diff
changeset
|
406 |
gameArgs[ 5] = [username UTF8String]; //UserNick |
3351a017d4ad
tap to dismiss 'get ready', add a toggle to enable/disable it
koda
parents:
3752
diff
changeset
|
407 |
gameArgs[ 6] = [[[self.systemSettings objectForKey:@"sound"] stringValue] UTF8String]; //isSoundEnabled |
3351a017d4ad
tap to dismiss 'get ready', add a toggle to enable/disable it
koda
parents:
3752
diff
changeset
|
408 |
gameArgs[ 7] = [[[self.systemSettings objectForKey:@"music"] stringValue] UTF8String]; //isMusicEnabled |
3351a017d4ad
tap to dismiss 'get ready', add a toggle to enable/disable it
koda
parents:
3752
diff
changeset
|
409 |
gameArgs[ 8] = [[[self.systemSettings objectForKey:@"alternate"] stringValue] UTF8String]; //cAltDamage |
3803 | 410 |
gameArgs[ 9] = NULL; //unused |
3779
3351a017d4ad
tap to dismiss 'get ready', add a toggle to enable/disable it
koda
parents:
3752
diff
changeset
|
411 |
gameArgs[10] = NULL; //recordFileName |
3697 | 412 |
|
3547 | 413 |
[wSize release]; |
414 |
[hSize release]; |
|
415 |
[localeString release]; |
|
416 |
[ipcString release]; |
|
417 |
[username release]; |
|
418 |
return gameArgs; |
|
419 |
} |
|
420 |
||
421 |
||
422 |
@end |