project_files/HedgewarsMobile/Classes/otherSrc/CommodityFunctions.m
changeset 3546 ccf4854df294
parent 3545 b07ee704f35d
child 3547 02875b1145b7
equal deleted inserted replaced
3545:b07ee704f35d 3546:ccf4854df294
     1 //
       
     2 //  CommodityFunctions.m
       
     3 //  HedgewarsMobile
       
     4 //
       
     5 //  Created by Vittorio on 08/04/10.
       
     6 //  Copyright 2010 __MyCompanyName__. All rights reserved.
       
     7 //
       
     8 
       
     9 #import "CommodityFunctions.h"
       
    10 #import <mach/mach.h>
       
    11 #import <mach/mach_host.h>
       
    12 
       
    13 void createTeamNamed (NSString *nameWithoutExt) {
       
    14     NSString *teamsDirectory = TEAMS_DIRECTORY();
       
    15     
       
    16     if (![[NSFileManager defaultManager] fileExistsAtPath: teamsDirectory]) {
       
    17         [[NSFileManager defaultManager] createDirectoryAtPath:teamsDirectory 
       
    18                                   withIntermediateDirectories:NO 
       
    19                                                    attributes:nil 
       
    20                                                         error:NULL];
       
    21     }
       
    22     
       
    23     NSMutableArray *hedgehogs = [[NSMutableArray alloc] initWithCapacity: MAX_HOGS];
       
    24     
       
    25     for (int i = 0; i < MAX_HOGS; i++) {
       
    26         NSString *hogName = [[NSString alloc] initWithFormat:@"hedgehog %d",i];
       
    27         NSDictionary *hog = [[NSDictionary alloc] initWithObjectsAndKeys: [NSNumber numberWithInt:0],@"level",
       
    28                              hogName,@"hogname", @"NoHat",@"hat", nil];
       
    29         [hogName release];
       
    30         [hedgehogs addObject:hog];
       
    31         [hog release];
       
    32     }
       
    33     
       
    34     NSDictionary *theTeam = [[NSDictionary alloc] initWithObjectsAndKeys:@"0",@"hash", nameWithoutExt,@"teamname",
       
    35                              @"Statue",@"grave", @"Plane",@"fort", @"Default",@"voicepack",
       
    36                              @"hedgewars",@"flag", hedgehogs,@"hedgehogs", nil];
       
    37     [hedgehogs release];
       
    38     
       
    39     NSString *teamFile = [[NSString alloc] initWithFormat:@"%@/%@.plist", teamsDirectory, nameWithoutExt];
       
    40 
       
    41     [theTeam writeToFile:teamFile atomically:YES];
       
    42     [teamFile release];
       
    43     [theTeam release];
       
    44 }
       
    45 
       
    46 void createWeaponNamed (NSString *nameWithoutExt) {
       
    47     NSString *weaponsDirectory = WEAPONS_DIRECTORY();
       
    48 
       
    49     if (![[NSFileManager defaultManager] fileExistsAtPath: weaponsDirectory]) {
       
    50         [[NSFileManager defaultManager] createDirectoryAtPath:weaponsDirectory 
       
    51                                   withIntermediateDirectories:NO 
       
    52                                                    attributes:nil 
       
    53                                                         error:NULL];
       
    54     }
       
    55     
       
    56     NSDictionary *theWeapon = [[NSDictionary alloc] initWithObjectsAndKeys:
       
    57                                @"9391929422199121032235111001201000000211190911",@"ammostore_initialqt",
       
    58                                @"0405040541600655546554464776576666666155501000",@"ammostore_probability",
       
    59                                @"0000000000000205500000040007004000000000200000",@"ammostore_delay",
       
    60                                @"1311110312111111123114111111111111111211101111",@"ammostore_crate", nil];
       
    61     
       
    62     NSString *weaponFile = [[NSString alloc] initWithFormat:@"%@/%@.plist", weaponsDirectory, nameWithoutExt];
       
    63     
       
    64     [theWeapon writeToFile:weaponFile atomically:YES];
       
    65     [weaponFile release];
       
    66     [theWeapon release];
       
    67 }
       
    68 
       
    69 void createSchemeNamed (NSString *nameWithoutExt) {
       
    70     NSString *schemesDirectory = SCHEMES_DIRECTORY();
       
    71     
       
    72     if (![[NSFileManager defaultManager] fileExistsAtPath: schemesDirectory]) {
       
    73         [[NSFileManager defaultManager] createDirectoryAtPath:schemesDirectory 
       
    74                                   withIntermediateDirectories:NO 
       
    75                                                    attributes:nil 
       
    76                                                         error:NULL];
       
    77     }
       
    78     
       
    79     NSArray *theScheme = [[NSArray alloc] initWithObjects:
       
    80                           [NSNumber numberWithBool:NO],    //fortmode
       
    81                           [NSNumber numberWithBool:NO],    //divideteam
       
    82                           [NSNumber numberWithBool:NO],    //solidland
       
    83                           [NSNumber numberWithBool:NO],    //addborder
       
    84                           [NSNumber numberWithBool:NO],    //lowgravity
       
    85                           [NSNumber numberWithBool:NO],    //lasersight
       
    86                           [NSNumber numberWithBool:NO],    //invulnerable
       
    87                           [NSNumber numberWithBool:NO],    //addmines
       
    88                           [NSNumber numberWithBool:NO],    //vampirism
       
    89                           [NSNumber numberWithBool:NO],    //karma
       
    90                           [NSNumber numberWithBool:NO],    //artillery
       
    91                           [NSNumber numberWithBool:YES],   //randomorder
       
    92                           [NSNumber numberWithBool:NO],    //king
       
    93                           [NSNumber numberWithBool:NO],    //placehedgehogs
       
    94                           [NSNumber numberWithBool:NO],    //clansharesammo
       
    95                           [NSNumber numberWithBool:NO],    //disablegirders
       
    96                           [NSNumber numberWithBool:NO],    //disablelandobjects
       
    97                           [NSNumber numberWithInt:100],    //damagemodifier
       
    98                           [NSNumber numberWithInt:45],     //turntime
       
    99                           [NSNumber numberWithInt:100],    //initialhealth
       
   100                           [NSNumber numberWithInt:15],     //suddendeathtimeout
       
   101                           [NSNumber numberWithInt:5],      //cratedrops
       
   102                           [NSNumber numberWithInt:3],      //minestime
       
   103                           [NSNumber numberWithInt:4],      //mines
       
   104                           [NSNumber numberWithInt:0],      //dudmines
       
   105                           [NSNumber numberWithInt:2],      //explosives
       
   106                           nil];
       
   107     
       
   108     NSString *schemeFile = [[NSString alloc] initWithFormat:@"%@/%@.plist", schemesDirectory, nameWithoutExt];
       
   109 
       
   110     [theScheme writeToFile:schemeFile atomically:YES];
       
   111     [schemeFile release];
       
   112     [theScheme release];
       
   113 }
       
   114 
       
   115 BOOL rotationManager (UIInterfaceOrientation interfaceOrientation) {
       
   116     if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
       
   117         return YES;
       
   118     else
       
   119         return (interfaceOrientation == UIInterfaceOrientationLandscapeRight) ||
       
   120                (interfaceOrientation == UIInterfaceOrientationLandscapeLeft);  
       
   121 }
       
   122 
       
   123 NSInteger randomPort () {
       
   124     srandom(time(NULL));
       
   125     return (random() % 64511) + 1024;
       
   126 }
       
   127 
       
   128 void popError (const char *title, const char *message) {
       
   129     UIAlertView *alert = [[UIAlertView alloc] initWithTitle:[NSString stringWithUTF8String:title]
       
   130                                                     message:[NSString stringWithUTF8String:message]
       
   131                                                    delegate:nil
       
   132                                           cancelButtonTitle:@"Ok"
       
   133                                           otherButtonTitles:nil];
       
   134     [alert show];
       
   135     [alert release];
       
   136 }
       
   137 
       
   138 // by http://landonf.bikemonkey.org/code/iphone/Determining_Available_Memory.20081203.html
       
   139 void print_free_memory () {
       
   140     mach_port_t host_port;
       
   141     mach_msg_type_number_t host_size;
       
   142     vm_size_t pagesize;
       
   143     
       
   144     host_port = mach_host_self();
       
   145     host_size = sizeof(vm_statistics_data_t) / sizeof(integer_t);
       
   146     host_page_size(host_port, &pagesize);        
       
   147  
       
   148     vm_statistics_data_t vm_stat;
       
   149               
       
   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");
       
   152  
       
   153     /* Stats in bytes */ 
       
   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;
       
   156     natural_t mem_total = mem_used + mem_free;
       
   157     DLog(@"used: %u free: %u total: %u", mem_used, mem_free, mem_total);
       
   158 }