28 @synthesize blackView, savePath, port; |
28 @synthesize blackView, savePath, port; |
29 |
29 |
30 #pragma mark - |
30 #pragma mark - |
31 #pragma mark Instance methods for engine interaction |
31 #pragma mark Instance methods for engine interaction |
32 // prepares the controllers for hosting a game |
32 // prepares the controllers for hosting a game |
33 -(void) earlyEngineLaunch:(NSDictionary *)optionsOrNil { |
33 - (void)earlyEngineLaunch:(NSDictionary *)optionsOrNil { |
34 [self retain]; |
|
35 [[AudioManagerController mainManager] fadeOutBackgroundMusic]; |
34 [[AudioManagerController mainManager] fadeOutBackgroundMusic]; |
36 |
35 |
37 EngineProtocolNetwork *engineProtocol = [[EngineProtocolNetwork alloc] init]; |
36 EngineProtocolNetwork *engineProtocol = [[EngineProtocolNetwork alloc] init]; |
38 self.port = engineProtocol.enginePort; |
37 self.port = engineProtocol.enginePort; |
39 engineProtocol.delegate = self; |
38 engineProtocol.delegate = self; |
40 [engineProtocol spawnThread:self.savePath withOptions:optionsOrNil]; |
39 [engineProtocol spawnThread:self.savePath withOptions:optionsOrNil]; |
41 [engineProtocol release]; |
|
42 |
40 |
43 // add a black view hiding the background |
41 // add a black view hiding the background |
44 UIWindow *thisWindow = [[HedgewarsAppDelegate sharedAppDelegate] uiwindow]; |
42 UIWindow *thisWindow = [[HedgewarsAppDelegate sharedAppDelegate] uiwindow]; |
45 self.blackView = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; |
43 self.blackView = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; |
46 self.blackView.opaque = YES; |
44 self.blackView.opaque = YES; |
187 |
177 |
188 // moar cleanup |
178 // moar cleanup |
189 [self lateEngineLaunch]; |
179 [self lateEngineLaunch]; |
190 } |
180 } |
191 |
181 |
192 -(void) dealloc { |
|
193 releaseAndNil(blackView); |
|
194 releaseAndNil(savePath); |
|
195 [super dealloc]; |
|
196 } |
|
197 |
182 |
198 #pragma mark - |
183 #pragma mark - |
199 #pragma mark EngineProtocolDelegate methods |
184 #pragma mark EngineProtocolDelegate methods |
200 -(void) gameEndedWithStatistics:(NSArray *)stats { |
185 - (void)gameEndedWithStatistics:(NSArray *)stats { |
201 if (stats != nil) { |
186 if (stats != nil) { |
202 StatsPageViewController *statsPage = [[StatsPageViewController alloc] init]; |
187 StatsPageViewController *statsPage = [[StatsPageViewController alloc] init]; |
203 statsPage.statsArray = stats; |
188 statsPage.statsArray = stats; |
204 statsPage.modalTransitionStyle = UIModalTransitionStyleCoverVertical; |
189 statsPage.modalTransitionStyle = UIModalTransitionStyleCoverVertical; |
205 |
190 |
206 [callingController presentViewController:statsPage animated:YES completion:nil]; |
191 [callingController presentViewController:statsPage animated:YES completion:nil]; |
207 [statsPage release]; |
|
208 } |
192 } |
209 } |
193 } |
210 |
194 |
211 #pragma mark - |
195 #pragma mark - |
212 #pragma mark Class methods for setting up the engine from outsite |
196 #pragma mark Class methods for setting up the engine from outsite |
213 +(void) registerCallingController:(UIViewController *)controller { |
197 + (void)registerCallingController:(UIViewController *)controller { |
214 callingController = controller; |
198 callingController = controller; |
215 } |
199 } |
216 |
200 |
217 +(void) startGame:(TGameType) type atPath:(NSString *)path withOptions:(NSDictionary *)config { |
201 + (void)startGame:(TGameType)type atPath:(NSString *)path withOptions:(NSDictionary *)config { |
218 [HWUtils setGameType:type]; |
202 [HWUtils setGameType:type]; |
219 id bridge = [[self alloc] init]; |
203 id bridge = [[self alloc] init]; |
220 [bridge setSavePath:path]; |
204 [bridge setSavePath:path]; |
221 [bridge earlyEngineLaunch:config]; |
205 [bridge earlyEngineLaunch:config]; |
222 [bridge release]; |
206 } |
223 } |
207 |
224 |
208 + (void)startLocalGame:(NSDictionary *)withOptions { |
225 +(void) startLocalGame:(NSDictionary *)withOptions { |
|
226 NSDateFormatter *outputFormatter = [[NSDateFormatter alloc] init]; |
209 NSDateFormatter *outputFormatter = [[NSDateFormatter alloc] init]; |
227 [outputFormatter setDateFormat:@"yyyy-MM-dd '@' HH.mm"]; |
210 [outputFormatter setDateFormat:@"yyyy-MM-dd '@' HH.mm"]; |
228 NSString *savePath = [[NSString alloc] initWithFormat:@"%@%@.hws",SAVES_DIRECTORY(),[outputFormatter stringFromDate:[NSDate date]]]; |
211 NSString *savePath = [[NSString alloc] initWithFormat:@"%@%@.hws",SAVES_DIRECTORY(),[outputFormatter stringFromDate:[NSDate date]]]; |
229 [outputFormatter release]; |
|
230 |
212 |
231 // in the rare case in which a savefile with the same name exists the older one must be removed (otherwise it gets corrupted) |
213 // in the rare case in which a savefile with the same name exists the older one must be removed (otherwise it gets corrupted) |
232 if ([[NSFileManager defaultManager] fileExistsAtPath:savePath]) |
214 if ([[NSFileManager defaultManager] fileExistsAtPath:savePath]) |
233 [[NSFileManager defaultManager] removeItemAtPath:savePath error:nil]; |
215 [[NSFileManager defaultManager] removeItemAtPath:savePath error:nil]; |
234 |
216 |
235 [self startGame:gtLocal atPath:savePath withOptions:withOptions]; |
217 [self startGame:gtLocal atPath:savePath withOptions:withOptions]; |
236 [savePath release]; |
218 } |
237 } |
219 |
238 |
220 + (void)startSaveGame:(NSString *)atPath { |
239 +(void) startSaveGame:(NSString *)atPath { |
|
240 [self startGame:gtSave atPath:atPath withOptions:nil]; |
221 [self startGame:gtSave atPath:atPath withOptions:nil]; |
241 } |
222 } |
242 |
223 |
243 +(void) startMissionGame:(NSString *)withScript { |
224 + (void)startMissionGame:(NSString *)withScript { |
244 NSString *seedCmd = [self seedCommand]; |
225 NSString *seedCmd = [self seedCommand]; |
245 NSString *missionPath = [[NSString alloc] initWithFormat:@"escript Missions/Training/%@.lua",withScript]; |
226 NSString *missionPath = [[NSString alloc] initWithFormat:@"escript Missions/Training/%@.lua",withScript]; |
246 NSDictionary *missionDict = [[NSDictionary alloc] initWithObjectsAndKeys:missionPath, @"mission_command", seedCmd, @"seed_command", nil]; |
227 NSDictionary *missionDict = [[NSDictionary alloc] initWithObjectsAndKeys:missionPath, @"mission_command", seedCmd, @"seed_command", nil]; |
247 [missionPath release]; |
|
248 [seedCmd release]; |
|
249 |
228 |
250 [self startGame:gtMission atPath:nil withOptions:missionDict]; |
229 [self startGame:gtMission atPath:nil withOptions:missionDict]; |
251 [missionDict release]; |
230 } |
252 } |
231 |
253 |
232 + (NSString *)seedCommand { |
254 +(NSString *) seedCommand { |
|
255 // generate a seed |
233 // generate a seed |
256 NSString *seed = [HWUtils seed]; |
234 NSString *seed = [HWUtils seed]; |
257 NSString *seedCmd = [[NSString alloc] initWithFormat:@"eseed {%@}", seed]; |
235 NSString *seedCmd = [[NSString alloc] initWithFormat:@"eseed {%@}", seed]; |
258 [seed release]; |
|
259 return seedCmd; |
236 return seedCmd; |
260 } |
237 } |
261 |
238 |
262 +(void) startCampaignMissionGameWithScript:(NSString *)missionScriptName forCampaign:(NSString *)campaignName { |
239 + (void)startCampaignMissionGameWithScript:(NSString *)missionScriptName forCampaign:(NSString *)campaignName { |
263 NSString *seedCmd = [self seedCommand]; |
240 NSString *seedCmd = [self seedCommand]; |
264 NSString *campaignMissionPath = [[NSString alloc] initWithFormat:@"escript Missions/Campaign/%@/%@", campaignName, missionScriptName]; |
241 NSString *campaignMissionPath = [[NSString alloc] initWithFormat:@"escript Missions/Campaign/%@/%@", campaignName, missionScriptName]; |
265 NSDictionary *campaignMissionDict = [[NSDictionary alloc] initWithObjectsAndKeys:campaignMissionPath, @"mission_command", seedCmd, @"seed_command", nil]; |
242 NSDictionary *campaignMissionDict = [[NSDictionary alloc] initWithObjectsAndKeys:campaignMissionPath, @"mission_command", seedCmd, @"seed_command", nil]; |
266 [campaignMissionPath release]; |
|
267 [seedCmd release]; |
|
268 |
243 |
269 [self startGame:gtCampaign atPath:nil withOptions:campaignMissionDict]; |
244 [self startGame:gtCampaign atPath:nil withOptions:campaignMissionDict]; |
270 [campaignMissionDict release]; |
245 } |
271 } |
246 |
272 |
247 + (void)startSimpleGame { |
273 +(void) startSimpleGame { |
|
274 NSString *seedCmd = [self seedCommand]; |
248 NSString *seedCmd = [self seedCommand]; |
275 |
249 |
276 // pick a random static map |
250 // pick a random static map |
277 NSArray *listOfMaps = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:MAPS_DIRECTORY() error:NULL]; |
251 NSArray *listOfMaps = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:MAPS_DIRECTORY() error:NULL]; |
278 NSString *mapName = [listOfMaps objectAtIndex:arc4random_uniform((int)[listOfMaps count])]; |
252 NSString *mapName = [listOfMaps objectAtIndex:arc4random_uniform((int)[listOfMaps count])]; |
279 NSString *fileCfg = [[NSString alloc] initWithFormat:@"%@/%@/map.cfg",MAPS_DIRECTORY(),mapName]; |
253 NSString *fileCfg = [[NSString alloc] initWithFormat:@"%@/%@/map.cfg",MAPS_DIRECTORY(),mapName]; |
280 NSString *contents = [[NSString alloc] initWithContentsOfFile:fileCfg encoding:NSUTF8StringEncoding error:NULL]; |
254 NSString *contents = [[NSString alloc] initWithContentsOfFile:fileCfg encoding:NSUTF8StringEncoding error:NULL]; |
281 [fileCfg release]; |
|
282 NSArray *split = [contents componentsSeparatedByString:@"\n"]; |
255 NSArray *split = [contents componentsSeparatedByString:@"\n"]; |
283 [contents release]; |
|
284 NSString *themeCommand = [[NSString alloc] initWithFormat:@"etheme %@", [split objectAtIndex:0]]; |
256 NSString *themeCommand = [[NSString alloc] initWithFormat:@"etheme %@", [split objectAtIndex:0]]; |
285 NSString *staticMapCommand = [[NSString alloc] initWithFormat:@"emap %@", mapName]; |
257 NSString *staticMapCommand = [[NSString alloc] initWithFormat:@"emap %@", mapName]; |
286 |
258 |
287 // select teams with two different colors |
259 // select teams with two different colors |
288 NSArray *colorArray = [HWUtils teamColors]; |
260 NSArray *colorArray = [HWUtils teamColors]; |
299 @"Ninjas.plist",@"team",nil]; |
271 @"Ninjas.plist",@"team",nil]; |
300 NSDictionary *secondTeam = [[NSDictionary alloc] initWithObjectsAndKeys:[NSNumber numberWithInt:4],@"number", |
272 NSDictionary *secondTeam = [[NSDictionary alloc] initWithObjectsAndKeys:[NSNumber numberWithInt:4],@"number", |
301 [NSNumber numberWithUnsignedInt:secondColor],@"color", |
273 [NSNumber numberWithUnsignedInt:secondColor],@"color", |
302 @"Robots.plist",@"team",nil]; |
274 @"Robots.plist",@"team",nil]; |
303 NSArray *listOfTeams = [[NSArray alloc] initWithObjects:firstTeam,secondTeam,nil]; |
275 NSArray *listOfTeams = [[NSArray alloc] initWithObjects:firstTeam,secondTeam,nil]; |
304 [firstTeam release]; |
|
305 [secondTeam release]; |
|
306 |
276 |
307 // create the configuration |
277 // create the configuration |
308 NSDictionary *gameDictionary = [[NSDictionary alloc] initWithObjectsAndKeys: |
278 NSDictionary *gameDictionary = [[NSDictionary alloc] initWithObjectsAndKeys: |
309 seedCmd,@"seed_command", |
279 seedCmd,@"seed_command", |
310 @"e$template_filter 0",@"templatefilter_command", |
280 @"e$template_filter 0",@"templatefilter_command", |