project_files/HedgewarsMobile/Classes/CommodityFunctions.m
changeset 3697 d5b30d6373fc
parent 3670 4c673e57f0d7
child 3737 2ba6ac8a114b
equal deleted inserted replaced
3695:c11abf387a7d 3697:d5b30d6373fc
    12 #import <mach/mach.h>
    12 #import <mach/mach.h>
    13 #import <mach/mach_host.h>
    13 #import <mach/mach_host.h>
    14 
    14 
    15 void createTeamNamed (NSString *nameWithoutExt) {
    15 void createTeamNamed (NSString *nameWithoutExt) {
    16     NSString *teamsDirectory = TEAMS_DIRECTORY();
    16     NSString *teamsDirectory = TEAMS_DIRECTORY();
    17     
    17 
    18     if (![[NSFileManager defaultManager] fileExistsAtPath: teamsDirectory]) {
    18     if (![[NSFileManager defaultManager] fileExistsAtPath: teamsDirectory]) {
    19         [[NSFileManager defaultManager] createDirectoryAtPath:teamsDirectory 
    19         [[NSFileManager defaultManager] createDirectoryAtPath:teamsDirectory
    20                                   withIntermediateDirectories:NO 
    20                                   withIntermediateDirectories:NO
    21                                                    attributes:nil 
    21                                                    attributes:nil
    22                                                         error:NULL];
    22                                                         error:NULL];
    23     }
    23     }
    24     
    24 
    25     NSMutableArray *hedgehogs = [[NSMutableArray alloc] initWithCapacity: MAX_HOGS];
    25     NSMutableArray *hedgehogs = [[NSMutableArray alloc] initWithCapacity: MAX_HOGS];
    26     
    26 
    27     for (int i = 0; i < MAX_HOGS; i++) {
    27     for (int i = 0; i < MAX_HOGS; i++) {
    28         NSString *hogName = [[NSString alloc] initWithFormat:@"hedgehog %d",i];
    28         NSString *hogName = [[NSString alloc] initWithFormat:@"hedgehog %d",i];
    29         NSDictionary *hog = [[NSDictionary alloc] initWithObjectsAndKeys: [NSNumber numberWithInt:0],@"level",
    29         NSDictionary *hog = [[NSDictionary alloc] initWithObjectsAndKeys: [NSNumber numberWithInt:0],@"level",
    30                              hogName,@"hogname", @"NoHat",@"hat", nil];
    30                              hogName,@"hogname", @"NoHat",@"hat", nil];
    31         [hogName release];
    31         [hogName release];
    32         [hedgehogs addObject:hog];
    32         [hedgehogs addObject:hog];
    33         [hog release];
    33         [hog release];
    34     }
    34     }
    35     
    35 
    36     NSDictionary *theTeam = [[NSDictionary alloc] initWithObjectsAndKeys:@"0",@"hash",
    36     NSDictionary *theTeam = [[NSDictionary alloc] initWithObjectsAndKeys:@"0",@"hash",
    37                              @"Statue",@"grave", @"Plane",@"fort", @"Default",@"voicepack",
    37                              @"Statue",@"grave", @"Plane",@"fort", @"Default",@"voicepack",
    38                              @"hedgewars",@"flag", hedgehogs,@"hedgehogs", nil];
    38                              @"hedgewars",@"flag", hedgehogs,@"hedgehogs", nil];
    39     [hedgehogs release];
    39     [hedgehogs release];
    40     
    40 
    41     NSString *teamFile = [[NSString alloc] initWithFormat:@"%@/%@.plist", teamsDirectory, nameWithoutExt];
    41     NSString *teamFile = [[NSString alloc] initWithFormat:@"%@/%@.plist", teamsDirectory, nameWithoutExt];
    42 
    42 
    43     [theTeam writeToFile:teamFile atomically:YES];
    43     [theTeam writeToFile:teamFile atomically:YES];
    44     [teamFile release];
    44     [teamFile release];
    45     [theTeam release];
    45     [theTeam release];
    47 
    47 
    48 void createWeaponNamed (NSString *nameWithoutExt) {
    48 void createWeaponNamed (NSString *nameWithoutExt) {
    49     NSString *weaponsDirectory = WEAPONS_DIRECTORY();
    49     NSString *weaponsDirectory = WEAPONS_DIRECTORY();
    50 
    50 
    51     if (![[NSFileManager defaultManager] fileExistsAtPath: weaponsDirectory]) {
    51     if (![[NSFileManager defaultManager] fileExistsAtPath: weaponsDirectory]) {
    52         [[NSFileManager defaultManager] createDirectoryAtPath:weaponsDirectory 
    52         [[NSFileManager defaultManager] createDirectoryAtPath:weaponsDirectory
    53                                   withIntermediateDirectories:NO 
    53                                   withIntermediateDirectories:NO
    54                                                    attributes:nil 
    54                                                    attributes:nil
    55                                                         error:NULL];
    55                                                         error:NULL];
    56     }
    56     }
    57     
    57 
    58     NSDictionary *theWeapon = [[NSDictionary alloc] initWithObjectsAndKeys:
    58     NSDictionary *theWeapon = [[NSDictionary alloc] initWithObjectsAndKeys:
    59                                [NSNumber numberWithInt:CURRENT_AMMOSIZE],@"version",
    59                                [NSNumber numberWithInt:CURRENT_AMMOSIZE],@"version",
    60                                @"9391929422199121032235111001201000000211110111",@"ammostore_initialqt",
    60                                @"9391929422199121032235111001201000000211110111",@"ammostore_initialqt",
    61                                @"0405040541600655546554464776576666666155510111",@"ammostore_probability",
    61                                @"0405040541600655546554464776576666666155510111",@"ammostore_probability",
    62                                @"0000000000000205500000040007004000000000200000",@"ammostore_delay",
    62                                @"0000000000000205500000040007004000000000200000",@"ammostore_delay",
    63                                @"1311110312111111123114111111111111111211111111",@"ammostore_crate", nil];
    63                                @"1311110312111111123114111111111111111211111111",@"ammostore_crate", nil];
    64     
    64 
    65     NSString *weaponFile = [[NSString alloc] initWithFormat:@"%@/%@.plist", weaponsDirectory, nameWithoutExt];
    65     NSString *weaponFile = [[NSString alloc] initWithFormat:@"%@/%@.plist", weaponsDirectory, nameWithoutExt];
    66     
    66 
    67     [theWeapon writeToFile:weaponFile atomically:YES];
    67     [theWeapon writeToFile:weaponFile atomically:YES];
    68     [weaponFile release];
    68     [weaponFile release];
    69     [theWeapon release];
    69     [theWeapon release];
    70 }
    70 }
    71 
    71 
    72 void createSchemeNamed (NSString *nameWithoutExt) {
    72 void createSchemeNamed (NSString *nameWithoutExt) {
    73     NSString *schemesDirectory = SCHEMES_DIRECTORY();
    73     NSString *schemesDirectory = SCHEMES_DIRECTORY();
    74     
    74 
    75     if (![[NSFileManager defaultManager] fileExistsAtPath: schemesDirectory]) {
    75     if (![[NSFileManager defaultManager] fileExistsAtPath: schemesDirectory]) {
    76         [[NSFileManager defaultManager] createDirectoryAtPath:schemesDirectory 
    76         [[NSFileManager defaultManager] createDirectoryAtPath:schemesDirectory
    77                                   withIntermediateDirectories:NO 
    77                                   withIntermediateDirectories:NO
    78                                                    attributes:nil 
    78                                                    attributes:nil
    79                                                         error:NULL];
    79                                                         error:NULL];
    80     }
    80     }
    81     
    81 
    82     NSArray *theScheme = [[NSArray alloc] initWithObjects:
    82     NSArray *theScheme = [[NSArray alloc] initWithObjects:
    83                           [NSNumber numberWithBool:NO],    //fortmode
    83                           [NSNumber numberWithBool:NO],    //fortmode
    84                           [NSNumber numberWithBool:NO],    //divideteam
    84                           [NSNumber numberWithBool:NO],    //divideteam
    85                           [NSNumber numberWithBool:NO],    //solidland
    85                           [NSNumber numberWithBool:NO],    //solidland
    86                           [NSNumber numberWithBool:NO],    //addborder
    86                           [NSNumber numberWithBool:NO],    //addborder
   105                           [NSNumber numberWithInt:3],      //minestime
   105                           [NSNumber numberWithInt:3],      //minestime
   106                           [NSNumber numberWithInt:4],      //mines
   106                           [NSNumber numberWithInt:4],      //mines
   107                           [NSNumber numberWithInt:0],      //dudmines
   107                           [NSNumber numberWithInt:0],      //dudmines
   108                           [NSNumber numberWithInt:2],      //explosives
   108                           [NSNumber numberWithInt:2],      //explosives
   109                           nil];
   109                           nil];
   110     
   110 
   111     NSString *schemeFile = [[NSString alloc] initWithFormat:@"%@/%@.plist", schemesDirectory, nameWithoutExt];
   111     NSString *schemeFile = [[NSString alloc] initWithFormat:@"%@/%@.plist", schemesDirectory, nameWithoutExt];
   112 
   112 
   113     [theScheme writeToFile:schemeFile atomically:YES];
   113     [theScheme writeToFile:schemeFile atomically:YES];
   114     [schemeFile release];
   114     [schemeFile release];
   115     [theScheme release];
   115     [theScheme release];
   116 }
   116 }
   117 
   117 
   118 BOOL rotationManager (UIInterfaceOrientation interfaceOrientation) {
   118 BOOL rotationManager (UIInterfaceOrientation interfaceOrientation) {
   119     return (interfaceOrientation == UIInterfaceOrientationLandscapeRight) ||
   119     return (interfaceOrientation == UIInterfaceOrientationLandscapeRight) ||
   120            (interfaceOrientation == UIInterfaceOrientationLandscapeLeft);  
   120            (interfaceOrientation == UIInterfaceOrientationLandscapeLeft);
   121 }
   121 }
   122 
   122 
   123 NSInteger randomPort () {
   123 NSInteger randomPort () {
   124     srandom(time(NULL));
   124     srandom(time(NULL));
   125     return (random() % 64511) + 1024;
   125     return (random() % 64511) + 1024;
   138 // by http://landonf.bikemonkey.org/code/iphone/Determining_Available_Memory.20081203.html
   138 // by http://landonf.bikemonkey.org/code/iphone/Determining_Available_Memory.20081203.html
   139 void print_free_memory () {
   139 void print_free_memory () {
   140     mach_port_t host_port;
   140     mach_port_t host_port;
   141     mach_msg_type_number_t host_size;
   141     mach_msg_type_number_t host_size;
   142     vm_size_t pagesize;
   142     vm_size_t pagesize;
   143     
   143 
   144     host_port = mach_host_self();
   144     host_port = mach_host_self();
   145     host_size = sizeof(vm_statistics_data_t) / sizeof(integer_t);
   145     host_size = sizeof(vm_statistics_data_t) / sizeof(integer_t);
   146     host_page_size(host_port, &pagesize);        
   146     host_page_size(host_port, &pagesize);
   147  
   147 
   148     vm_statistics_data_t vm_stat;
   148     vm_statistics_data_t vm_stat;
   149               
   149 
   150     if (host_statistics(host_port, HOST_VM_INFO, (host_info_t)&vm_stat, &host_size) != KERN_SUCCESS)
   150     if (host_statistics(host_port, HOST_VM_INFO, (host_info_t)&vm_stat, &host_size) != KERN_SUCCESS)
   151         DLog(@"Failed to fetch vm statistics");
   151         DLog(@"Failed to fetch vm statistics");
   152  
   152 
   153     /* Stats in bytes */ 
   153     /* Stats in bytes */
   154     natural_t mem_used = (vm_stat.active_count + vm_stat.inactive_count + vm_stat.wire_count) * pagesize;
   154     natural_t mem_used = (vm_stat.active_count + vm_stat.inactive_count + vm_stat.wire_count) * pagesize;
   155     natural_t mem_free = vm_stat.free_count * pagesize;
   155     natural_t mem_free = vm_stat.free_count * pagesize;
   156     natural_t mem_total = mem_used + mem_free;
   156     natural_t mem_total = mem_used + mem_free;
   157     DLog(@"used: %u free: %u total: %u", mem_used, mem_free, mem_total);
   157     DLog(@"used: %u free: %u total: %u", mem_used, mem_free, mem_total);
   158 }
   158 }
   162 }
   162 }
   163 
   163 
   164 NSString *modelType () {
   164 NSString *modelType () {
   165     size_t size;
   165     size_t size;
   166     // set 'oldp' parameter to NULL to get the size of the data returned so we can allocate appropriate amount of space
   166     // set 'oldp' parameter to NULL to get the size of the data returned so we can allocate appropriate amount of space
   167     sysctlbyname("hw.machine", NULL, &size, NULL, 0); 
   167     sysctlbyname("hw.machine", NULL, &size, NULL, 0);
   168     char *name = (char *)malloc(sizeof(char) * size);
   168     char *name = (char *)malloc(sizeof(char) * size);
   169     // get the platform name
   169     // get the platform name
   170     sysctlbyname("hw.machine", name, &size, NULL, 0);
   170     sysctlbyname("hw.machine", name, &size, NULL, 0);
   171     NSString *modelId = [NSString stringWithUTF8String:name];
   171     NSString *modelId = [NSString stringWithUTF8String:name];
   172     free(name);
   172     free(name);
   173     
   173 
   174     return modelId;
   174     return modelId;
   175 }
   175 }