project_files/HedgewarsMobile/Classes/GameInterfaceBridge.m
branchios-develop
changeset 12872 00215a7ec5f5
parent 11572 28afdaa159cb
child 12873 f012ce693681
equal deleted inserted replaced
12871:2c06b1120749 12872:00215a7ec5f5
    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;
    50     [UIView beginAnimations:@"fade out" context:NULL];
    48     [UIView beginAnimations:@"fade out" context:NULL];
    51     [UIView setAnimationDuration:1];
    49     [UIView setAnimationDuration:1];
    52     self.blackView.alpha = 1;
    50     self.blackView.alpha = 1;
    53     [UIView commitAnimations];
    51     [UIView commitAnimations];
    54     [thisWindow addSubview:self.blackView];
    52     [thisWindow addSubview:self.blackView];
    55     [self.blackView release];
       
    56 
    53 
    57     // keep the point of return for games that completed loading
    54     // keep the point of return for games that completed loading
    58     NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    55     NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    59     [userDefaults setObject:self.savePath forKey:@"savedGamePath"];
    56     [userDefaults setObject:self.savePath forKey:@"savedGamePath"];
    60     [userDefaults setObject:[NSNumber numberWithBool:NO] forKey:@"saveIsValid"];
    57     [userDefaults setObject:[NSNumber numberWithBool:NO] forKey:@"saveIsValid"];
    63     // let's launch the engine using this -perfomSelector so that the runloop can deal with queued messages first
    60     // let's launch the engine using this -perfomSelector so that the runloop can deal with queued messages first
    64     [self performSelector:@selector(engineLaunch) withObject:nil afterDelay:0.1f];
    61     [self performSelector:@selector(engineLaunch) withObject:nil afterDelay:0.1f];
    65 }
    62 }
    66 
    63 
    67 // cleans up everything
    64 // cleans up everything
    68 -(void) lateEngineLaunch {
    65 - (void)lateEngineLaunch {
    69     // notify views below that they are getting the spotlight again
    66     // notify views below that they are getting the spotlight again
    70     [[[HedgewarsAppDelegate sharedAppDelegate] uiwindow] makeKeyAndVisible];
    67     [[[HedgewarsAppDelegate sharedAppDelegate] uiwindow] makeKeyAndVisible];
    71     [callingController viewWillAppear:YES];
    68     [callingController viewWillAppear:YES];
    72 
    69 
    73     // remove completed games notification
    70     // remove completed games notification
    89 
    86 
    90     // restart music and we're done
    87     // restart music and we're done
    91     [[AudioManagerController mainManager] fadeInBackgroundMusic];
    88     [[AudioManagerController mainManager] fadeInBackgroundMusic];
    92     [HWUtils setGameStatus:gsNone];
    89     [HWUtils setGameStatus:gsNone];
    93     [HWUtils setGameType:gtNone];
    90     [HWUtils setGameType:gtNone];
    94     [self release];
       
    95 }
    91 }
    96 
    92 
    97 // main routine for calling the actual game engine
    93 // main routine for calling the actual game engine
    98 -(void) engineLaunch {
    94 - (void)engineLaunch {
    99     CGFloat width, height;
    95     CGFloat width, height;
   100     CGFloat screenScale = [[UIScreen mainScreen] safeScale];
    96     CGFloat screenScale = [[UIScreen mainScreen] safeScale];
   101     NSString *ipcString = [[NSString alloc] initWithFormat:@"%d",self.port];
    97     NSString *ipcString = [[NSString alloc] initWithFormat:@"%d",self.port];
   102     
    98     
   103     NSString *localeString = [[NSString alloc] initWithFormat:@"%@.txt", [HWUtils languageID]];
    99     NSString *localeString = [[NSString alloc] initWithFormat:@"%@.txt", [HWUtils languageID]];
   139                                       @"--raw-quality", rawQuality,
   135                                       @"--raw-quality", rawQuality,
   140                                       @"--locale", localeString,
   136                                       @"--locale", localeString,
   141                                       @"--prefix", resourcePath,
   137                                       @"--prefix", resourcePath,
   142                                       @"--user-prefix", documentsDirectory,
   138                                       @"--user-prefix", documentsDirectory,
   143                                       nil];
   139                                       nil];
   144     [verticalSize release];
       
   145     [horizontalSize release];
       
   146     [resourcePath release];
       
   147     [localeString release];
       
   148     [ipcString release];
       
   149 
   140 
   150     NSString *username = [settings objectForKey:@"username"];
   141     NSString *username = [settings objectForKey:@"username"];
   151     if ([username length] > 0) {
   142     if ([username length] > 0) {
   152         [gameParameters addObject:@"--nick"];
   143         [gameParameters addObject:@"--nick"];
   153         [gameParameters addObject: username];
   144         [gameParameters addObject: username];
   173 
   164 
   174     int argc = [gameParameters count];
   165     int argc = [gameParameters count];
   175     const char **argv = (const char **)malloc(sizeof(const char*)*argc);
   166     const char **argv = (const char **)malloc(sizeof(const char*)*argc);
   176     for (int i = 0; i < argc; i++)
   167     for (int i = 0; i < argc; i++)
   177         argv[i] = strdup([[gameParameters objectAtIndex:i] UTF8String]);
   168         argv[i] = strdup([[gameParameters objectAtIndex:i] UTF8String]);
   178     [gameParameters release];
       
   179 
   169 
   180     // this is the pascal function that starts the game
   170     // this is the pascal function that starts the game
   181     RunEngine(argc, argv);
   171     RunEngine(argc, argv);
   182 
   172 
   183     // cleanup
   173     // cleanup
   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",
   315                                     listOfTeams,@"teams_list",
   285                                     listOfTeams,@"teams_list",
   316                                     @"Default.plist",@"scheme",
   286                                     @"Default.plist",@"scheme",
   317                                     @"Default.plist",@"weapon",
   287                                     @"Default.plist",@"weapon",
   318                                     @"",@"mission_command",
   288                                     @"",@"mission_command",
   319                                     nil];
   289                                     nil];
   320     [listOfTeams release];
       
   321     [staticMapCommand release];
       
   322     [themeCommand release];
       
   323     [seedCmd release];
       
   324 
   290 
   325     // launch game
   291     // launch game
   326     [GameInterfaceBridge startLocalGame:gameDictionary];
   292     [GameInterfaceBridge startLocalGame:gameDictionary];
   327     [gameDictionary release];
       
   328 }
   293 }
   329 
   294 
   330 @end
   295 @end