author | koda |
Sun, 17 Apr 2011 22:38:24 +0200 | |
changeset 5155 | f2165724605c |
parent 5154 | 851f36579ed4 |
child 5156 | 641abe679bf0 |
permissions | -rw-r--r-- |
3829 | 1 |
/* |
2 |
* Hedgewars-iOS, a Hedgewars port for iOS devices |
|
4976 | 3 |
* Copyright (c) 2009-2011 Vittorio Giovara <vittorio.giovara@gmail.com> |
3829 | 4 |
* |
5 |
* This program is free software; you can redistribute it and/or modify |
|
6 |
* it under the terms of the GNU General Public License as published by |
|
7 |
* the Free Software Foundation; version 2 of the License |
|
8 |
* |
|
9 |
* This program is distributed in the hope that it will be useful, |
|
10 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
12 |
* GNU General Public License for more details. |
|
13 |
* |
|
14 |
* You should have received a copy of the GNU General Public License |
|
15 |
* along with this program; if not, write to the Free Software |
|
16 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|
17 |
* |
|
18 |
* File created on 10/01/2010. |
|
19 |
*/ |
|
20 |
||
3547 | 21 |
|
5154
851f36579ed4
initial refactoring for interfacing the game engine from the ios frontend (game doesn't run yet)
koda
parents:
5002
diff
changeset
|
22 |
#import "EngineProtocolNetwork.h" |
3547 | 23 |
#import "PascalImports.h" |
24 |
#import "CommodityFunctions.h" |
|
3904
22e4d74240e5
finishing touches to save games handling (help label, dim on overlay, edit text only when table is editable)
koda
parents:
3902
diff
changeset
|
25 |
#import "OverlayViewController.h" |
3547 | 26 |
|
3902
3aac7ca07b0e
have Saves restart net when loaded, also resumes music
koda
parents:
3898
diff
changeset
|
27 |
#define BUFFER_SIZE 255 // like in original frontend |
3547 | 28 |
|
5154
851f36579ed4
initial refactoring for interfacing the game engine from the ios frontend (game doesn't run yet)
koda
parents:
5002
diff
changeset
|
29 |
@implementation EngineProtocolNetwork |
5155
f2165724605c
more refactoring, less warnings, less stuff kept around
koda
parents:
5154
diff
changeset
|
30 |
@synthesize delegate, stream, ipcPort, csd; |
3948
24daa33a3114
some rethinking of initial menu presentation and initial orientation (also merging images should be threadsafe now)
koda
parents:
3941
diff
changeset
|
31 |
|
5154
851f36579ed4
initial refactoring for interfacing the game engine from the ios frontend (game doesn't run yet)
koda
parents:
5002
diff
changeset
|
32 |
-(id) init { |
851f36579ed4
initial refactoring for interfacing the game engine from the ios frontend (game doesn't run yet)
koda
parents:
5002
diff
changeset
|
33 |
if (self = [super init]) { |
5155
f2165724605c
more refactoring, less warnings, less stuff kept around
koda
parents:
5154
diff
changeset
|
34 |
self.delegate = nil; |
f2165724605c
more refactoring, less warnings, less stuff kept around
koda
parents:
5154
diff
changeset
|
35 |
|
5154
851f36579ed4
initial refactoring for interfacing the game engine from the ios frontend (game doesn't run yet)
koda
parents:
5002
diff
changeset
|
36 |
self.ipcPort = 0; |
5155
f2165724605c
more refactoring, less warnings, less stuff kept around
koda
parents:
5154
diff
changeset
|
37 |
self.csd = NULL; |
f2165724605c
more refactoring, less warnings, less stuff kept around
koda
parents:
5154
diff
changeset
|
38 |
self.stream = nil; |
3697 | 39 |
} |
3547 | 40 |
return self; |
41 |
} |
|
42 |
||
5155
f2165724605c
more refactoring, less warnings, less stuff kept around
koda
parents:
5154
diff
changeset
|
43 |
-(id) initOnPort:(NSInteger) port { |
f2165724605c
more refactoring, less warnings, less stuff kept around
koda
parents:
5154
diff
changeset
|
44 |
if (self = [self init]) |
f2165724605c
more refactoring, less warnings, less stuff kept around
koda
parents:
5154
diff
changeset
|
45 |
self.ipcPort = port; |
f2165724605c
more refactoring, less warnings, less stuff kept around
koda
parents:
5154
diff
changeset
|
46 |
return self; |
f2165724605c
more refactoring, less warnings, less stuff kept around
koda
parents:
5154
diff
changeset
|
47 |
} |
f2165724605c
more refactoring, less warnings, less stuff kept around
koda
parents:
5154
diff
changeset
|
48 |
|
f2165724605c
more refactoring, less warnings, less stuff kept around
koda
parents:
5154
diff
changeset
|
49 |
-(void) gameHasEndedWithStats:(NSArray *)stats { |
f2165724605c
more refactoring, less warnings, less stuff kept around
koda
parents:
5154
diff
changeset
|
50 |
if (self.delegate != nil && [self.delegate respondsToSelector:@selector(gameHasEndedWithStats:)]) |
f2165724605c
more refactoring, less warnings, less stuff kept around
koda
parents:
5154
diff
changeset
|
51 |
[self.delegate gameHasEndedWithStats:stats]; |
f2165724605c
more refactoring, less warnings, less stuff kept around
koda
parents:
5154
diff
changeset
|
52 |
else |
f2165724605c
more refactoring, less warnings, less stuff kept around
koda
parents:
5154
diff
changeset
|
53 |
DLog(@"Error! delegate == nil"); |
f2165724605c
more refactoring, less warnings, less stuff kept around
koda
parents:
5154
diff
changeset
|
54 |
} |
f2165724605c
more refactoring, less warnings, less stuff kept around
koda
parents:
5154
diff
changeset
|
55 |
|
3547 | 56 |
-(void) dealloc { |
5155
f2165724605c
more refactoring, less warnings, less stuff kept around
koda
parents:
5154
diff
changeset
|
57 |
self.delegate = nil; |
f2165724605c
more refactoring, less warnings, less stuff kept around
koda
parents:
5154
diff
changeset
|
58 |
releaseAndNil(stream); |
3547 | 59 |
[super dealloc]; |
60 |
} |
|
61 |
||
5155
f2165724605c
more refactoring, less warnings, less stuff kept around
koda
parents:
5154
diff
changeset
|
62 |
#pragma mark - |
f2165724605c
more refactoring, less warnings, less stuff kept around
koda
parents:
5154
diff
changeset
|
63 |
#pragma mark Spawner functions |
f2165724605c
more refactoring, less warnings, less stuff kept around
koda
parents:
5154
diff
changeset
|
64 |
-(void) spawnThread:(NSString *)onSaveFile withOptions:(NSDictionary *)dictionary { |
f2165724605c
more refactoring, less warnings, less stuff kept around
koda
parents:
5154
diff
changeset
|
65 |
self.stream = [[NSOutputStream alloc] initToFileAtPath:onSaveFile append:YES]; |
5154
851f36579ed4
initial refactoring for interfacing the game engine from the ios frontend (game doesn't run yet)
koda
parents:
5002
diff
changeset
|
66 |
|
851f36579ed4
initial refactoring for interfacing the game engine from the ios frontend (game doesn't run yet)
koda
parents:
5002
diff
changeset
|
67 |
[NSThread detachNewThreadSelector:@selector(engineProtocol) |
851f36579ed4
initial refactoring for interfacing the game engine from the ios frontend (game doesn't run yet)
koda
parents:
5002
diff
changeset
|
68 |
toTarget:self |
5155
f2165724605c
more refactoring, less warnings, less stuff kept around
koda
parents:
5154
diff
changeset
|
69 |
withObject:dictionary]; |
f2165724605c
more refactoring, less warnings, less stuff kept around
koda
parents:
5154
diff
changeset
|
70 |
} |
f2165724605c
more refactoring, less warnings, less stuff kept around
koda
parents:
5154
diff
changeset
|
71 |
|
f2165724605c
more refactoring, less warnings, less stuff kept around
koda
parents:
5154
diff
changeset
|
72 |
-(void) spawnThread:(NSString *)onSaveFile { |
f2165724605c
more refactoring, less warnings, less stuff kept around
koda
parents:
5154
diff
changeset
|
73 |
[self spawnThread:onSaveFile withOptions:nil]; |
5154
851f36579ed4
initial refactoring for interfacing the game engine from the ios frontend (game doesn't run yet)
koda
parents:
5002
diff
changeset
|
74 |
} |
851f36579ed4
initial refactoring for interfacing the game engine from the ios frontend (game doesn't run yet)
koda
parents:
5002
diff
changeset
|
75 |
|
3547 | 76 |
#pragma mark - |
77 |
#pragma mark Provider functions |
|
78 |
// unpacks team data from the selected team.plist to a sequence of engine commands |
|
79 |
-(void) provideTeamData:(NSString *)teamName forHogs:(NSInteger) numberOfPlayingHogs withHealth:(NSInteger) initialHealth ofColor:(NSNumber *)teamColor { |
|
80 |
/* |
|
81 |
addteam <32charsMD5hash> <color> <team name> |
|
82 |
addhh <level> <health> <hedgehog name> |
|
83 |
<level> is 0 for human, 1-5 for bots (5 is the most stupid) |
|
84 |
*/ |
|
3697 | 85 |
|
3547 | 86 |
NSString *teamFile = [[NSString alloc] initWithFormat:@"%@/%@", TEAMS_DIRECTORY(), teamName]; |
87 |
NSDictionary *teamData = [[NSDictionary alloc] initWithContentsOfFile:teamFile]; |
|
88 |
[teamFile release]; |
|
3697 | 89 |
|
90 |
NSString *teamHashColorAndName = [[NSString alloc] initWithFormat:@"eaddteam %@ %@ %@", |
|
3660 | 91 |
[teamData objectForKey:@"hash"], [teamColor stringValue], [teamName stringByDeletingPathExtension]]; |
3547 | 92 |
[self sendToEngine: teamHashColorAndName]; |
93 |
[teamHashColorAndName release]; |
|
3697 | 94 |
|
3547 | 95 |
NSString *grave = [[NSString alloc] initWithFormat:@"egrave %@", [teamData objectForKey:@"grave"]]; |
96 |
[self sendToEngine: grave]; |
|
97 |
[grave release]; |
|
3697 | 98 |
|
3547 | 99 |
NSString *fort = [[NSString alloc] initWithFormat:@"efort %@", [teamData objectForKey:@"fort"]]; |
100 |
[self sendToEngine: fort]; |
|
101 |
[fort release]; |
|
3697 | 102 |
|
3547 | 103 |
NSString *voicepack = [[NSString alloc] initWithFormat:@"evoicepack %@", [teamData objectForKey:@"voicepack"]]; |
104 |
[self sendToEngine: voicepack]; |
|
105 |
[voicepack release]; |
|
3697 | 106 |
|
3547 | 107 |
NSString *flag = [[NSString alloc] initWithFormat:@"eflag %@", [teamData objectForKey:@"flag"]]; |
108 |
[self sendToEngine: flag]; |
|
109 |
[flag release]; |
|
3697 | 110 |
|
3547 | 111 |
NSArray *hogs = [teamData objectForKey:@"hedgehogs"]; |
112 |
for (int i = 0; i < numberOfPlayingHogs; i++) { |
|
113 |
NSDictionary *hog = [hogs objectAtIndex:i]; |
|
3697 | 114 |
|
115 |
NSString *hogLevelHealthAndName = [[NSString alloc] initWithFormat:@"eaddhh %@ %d %@", |
|
3547 | 116 |
[hog objectForKey:@"level"], initialHealth, [hog objectForKey:@"hogname"]]; |
117 |
[self sendToEngine: hogLevelHealthAndName]; |
|
118 |
[hogLevelHealthAndName release]; |
|
3697 | 119 |
|
3547 | 120 |
NSString *hogHat = [[NSString alloc] initWithFormat:@"ehat %@", [hog objectForKey:@"hat"]]; |
121 |
[self sendToEngine: hogHat]; |
|
122 |
[hogHat release]; |
|
123 |
} |
|
3697 | 124 |
|
3547 | 125 |
[teamData release]; |
126 |
} |
|
127 |
||
128 |
// unpacks ammostore data from the selected ammo.plist to a sequence of engine commands |
|
129 |
-(void) provideAmmoData:(NSString *)ammostoreName forPlayingTeams:(NSInteger) numberOfTeams { |
|
130 |
NSString *weaponPath = [[NSString alloc] initWithFormat:@"%@/%@",WEAPONS_DIRECTORY(),ammostoreName]; |
|
131 |
NSDictionary *ammoData = [[NSDictionary alloc] initWithContentsOfFile:weaponPath]; |
|
132 |
[weaponPath release]; |
|
3697 | 133 |
|
3621 | 134 |
// if we're loading an older version of ammos fill the engine message with 0s |
3930 | 135 |
int diff = HW_getNumberOfWeapons() - [[ammoData objectForKey:@"ammostore_initialqt"] length]; |
136 |
NSString *update = @""; |
|
137 |
while ([update length] < diff) |
|
138 |
update = [update stringByAppendingString:@"0"]; |
|
3697 | 139 |
|
3621 | 140 |
NSString *ammloadt = [[NSString alloc] initWithFormat:@"eammloadt %@%@", [ammoData objectForKey:@"ammostore_initialqt"], update]; |
3547 | 141 |
[self sendToEngine: ammloadt]; |
142 |
[ammloadt release]; |
|
3697 | 143 |
|
3621 | 144 |
NSString *ammprob = [[NSString alloc] initWithFormat:@"eammprob %@%@", [ammoData objectForKey:@"ammostore_probability"], update]; |
3547 | 145 |
[self sendToEngine: ammprob]; |
146 |
[ammprob release]; |
|
3697 | 147 |
|
3621 | 148 |
NSString *ammdelay = [[NSString alloc] initWithFormat:@"eammdelay %@%@", [ammoData objectForKey:@"ammostore_delay"], update]; |
3547 | 149 |
[self sendToEngine: ammdelay]; |
150 |
[ammdelay release]; |
|
3697 | 151 |
|
3621 | 152 |
NSString *ammreinf = [[NSString alloc] initWithFormat:@"eammreinf %@%@", [ammoData objectForKey:@"ammostore_crate"], update]; |
3547 | 153 |
[self sendToEngine: ammreinf]; |
154 |
[ammreinf release]; |
|
3697 | 155 |
|
3930 | 156 |
// send this for each team so it applies the same ammostore to all teams |
3547 | 157 |
NSString *ammstore = [[NSString alloc] initWithString:@"eammstore"]; |
158 |
for (int i = 0; i < numberOfTeams; i++) |
|
159 |
[self sendToEngine: ammstore]; |
|
160 |
[ammstore release]; |
|
3697 | 161 |
|
3547 | 162 |
[ammoData release]; |
163 |
} |
|
164 |
||
165 |
// unpacks scheme data from the selected scheme.plist to a sequence of engine commands |
|
166 |
-(NSInteger) provideScheme:(NSString *)schemeName { |
|
167 |
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
|
168 |
NSDictionary *schemeDictionary = [[NSDictionary alloc] initWithContentsOfFile:schemePath]; |
3547 | 169 |
[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
|
170 |
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
|
171 |
NSArray *gamemodArray = [schemeDictionary objectForKey:@"gamemod"]; |
3547 | 172 |
int result = 0; |
4000
ddc4a09889e7
engine: reorder GameFlags and update files, frontend and other stuff
koda
parents:
3996
diff
changeset
|
173 |
int mask = 0x00000004; |
3547 | 174 |
|
4000
ddc4a09889e7
engine: reorder GameFlags and update files, frontend and other stuff
koda
parents:
3996
diff
changeset
|
175 |
// pack the gameflags in a single var and send it |
ddc4a09889e7
engine: reorder GameFlags and update files, frontend and other stuff
koda
parents:
3996
diff
changeset
|
176 |
for (NSNumber *value in gamemodArray) { |
ddc4a09889e7
engine: reorder GameFlags and update files, frontend and other stuff
koda
parents:
3996
diff
changeset
|
177 |
if ([value boolValue] == YES) |
ddc4a09889e7
engine: reorder GameFlags and update files, frontend and other stuff
koda
parents:
3996
diff
changeset
|
178 |
result |= mask; |
ddc4a09889e7
engine: reorder GameFlags and update files, frontend and other stuff
koda
parents:
3996
diff
changeset
|
179 |
mask <<= 1; |
ddc4a09889e7
engine: reorder GameFlags and update files, frontend and other stuff
koda
parents:
3996
diff
changeset
|
180 |
} |
3547 | 181 |
NSString *flags = [[NSString alloc] initWithFormat:@"e$gmflags %d",result]; |
182 |
[self sendToEngine:flags]; |
|
183 |
[flags release]; |
|
3697 | 184 |
|
4211
7dcbd236ca59
this time i got it right, i'm sure of it; TEST ANYWAYS
koda
parents:
4210
diff
changeset
|
185 |
/* basic game flags */ |
7dcbd236ca59
this time i got it right, i'm sure of it; TEST ANYWAYS
koda
parents:
4210
diff
changeset
|
186 |
NSString *path = [[NSString alloc] initWithFormat:@"%@/basicFlags_en.plist",IFRONTEND_DIRECTORY()]; |
7dcbd236ca59
this time i got it right, i'm sure of it; TEST ANYWAYS
koda
parents:
4210
diff
changeset
|
187 |
NSArray *mods = [[NSArray alloc] initWithContentsOfFile:path]; |
7dcbd236ca59
this time i got it right, i'm sure of it; TEST ANYWAYS
koda
parents:
4210
diff
changeset
|
188 |
[path release]; |
3697 | 189 |
|
4605 | 190 |
result = [[basicArray objectAtIndex:0] intValue]; |
4211
7dcbd236ca59
this time i got it right, i'm sure of it; TEST ANYWAYS
koda
parents:
4210
diff
changeset
|
191 |
|
4605 | 192 |
for (int i = 1; i < [basicArray count]; i++) { |
193 |
NSDictionary *dict = [mods objectAtIndex:i]; |
|
194 |
NSString *command = [dict objectForKey:@"command"]; |
|
4211
7dcbd236ca59
this time i got it right, i'm sure of it; TEST ANYWAYS
koda
parents:
4210
diff
changeset
|
195 |
NSInteger value = [[basicArray objectAtIndex:i] intValue]; |
4605 | 196 |
if ([[dict objectForKey:@"checkOverMax"] boolValue] && value >= [[dict objectForKey:@"max"] intValue]) |
4211
7dcbd236ca59
this time i got it right, i'm sure of it; TEST ANYWAYS
koda
parents:
4210
diff
changeset
|
197 |
value = 9999; |
4605 | 198 |
if ([[dict objectForKey:@"times1000"] boolValue]) |
199 |
value = value * 1000; |
|
4211
7dcbd236ca59
this time i got it right, i'm sure of it; TEST ANYWAYS
koda
parents:
4210
diff
changeset
|
200 |
NSString *strToSend = [[NSString alloc] initWithFormat:@"%@ %d",command,value]; |
7dcbd236ca59
this time i got it right, i'm sure of it; TEST ANYWAYS
koda
parents:
4210
diff
changeset
|
201 |
[self sendToEngine:strToSend]; |
7dcbd236ca59
this time i got it right, i'm sure of it; TEST ANYWAYS
koda
parents:
4210
diff
changeset
|
202 |
[strToSend release]; |
7dcbd236ca59
this time i got it right, i'm sure of it; TEST ANYWAYS
koda
parents:
4210
diff
changeset
|
203 |
} |
7dcbd236ca59
this time i got it right, i'm sure of it; TEST ANYWAYS
koda
parents:
4210
diff
changeset
|
204 |
[mods release]; |
3697 | 205 |
|
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
|
206 |
[schemeDictionary release]; |
3547 | 207 |
return result; |
208 |
} |
|
209 |
||
210 |
#pragma mark - |
|
4547 | 211 |
#pragma mark Network relevant code |
4754 | 212 |
-(void) dumpRawData:(const char *)buffer ofSize:(uint8_t) length { |
5155
f2165724605c
more refactoring, less warnings, less stuff kept around
koda
parents:
5154
diff
changeset
|
213 |
[self.stream open]; |
f2165724605c
more refactoring, less warnings, less stuff kept around
koda
parents:
5154
diff
changeset
|
214 |
[self.stream write:&length maxLength:1]; |
f2165724605c
more refactoring, less warnings, less stuff kept around
koda
parents:
5154
diff
changeset
|
215 |
[self.stream write:(const uint8_t *)buffer maxLength:length]; |
f2165724605c
more refactoring, less warnings, less stuff kept around
koda
parents:
5154
diff
changeset
|
216 |
[self.stream close]; |
3912
e11df2de6af2
have engine try for a second position when map loading fails (in this way it's possible to move Missions data to any path)
koda
parents:
3911
diff
changeset
|
217 |
} |
e11df2de6af2
have engine try for a second position when map loading fails (in this way it's possible to move Missions data to any path)
koda
parents:
3911
diff
changeset
|
218 |
|
3891 | 219 |
// wrapper that computes the length of the message and then sends the command string, saving the command on a file |
4512 | 220 |
-(int) sendToEngine:(NSString *)string { |
3547 | 221 |
uint8_t length = [string length]; |
3697 | 222 |
|
4754 | 223 |
[self dumpRawData:[string UTF8String] ofSize:length]; |
3902
3aac7ca07b0e
have Saves restart net when loaded, also resumes music
koda
parents:
3898
diff
changeset
|
224 |
SDLNet_TCP_Send(csd, &length, 1); |
3891 | 225 |
return SDLNet_TCP_Send(csd, [string UTF8String], length); |
226 |
} |
|
227 |
||
228 |
// wrapper that computes the length of the message and then sends the command string, skipping file writing |
|
4512 | 229 |
-(int) sendToEngineNoSave:(NSString *)string { |
3891 | 230 |
uint8_t length = [string length]; |
231 |
||
3902
3aac7ca07b0e
have Saves restart net when loaded, also resumes music
koda
parents:
3898
diff
changeset
|
232 |
SDLNet_TCP_Send(csd, &length, 1); |
3547 | 233 |
return SDLNet_TCP_Send(csd, [string UTF8String], length); |
234 |
} |
|
235 |
||
5154
851f36579ed4
initial refactoring for interfacing the game engine from the ios frontend (game doesn't run yet)
koda
parents:
5002
diff
changeset
|
236 |
// this is launched as thread and handles all IPC with engine |
5155
f2165724605c
more refactoring, less warnings, less stuff kept around
koda
parents:
5154
diff
changeset
|
237 |
-(void) engineProtocol:(id) object { |
3547 | 238 |
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; |
5155
f2165724605c
more refactoring, less warnings, less stuff kept around
koda
parents:
5154
diff
changeset
|
239 |
NSDictionary *gameConfig = (NSDictionary *)object; |
f2165724605c
more refactoring, less warnings, less stuff kept around
koda
parents:
5154
diff
changeset
|
240 |
NSMutableArray *statsArray = nil; |
3548
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
241 |
TCPsocket sd; |
3547 | 242 |
IPaddress ip; |
243 |
int eProto; |
|
3548
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
244 |
BOOL clientQuit; |
4754 | 245 |
char const buffer[BUFFER_SIZE]; |
3547 | 246 |
uint8_t msgSize; |
3697 | 247 |
|
3548
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
248 |
clientQuit = NO; |
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
249 |
csd = NULL; |
3547 | 250 |
|
251 |
if (SDLNet_Init() < 0) { |
|
252 |
DLog(@"SDLNet_Init: %s", SDLNet_GetError()); |
|
3548
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
253 |
clientQuit = YES; |
3547 | 254 |
} |
3697 | 255 |
|
3547 | 256 |
// Resolving the host using NULL make network interface to listen |
3548
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
257 |
if (SDLNet_ResolveHost(&ip, NULL, ipcPort) < 0 && !clientQuit) { |
3547 | 258 |
DLog(@"SDLNet_ResolveHost: %s\n", SDLNet_GetError()); |
3548
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
259 |
clientQuit = YES; |
3547 | 260 |
} |
3697 | 261 |
|
262 |
// 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
|
263 |
if (!(sd = SDLNet_TCP_Open(&ip)) && !clientQuit) { |
3547 | 264 |
DLog(@"SDLNet_TCP_Open: %s %\n", SDLNet_GetError(), ipcPort); |
3548
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
265 |
clientQuit = YES; |
3547 | 266 |
} |
3697 | 267 |
|
3547 | 268 |
DLog(@"Waiting for a client on port %d", ipcPort); |
3697 | 269 |
while (csd == NULL) |
3547 | 270 |
csd = SDLNet_TCP_Accept(sd); |
3548
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
271 |
SDLNet_TCP_Close(sd); |
3697 | 272 |
|
3548
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
273 |
while (!clientQuit) { |
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
274 |
msgSize = 0; |
4754 | 275 |
memset((void *)buffer, '\0', BUFFER_SIZE); |
3548
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
276 |
if (SDLNet_TCP_Recv(csd, &msgSize, sizeof(uint8_t)) <= 0) |
3902
3aac7ca07b0e
have Saves restart net when loaded, also resumes music
koda
parents:
3898
diff
changeset
|
277 |
break; |
4754 | 278 |
if (SDLNet_TCP_Recv(csd, (void *)buffer, msgSize) <= 0) |
3902
3aac7ca07b0e
have Saves restart net when loaded, also resumes music
koda
parents:
3898
diff
changeset
|
279 |
break; |
3697 | 280 |
|
3548
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
281 |
switch (buffer[0]) { |
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
282 |
case 'C': |
5155
f2165724605c
more refactoring, less warnings, less stuff kept around
koda
parents:
5154
diff
changeset
|
283 |
DLog(@"Sending game config...\n%@", gameConfig); |
3697 | 284 |
|
5154
851f36579ed4
initial refactoring for interfacing the game engine from the ios frontend (game doesn't run yet)
koda
parents:
5002
diff
changeset
|
285 |
/*if (isNetGame == YES) |
3893
568bfd083465
allow more flexibility between viewcontrollers, also added stub pages for saved games
koda
parents:
3891
diff
changeset
|
286 |
[self sendToEngineNoSave:@"TN"]; |
5154
851f36579ed4
initial refactoring for interfacing the game engine from the ios frontend (game doesn't run yet)
koda
parents:
5002
diff
changeset
|
287 |
else*/ |
3893
568bfd083465
allow more flexibility between viewcontrollers, also added stub pages for saved games
koda
parents:
3891
diff
changeset
|
288 |
[self sendToEngineNoSave:@"TL"]; |
3891 | 289 |
NSString *saveHeader = @"TS"; |
4754 | 290 |
[self dumpRawData:[saveHeader UTF8String] ofSize:[saveHeader length]]; |
3697 | 291 |
|
3547 | 292 |
// seed info |
5155
f2165724605c
more refactoring, less warnings, less stuff kept around
koda
parents:
5154
diff
changeset
|
293 |
[self sendToEngine:[gameConfig objectForKey:@"seed_command"]]; |
3697 | 294 |
|
3547 | 295 |
// dimension of the map |
5155
f2165724605c
more refactoring, less warnings, less stuff kept around
koda
parents:
5154
diff
changeset
|
296 |
[self sendToEngine:[gameConfig objectForKey:@"templatefilter_command"]]; |
f2165724605c
more refactoring, less warnings, less stuff kept around
koda
parents:
5154
diff
changeset
|
297 |
[self sendToEngine:[gameConfig objectForKey:@"mapgen_command"]]; |
f2165724605c
more refactoring, less warnings, less stuff kept around
koda
parents:
5154
diff
changeset
|
298 |
[self sendToEngine:[gameConfig objectForKey:@"mazesize_command"]]; |
3697 | 299 |
|
3642 | 300 |
// static land (if set) |
5155
f2165724605c
more refactoring, less warnings, less stuff kept around
koda
parents:
5154
diff
changeset
|
301 |
NSString *staticMap = [gameConfig objectForKey:@"staticmap_command"]; |
3642 | 302 |
if ([staticMap length] != 0) |
303 |
[self sendToEngine:staticMap]; |
|
3697 | 304 |
|
3910
dd47efbdec46
move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
3904
diff
changeset
|
305 |
// lua script (if set) |
5155
f2165724605c
more refactoring, less warnings, less stuff kept around
koda
parents:
5154
diff
changeset
|
306 |
NSString *script = [gameConfig objectForKey:@"mission_command"]; |
3910
dd47efbdec46
move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
3904
diff
changeset
|
307 |
if ([script length] != 0) |
dd47efbdec46
move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
3904
diff
changeset
|
308 |
[self sendToEngine:script]; |
5154
851f36579ed4
initial refactoring for interfacing the game engine from the ios frontend (game doesn't run yet)
koda
parents:
5002
diff
changeset
|
309 |
|
3547 | 310 |
// theme info |
5155
f2165724605c
more refactoring, less warnings, less stuff kept around
koda
parents:
5154
diff
changeset
|
311 |
[self sendToEngine:[gameConfig objectForKey:@"theme_command"]]; |
3697 | 312 |
|
3547 | 313 |
// scheme (returns initial health) |
5155
f2165724605c
more refactoring, less warnings, less stuff kept around
koda
parents:
5154
diff
changeset
|
314 |
NSInteger health = [self provideScheme:[gameConfig objectForKey:@"scheme"]]; |
3697 | 315 |
|
4000
ddc4a09889e7
engine: reorder GameFlags and update files, frontend and other stuff
koda
parents:
3996
diff
changeset
|
316 |
// send an ammostore for each team |
5155
f2165724605c
more refactoring, less warnings, less stuff kept around
koda
parents:
5154
diff
changeset
|
317 |
NSArray *teamsConfig = [gameConfig objectForKey:@"teams_list"]; |
f2165724605c
more refactoring, less warnings, less stuff kept around
koda
parents:
5154
diff
changeset
|
318 |
[self provideAmmoData:[gameConfig objectForKey:@"weapon"] forPlayingTeams:[teamsConfig count]]; |
4000
ddc4a09889e7
engine: reorder GameFlags and update files, frontend and other stuff
koda
parents:
3996
diff
changeset
|
319 |
|
ddc4a09889e7
engine: reorder GameFlags and update files, frontend and other stuff
koda
parents:
3996
diff
changeset
|
320 |
// finally add hogs |
3547 | 321 |
for (NSDictionary *teamData in teamsConfig) { |
3697 | 322 |
[self provideTeamData:[teamData objectForKey:@"team"] |
3547 | 323 |
forHogs:[[teamData objectForKey:@"number"] intValue] |
324 |
withHealth:health |
|
325 |
ofColor:[teamData objectForKey:@"color"]]; |
|
326 |
} |
|
3548
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
327 |
break; |
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
328 |
case '?': |
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
329 |
DLog(@"Ping? Pong!"); |
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
330 |
[self sendToEngine:@"!"]; |
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
331 |
break; |
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
332 |
case 'E': |
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
333 |
DLog(@"ERROR - last console line: [%s]", &buffer[1]); |
3547 | 334 |
clientQuit = YES; |
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 'e': |
3912
e11df2de6af2
have engine try for a second position when map loading fails (in this way it's possible to move Missions data to any path)
koda
parents:
3911
diff
changeset
|
337 |
[self dumpRawData:buffer ofSize:msgSize]; |
e11df2de6af2
have engine try for a second position when map loading fails (in this way it's possible to move Missions data to any path)
koda
parents:
3911
diff
changeset
|
338 |
|
3902
3aac7ca07b0e
have Saves restart net when loaded, also resumes music
koda
parents:
3898
diff
changeset
|
339 |
sscanf((char *)buffer, "%*s %d", &eProto); |
4603 | 340 |
int netProto; |
3548
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
341 |
char *versionStr; |
3697 | 342 |
|
3548
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
343 |
HW_versionInfo(&netProto, &versionStr); |
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
344 |
if (netProto == eProto) { |
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
345 |
DLog(@"Setting protocol version %d (%s)", eProto, versionStr); |
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
346 |
} else { |
4603 | 347 |
DLog(@"ERROR - wrong protocol number: %d (expecting %d)", netProto, eProto); |
3547 | 348 |
clientQuit = YES; |
3548
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
349 |
} |
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
350 |
break; |
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
351 |
case 'i': |
5155
f2165724605c
more refactoring, less warnings, less stuff kept around
koda
parents:
5154
diff
changeset
|
352 |
if (statsArray == nil) { |
f2165724605c
more refactoring, less warnings, less stuff kept around
koda
parents:
5154
diff
changeset
|
353 |
statsArray = [[NSMutableArray alloc] initWithCapacity:10 - 2]; |
4856 | 354 |
NSMutableArray *ranking = [[NSMutableArray alloc] initWithCapacity:4]; |
5155
f2165724605c
more refactoring, less warnings, less stuff kept around
koda
parents:
5154
diff
changeset
|
355 |
[statsArray insertObject:ranking atIndex:0]; |
4856 | 356 |
[ranking release]; |
357 |
} |
|
4769 | 358 |
NSString *tempStr = [NSString stringWithUTF8String:&buffer[2]]; |
4856 | 359 |
NSArray *info = [tempStr componentsSeparatedByString:@" "]; |
360 |
NSString *arg = [info objectAtIndex:0]; |
|
4793 | 361 |
int index = [arg length] + 3; |
3548
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
362 |
switch (buffer[1]) { |
4760 | 363 |
case 'r': // winning team |
5155
f2165724605c
more refactoring, less warnings, less stuff kept around
koda
parents:
5154
diff
changeset
|
364 |
[statsArray insertObject:[NSString stringWithUTF8String:&buffer[2]] atIndex:1]; |
3547 | 365 |
break; |
4760 | 366 |
case 'D': // best shot |
5155
f2165724605c
more refactoring, less warnings, less stuff kept around
koda
parents:
5154
diff
changeset
|
367 |
[statsArray addObject:[NSString stringWithFormat:@"The best shot award won by %s (with %@ points)", &buffer[index], arg]]; |
4549 | 368 |
break; |
4760 | 369 |
case 'k': // best hedgehog |
5155
f2165724605c
more refactoring, less warnings, less stuff kept around
koda
parents:
5154
diff
changeset
|
370 |
[statsArray addObject:[NSString stringWithFormat:@"The best killer is %s with %@ kills in a turn", &buffer[index], arg]]; |
3902
3aac7ca07b0e
have Saves restart net when loaded, also resumes music
koda
parents:
3898
diff
changeset
|
371 |
break; |
4760 | 372 |
case 'K': // number of hogs killed |
5155
f2165724605c
more refactoring, less warnings, less stuff kept around
koda
parents:
5154
diff
changeset
|
373 |
[statsArray addObject:[NSString stringWithFormat:@"%@ hedgehog(s) were killed during this round", arg]]; |
4549 | 374 |
break; |
4856 | 375 |
case 'H': // team health/graph |
4549 | 376 |
break; |
4760 | 377 |
case 'T': // local team stats |
4856 | 378 |
// still WIP in statsPage.cpp |
4549 | 379 |
break; |
4856 | 380 |
case 'P': // teams ranking |
5155
f2165724605c
more refactoring, less warnings, less stuff kept around
koda
parents:
5154
diff
changeset
|
381 |
[[statsArray objectAtIndex:0] addObject:tempStr]; |
4549 | 382 |
break; |
4760 | 383 |
case 's': // self damage |
5155
f2165724605c
more refactoring, less warnings, less stuff kept around
koda
parents:
5154
diff
changeset
|
384 |
[statsArray addObject:[NSString stringWithFormat:@"%s thought it's good to shoot his own hedgehogs with %@ points", &buffer[index], arg]]; |
4549 | 385 |
break; |
4760 | 386 |
case 'S': // friendly fire |
5155
f2165724605c
more refactoring, less warnings, less stuff kept around
koda
parents:
5154
diff
changeset
|
387 |
[statsArray addObject:[NSString stringWithFormat:@"%s killed %@ of his own hedgehogs", &buffer[index], arg]]; |
4549 | 388 |
break; |
4760 | 389 |
case 'B': // turn skipped |
5155
f2165724605c
more refactoring, less warnings, less stuff kept around
koda
parents:
5154
diff
changeset
|
390 |
[statsArray addObject:[NSString stringWithFormat:@"%s was scared and skipped turn %@ times", &buffer[index], arg]]; |
4549 | 391 |
break; |
3902
3aac7ca07b0e
have Saves restart net when loaded, also resumes music
koda
parents:
3898
diff
changeset
|
392 |
default: |
4549 | 393 |
DLog(@"Unhandled stat message, see statsPage.cpp"); |
3547 | 394 |
break; |
395 |
} |
|
3548
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
396 |
break; |
3902
3aac7ca07b0e
have Saves restart net when loaded, also resumes music
koda
parents:
3898
diff
changeset
|
397 |
case 'q': |
4924 | 398 |
// game ended, can remove the savefile and the trailing overlay (when dualhead) |
5155
f2165724605c
more refactoring, less warnings, less stuff kept around
koda
parents:
5154
diff
changeset
|
399 |
[self gameHasEndedWithStats:statsArray]; |
4861
91f889289a47
(ios) perform a small change about how to close the game window, should hopefully save memory and avoid crashes (who am i kidding? that's the usual PR talk...)
koda
parents:
4856
diff
changeset
|
400 |
break; |
91f889289a47
(ios) perform a small change about how to close the game window, should hopefully save memory and avoid crashes (who am i kidding? that's the usual PR talk...)
koda
parents:
4856
diff
changeset
|
401 |
case 'Q': |
91f889289a47
(ios) perform a small change about how to close the game window, should hopefully save memory and avoid crashes (who am i kidding? that's the usual PR talk...)
koda
parents:
4856
diff
changeset
|
402 |
// game exited but not completed, nothing to do (just don't save the message) |
3902
3aac7ca07b0e
have Saves restart net when loaded, also resumes music
koda
parents:
3898
diff
changeset
|
403 |
break; |
3548
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
404 |
default: |
3912
e11df2de6af2
have engine try for a second position when map loading fails (in this way it's possible to move Missions data to any path)
koda
parents:
3911
diff
changeset
|
405 |
[self dumpRawData:buffer ofSize:msgSize]; |
3548
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
406 |
break; |
3547 | 407 |
} |
408 |
} |
|
4510 | 409 |
DLog(@"Engine exited, ending thread"); |
4861
91f889289a47
(ios) perform a small change about how to close the game window, should hopefully save memory and avoid crashes (who am i kidding? that's the usual PR talk...)
koda
parents:
4856
diff
changeset
|
410 |
|
3548
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
411 |
// Close the client socket |
3697 | 412 |
SDLNet_TCP_Close(csd); |
3548
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
413 |
SDLNet_Quit(); |
3697 | 414 |
|
3547 | 415 |
[pool release]; |
4861
91f889289a47
(ios) perform a small change about how to close the game window, should hopefully save memory and avoid crashes (who am i kidding? that's the usual PR talk...)
koda
parents:
4856
diff
changeset
|
416 |
// Invoking this method should be avoided as it does not give your thread a chance |
91f889289a47
(ios) perform a small change about how to close the game window, should hopefully save memory and avoid crashes (who am i kidding? that's the usual PR talk...)
koda
parents:
4856
diff
changeset
|
417 |
// to clean up any resources it allocated during its execution. |
3547 | 418 |
//[NSThread exit]; |
419 |
} |
|
420 |
||
421 |
@end |