OSAtomicIncrement32Barrier deprecated, how to solve this issue? - swift

i'm trying Parse server on my ios app using Xcode14 and ios 16, I installed the pod Parse, but when I run the code I get the following warning message:
'OSAtomicIncrement32Barrier' is deprecated: first deprecated in iOS 10.0 - Use atomic_fetch_add() from <stdatomic.h> instead
Any help how I could fix this issue:
+ (instancetype)taskForCompletionOfAllTasks:(nullable NSArray<BFTask *> *)tasks {
__block int32_t total = (int32_t)tasks.count;
if (total == 0) {
return [self taskWithResult:nil];
}
__block int32_t cancelled = 0;
NSObject *lock = [[NSObject alloc] init];
NSMutableArray *errors = [NSMutableArray array];
BFTaskCompletionSource *tcs = [BFTaskCompletionSource taskCompletionSource];
for (BFTask *task in tasks) {
[task continueWithBlock:^id(BFTask *t) {
if (t.error) {
#synchronized (lock) {
[errors addObject:t.error];
}
} else if (t.cancelled) {
OSAtomicIncrement32Barrier(&cancelled); // error is here
}
if (OSAtomicDecrement32Barrier(&total) == 0) { // error is here
if (errors.count > 0) {
if (errors.count == 1) {
tcs.error = [errors firstObject];
} else {
NSError *error = [NSError errorWithDomain:BFTaskErrorDomain
code:kBFMultipleErrorsError
userInfo:#{ BFTaskMultipleErrorsUserInfoKey: errors }];
tcs.error = error;
}
} else if (cancelled > 0) {
[tcs cancel];
} else {
tcs.result = nil;
}
}
return nil;
}];
}
return tcs.task;
}

C11 has support for atomics so you can do something like:
#include <stdatomic.h>
// Declare an atomic int and initialize it to zero
atomic_int total;
atomic_init(&total, 0);
// Note: C17 and C23 allow safe direct initialization
// i.e. atomic_int total = 0;
// Operations such as ++total and --total are atomic
++total;
// Or alternatively
atomic_fetch_add(&total, 1);
atomic_fetch_sub(&total, 1);

Related

Get priority and uptime from pid? ( iOS )

This is for an app intended for the app store.
Using the code from here, I can get a list of running processes and their pids. However, I've found several apps in the appstore (like this one that have also retrieved each process's priority and start time.
(Note: I don't care whether it's uptime, for how long the process has been active, or the wall clock date/time the process started).
Is there any documented way to do this?
Here is the code to get all the process related info you want such as Name, Priority,StartDate, ParentID,Status. Here is the link to get full resource with demo.
// List of process information including PID's, Names, PPID's, and Status'
+ (NSMutableArray *)processesInformation {
// Get the list of processes and all information about them
#try {
// Make a new integer array holding all the kernel processes
int mib[4] = {CTL_KERN, KERN_PROC, KERN_PROC_ALL, 0};
// Make a new size of 4
size_t miblen = 4;
size_t size = 0;
int st = sysctl(mib, miblen, NULL, &size, NULL, 0);
// Set up the processes and new process struct
struct kinfo_proc *process = NULL;
struct kinfo_proc *newprocess = NULL;
// do, while loop rnning through all the processes
do {
size += size / 10;
newprocess = realloc(process, size);
if (!newprocess) {
if (process) free(process);
// Error
return nil;
}
process = newprocess;
st = sysctl(mib, miblen, process, &size, NULL, 0);
} while (st == -1 && errno == ENOMEM);
if (st == 0) {
if (size % sizeof(struct kinfo_proc) == 0) {
int nprocess = size / sizeof(struct kinfo_proc);
if (nprocess) {
NSMutableArray *array = [[NSMutableArray alloc] init];
for (int i = nprocess - 1; i >= 0; i--) {
NSString *processID = [[NSString alloc] initWithFormat:#"%d", process[i].kp_proc.p_pid];
NSString *processName = [[NSString alloc] initWithFormat:#"%s", process[i].kp_proc.p_comm];
NSString *processPriority = [[NSString alloc] initWithFormat:#"%d", process[i].kp_proc.p_priority];
NSDate *processStartDate = [NSDate dateWithTimeIntervalSince1970:process[i].kp_proc.p_un.__p_starttime.tv_sec];
NSString *processParentID = [[NSString alloc] initWithFormat:#"%d", [self parentPIDForProcess:(int)process[i].kp_proc.p_pid]];
NSString *processStatus = [[NSString alloc] initWithFormat:#"%d", (int)process[i].kp_proc.p_stat];
NSString *processFlags = [[NSString alloc] initWithFormat:#"%d", (int)process[i].kp_proc.p_flag];
// Check to make sure all values are valid (if not, make them)
if (processID == nil || processID.length <= 0) {
// Invalid value
processID = #"Unkown";
}
if (processName == nil || processName.length <= 0) {
// Invalid value
processName = #"Unkown";
}
if (processPriority == nil || processPriority.length <= 0) {
// Invalid value
processPriority = #"Unkown";
}
if (processStartDate == nil) {
// Invalid value
processStartDate = [NSDate date];
}
if (processParentID == nil || processParentID.length <= 0) {
// Invalid value
processParentID = #"Unkown";
}
if (processStatus == nil || processStatus.length <= 0) {
// Invalid value
processStatus = #"Unkown";
}
if (processFlags == nil || processFlags.length <= 0) {
// Invalid value
processFlags = #"Unkown";
}
// Create an array of the objects
NSArray *ItemArray = [NSArray arrayWithObjects:processID, processName, processPriority, processStartDate, processParentID, processStatus, processFlags, nil];
// Create an array of keys
NSArray *KeyArray = [NSArray arrayWithObjects:#"PID", #"Name", #"Priority", #"StartDate", #"ParentID", #"Status", #"Flags", nil];
// Create the dictionary
NSDictionary *dict = [[NSDictionary alloc] initWithObjects:ItemArray forKeys:KeyArray];
// Add the objects to the array
[array addObject:dict];
}
// Make sure the array is usable
if (array.count <= 0) {
// Error, nothing in array
return nil;
}
// Free the process
free(process);
// Successful
return array;
}
}
}
// Something failed
return nil;
}
#catch (NSException * ex) {
// Error
return nil;
}
}
// Parent ID for a certain PID
+ (int)parentPIDForProcess:(int)pid {
// Get the parent ID for a certain process
#try {
// Set up the variables
struct kinfo_proc info;
size_t length = sizeof(struct kinfo_proc);
int mib[4] = { CTL_KERN, KERN_PROC, KERN_PROC_PID, pid };
if (sysctl(mib, 4, &info, &length, NULL, 0) < 0)
// Unknown value
return -1;
if (length == 0)
// Unknown value
return -1;
// Make an int for the PPID
int PPID = info.kp_eproc.e_ppid;
// Check to make sure it's valid
if (PPID <= 0) {
// No PPID found
return -1;
}
// Successful
return PPID;
}
#catch (NSException *exception) {
// Error
return -1;
}
}

How Can i Know if GKAchievement is Completed?

I have built objectives for my game and everything works just fine accept the part of making the objectives not be called any more after any of them is completed.
I know there is a Property of the GKAchievement Class "completed" which is a boolean that returns yes when the Achievement is 100 percent done.
here is the method that called when a Achievement is 100 percent done it passes id which is the Achievement identifier and report the acheeee :
- (void)AchivmentDidAchive:(id)Achivment{
NSString *identifier = Achivment;
NSLog(#"%#",identifier);
self.achivment = [[GKAchievement alloc]initWithIdentifier:identifier];
self.achivment.showsCompletionBanner = YES;
if (!self.achivment.completed) {
self.achivment.percentComplete = 100;
NSLog(#"Reproting!");
[self.achivment reportAchievementWithCompletionHandler: ^(NSError *error)
{
}];
}
else {
NSLog(#"Achivment Completed!");
} }
what I am trying to do here is to set the percent completed to 100 and report it so in the next time ie want get called again.
but it always works... any better idea for how to handle this?
in interface add variable & property:
NSMutableDictionary *earnedAchievementCache;
#property (nonatomic, retain)NSMutableDictionary *earnedAchievementCache;
in .m:
#synthesize earnedAchievementCache;
- (void) submitAchievement: (NSString*) identifier percentComplete: (double) percentComplete
{
if(self.earnedAchievementCache == NULL)
{
[GKAchievement loadAchievementsWithCompletionHandler: ^(NSArray *scores, NSError *error)
{
if(error == NULL)
{
NSMutableDictionary* tempCache= [NSMutableDictionary dictionaryWithCapacity: [scores count]];
for (GKAchievement* score in scores)
{
[tempCache setObject: score forKey: score.identifier];
}
self.earnedAchievementCache= tempCache;
[self submitAchievement: identifier percentComplete: percentComplete];
}
}];
}
else
{
GKAchievement* achievement= [self.earnedAchievementCache objectForKey: identifier];
if(achievement != NULL)
{
if((achievement.percentComplete >= 100.0) || (achievement.percentComplete >= percentComplete))
{
achievement= NULL;
}
achievement.percentComplete= percentComplete;
}
else
{
achievement= [[[GKAchievement alloc] initWithIdentifier: identifier] autorelease];
achievement.percentComplete= percentComplete;
[self.earnedAchievementCache setObject: achievement forKey: achievement.identifier];
}
if(achievement!= NULL)
{
//Submit the Achievement...
if (achievement.percentComplete>=100) {
//show banner
achievement.showsCompletionBanner = YES; //only in IOS 5+
}
[achievement reportAchievementWithCompletionHandler: ^(NSError *error)
{
if (error!=NULL){
NSLog(#"Error!!");
} else NSLog(#"all is well");
}];
}
}
}
in dealloc :
[self.earnedAchievementCache release];
i'm using the cache to not submit scores already submitted / completed
PS: the code is perfect just copy and paste it into your class and it will work
this is what I use in my helper Game Center class:
-(void) reportAchievementWithID:(NSString*) AchievementID {
[GKAchievement loadAchievementsWithCompletionHandler:^(NSArray *achievements, NSError *error) {
if(error) NSLog(#"error");
for (GKAchievement *ach in achievements) {
if([ach.identifier isEqualToString:AchievementID]) { //already submitted
NSLog(#"Already submitted");
return ;
}
}
GKAchievement *achievementToSend = [[GKAchievement alloc] initWithIdentifier:AchievementID];
achievementToSend.percentComplete = 100;
achievementToSend.showsCompletionBanner = YES;
[achievementToSend reportAchievementWithCompletionHandler:NULL];
}];
}
note: I don't use percentages in my achievements, so you'd need to modify things a little bit if you do.

NSPOSIXErrorDomain Code=12 "Cannot allocate memory" in 3G network

I'm trying to send a file on FTP server in my iPhone application.
Everything seems to be okay in WiFi and GSM:EDGE network, but in 3G network an error appears (not always, but very often):
Error Domain=NSPOSIXErrorDomain
Code=12 "The operation couldn’t be
completed. Cannot allocate memory"
Below the code where the error appears:
- (void)stream:(NSStream*)aStream handleEvent:(NSStreamEvent)eventCode {
switch( eventCode ) {
case NSStreamEventHasSpaceAvailable: {
if( _readDataOffset == _readDataLimit ) {
NSInteger readDataLen = [_readStream read:[_readData mutableBytes] maxLength:kReadDataLength];
NSLog(#"readDataLen is %d",readDataLen);
if( -1 == readDataLen ) {
_error = [[_readStream streamError] retain];
_keepRunning = NO;
} else if( 0 == readDataLen ) {
_keepRunning = NO;
} else {
_readDataOffset = 0;
_readDataLimit = readDataLen;
}
}
if( _readDataOffset != _readDataLimit ) {
NSOutputStream* writeStream = (NSOutputStream*)aStream;
uint8_t *buffer = (void *)[_readData bytes];
// vvvv and here the value of writtenDataLen is often -1 (but only on 3G network)
NSInteger writtenDataLen = [writeStream write:&buffer[_readDataOffset] maxLength:_readDataLimit - _readDataOffset];
if( writtenDataLen > 0 ) {
_readDataOffset += writtenDataLen;
_writtenDataLen += writtenDataLen;
[self ftpPutDidWriteInternal];
} else if( -1 == writtenDataLen ) {
_error = [[writeStream streamError] retain];
_keepRunning = NO;
}
}
} break;
case NSStreamEventErrorOccurred: {
_error = [aStream.streamError retain];
_keepRunning = NO;
} break;
}
}
What can be important, the whole sending is done in separate thread which has it's own NSAutoreleasePool.
Is there anyone who got the issue? Any suggestion? I would be appreciate.
UPDATE:
I've just checked that popular iPhone application "FTP On The Go" has got the same (?) issue during sending a file in 3G network! There is no error handled, but the transfer stops.
UPDATE 2:
I can't believe it, but it's true: SimpleFTPSample from Apple is also affected with the issue.
And here it is - the solution (or rather workaround):
you should set property of writeStream to false, to switch off default persistant connection
CFWriteStreamSetProperty( (CFWriteStreamRef)writeStreamRef, kCFStreamPropertyFTPAttemptPersistentConnection, kCFBooleanFalse ) ;
Have resolved this error with using operation for request (NSMutableUrlConnection) with #autorelease{} for main function
- (void)main
NSURLConnection* connection;
#autoreleasepool //urgently needed for 3G upload
{
self.currentRequest = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:#"test.php"]];
[self.currentRequest setHTTPMethod:#"PUT"];
[self.currentRequest setHTTPBody:self.data];//inpustStream doesn't work
connection = [NSURLConnection connectionWithRequest:self.currentRequest delegate:self];
[connection start];
}//end autorelease pool
do
{
[[NSRunLoop currentRunLoop] runMode: NSDefaultRunLoopMode beforeDate: [NSDate distantFuture]];
if ([self isCancelled])
{
connection = nil;
isFailed = YES;
break;
}
self.status(statusUpdateMessage);
}
while (!isFailed && !isCompleted);
[timer invalidate];//test
timer = nil;
//corresponding of status via blocks
self.completed(!isFailed);
self.status(isFailed ? errorMessage : #"Completed");
if (isFailed)
{
self.failed(errorMessage != nil ? errorMessage : #"Undefined error");
}
self.data = nil;
self.currentRequest = nil;
connection = nil;
}

Memory leak issue

In my class Orders, I've 2 functions 'generateOrderSummary' & 'createOrderSummaryItem':
- (OrderSummary *)generateOrderSummary {
//Cleaning the references but OrderSummaryItem leaks
[self.currentOrderSummary removeAllOrderedItems];
self.currentOrderSummary = nil;
for(MenuItem *menuItem in self.selectedItems) {
OrderSummaryItem *orderSummaryItem = [self createOrderSummaryItem:menuItem];
//Retain count = 1
[orderSummary addOrderedItem:orderSummaryItem withServiceStationCode:menuItem.serviceStationCode withCategory:menuItem.category withCourseOrder:menuItem.courseOrder];
//Retain count = 5;
}
self.currentOrderSummary = orderSummary;
[orderSummary release];
orderSummary = nil;
return self.currentOrderSummary;
}
- (OrderSummaryItem *) createOrderSummaryItem:(MenuItem *)menuItem {
OrderSummaryItem *summaryItem = [[[OrderSummaryItem alloc]init] autorelease];
//Set summaryItem properties
return summaryItem;
}
This' OrderSummary class with 3 NSMutableDictionaries and 1 NSMutableArray all keeping reference to the OrderSummaryItem object.
-(void)addOrderedItem:(OrderSummaryItem *)orderedItem withServiceStationCode:(NSString *)serviceStationCode withCategory:(NSString *)category withCourseOrder:(NSString *)courseOrder {
if (self.serviceStationDict == nil) {
self.serviceStationDict = [NSMutableDictionary dictionaryWithCapacity:1];
}
if (self.categoryDict == nil) {
self.categoryDict = [NSMutableDictionary dictionaryWithCapacity:1];
}
if (self.courseOrderDict == nil) {
self.courseOrderDict = [NSMutableDictionary dictionaryWithCapacity:1];
}
if(self.orderedItems == nil) {
self.orderedItems = [NSMutableArray arrayWithCapacity:1];
}
if(serviceStationCode != nil) {
NSMutableArray *orderedItemsForServiceStation = [self.serviceStationDict objectForKey:serviceStationCode];
if (orderedItemsForServiceStation != nil) {
[orderedItemsForServiceStation addObject:orderedItem];
}
else {
orderedItemsForServiceStation = [NSMutableArray arrayWithCapacity:1];
[orderedItemsForServiceStation addObject:orderedItem];
[self.serviceStationDict setObject:orderedItemsForServiceStation forKey:serviceStationCode];
//Retain count = 2
}
}
if(category != nil) {
NSMutableArray *orderedItemsForCategory = [self.categoryDict objectForKey:category];
if (orderedItemsForCategory != nil) {
[orderedItemsForCategory addObject:orderedItem];
}
else {
orderedItemsForCategory = [NSMutableArray arrayWithCapacity:1];
[orderedItemsForCategory addObject:orderedItem];
[self.categoryDict setObject:orderedItemsForCategory forKey:category];
//Retain count = 3
}
}
if(courseOrder != nil) {
NSMutableArray *orderedItemsForCourseOrder = [self.courseOrderDict objectForKey:courseOrder];
if (orderedItemsForCourseOrder != nil) {
[orderedItemsForCourseOrder addObject:orderedItem];
}
else {
orderedItemsForCourseOrder = [NSMutableArray arrayWithCapacity:1];
[orderedItemsForCourseOrder addObject:orderedItem];
[self.courseOrderDict setObject:orderedItemsForCourseOrder forKey:courseOrder];
//Retain count = 4
}
}
[self.orderedItems addObject:orderedItem];
//Retain count = 5
}
-(void)removeAllOrderedItems{
[self.serviceStationDict removeAllObjects];
self.serviceStationDict = nil;
[self.categoryDict removeAllObjects];
self.categoryDict = nil;
[self.courseOrderDict removeAllObjects];
self.courseOrderDict = nil;
[self.orderedItems removeAllObjects];
self.orderedItems = nil;
}
I'm not able to find out why my OrderSummaryItem is leaking despite cleaning it. My 'generateOrderSummary' function is called every 5 seconds to refresh the view.
if u declare your arrays getter and setter than after alloc it so u have to Release **twice** because its retain count becomes 2 that`s y try that...
Head First Iphone Development in this Books it`s explain well...
try adding autorelease pool here:
for(MenuItem *menuItem in self.selectedItems) {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
OrderSummaryItem *orderSummaryItem = [self createOrderSummaryItem:menuItem];
//Retain count = 1
[orderSummary addOrderedItem:orderSummaryItem withServiceStationCode:menuItem.serviceStationCode withCategory:menuItem.category withCourseOrder:menuItem.courseOrder];
//Retain count = 5;
[pool release];}
Maybe there's no leak, just main pool does not drain frequently enough

wait for a method to finish before starting another

assume that i have two sequential methods, methodA and methodB (methodB is called at the end of methodA) and i need to finish completely with the process of methodA before starting methodB :
-(void)methodA {
//do some very heavy computation here
[self methodB];
}
-(void)methodB {
//this method performs some animation
}
methodA requires alot of processing power and takes time to finish while methodB has animations. The animations are starting laggy at first and im guessing this has to do with the overhead of method A. So how can i make the second method start only after the first has finished completely?
this is the actual code:
the code is very messy and unorganized. there is alot of code that has been commented out so please ingore that. I test some things sometimes and then comment them out incase i need them later.
here is what is happening: it starts with (void)checkMoves method. This method calls either one of two similar methods which are
-(void)getMovesForJourney:(int)journey withRolls:(NSArray *)rolls or
-(void) getEntryMovesForJourney:(int)journey withRolls:(NSArray *)rolls
these methods are wrappers for a long recursive process (which is most probably where the multithreading is happening).
at the end of (void)checkMoves another method -(void)analyseMoves is called. This is also a heavy process.
and finally, at the end of -(void)analyseMoves the animation method -(void)move: is called. this last method is starting with a lag.
finally at the end of -(void)move: the process will start again until no more 'moves' are available.
the methods below are not in order so please refer to the description above to know whats going on.
-(void) checkMoves {
GameScene *game = [GameScene sharedGameScene];
CCLOG(#"start");
for (Stone *stone in stones) {
if (!stone.onBoard) {
if ([game EntryAvailable]) {
[self getEntryMovesForJourney:stone.journey withRolls:[game rollsAsIntegers]];
}
}
else {
[self getMovesForJourney:stone.journey withRolls:[game rollsAsIntegers]];
}
}
CCLOG(#"finish");
[self analyseMoves];
}
-(NSMutableArray *) otherPlayerLocations {
GameScene *game = [GameScene sharedGameScene];
Board *board = [game board];
Player *otherPlayer = [game playerOne];
NSMutableArray *locations = [[[NSMutableArray alloc] init] autorelease];
for (Stone *stone in otherPlayer.stones) {
if (stone.journey < 77) {
int location;
if (stone.onBoard)
location = [[board tileForATag:[self convertJourneyToTileTag:stone.journey]] bTag];
else
location = 0;
[locations addObject:[NSNumber numberWithInt:location]];
}
}
return locations;
}
-(void) analyseMoves {
if (moves.count > 0) {
NSMutableArray *killMoves = [[[NSMutableArray alloc] init] autorelease];
NSMutableArray *safeMoves = [[[NSMutableArray alloc] init] autorelease];
int mostThreatened;
float highestThreat = -1.0;
float lowestThreat = 100.0;
AIMove *safestMove;
//get kill and safe moves in seperate arrays
for (AIMove *move in moves) {
if (move.moveType == killMoveType)
[killMoves addObject:move];
else if (move.moveType == safeMoveType)
[safeMoves addObject:move];
// get highest threatened stone
//if (move.potThreat > highestThreat) {
// mostThreatened = move.journey;
// highestThreat = move.potThreat;
if (move.initThreat < lowestThreat) {
lowestThreat = move.initThreat;
safestMove = [move copy];
CCLOG(#"safest move assigned");
}
//}
}
//MOVE SELECTION:
//___________________________________
//choose best kill move
int killJourney;
if (killMoves.count > 0) {
//leave one move in killMoves with highest journey
killJourney = [[killMoves objectAtIndex:0] tileTag];
for (AIMove *killMove in killMoves) {
if (killMove.tileTag < killJourney)
[killMoves removeObject:killMove];
else
killJourney = killMove.tileTag;
}
}
//select correct move
if (killMoves.count > 0) {
[self move:[killMoves objectAtIndex:0]];
CCLOG(#"Kill move chosen");
}
else {
CCLOG(#"this is called!!!!");
CCLOG(#"safest move with initThreat: %i, potThreat: %i",safestMove.journey, safestMove.tileTag);
[self move:safestMove];
CCLOG(#"Success");
}
/*
else if (safeMoves.count > 0) {
[self move:[safeMoves objectAtIndex:0]];
CCLOG(#"safe move chosen");
}
else {
//temp random move generation
randomMove = CCRANDOM_0_1()*[moves count];
AIMove *move = [moves objectAtIndex:randomMove];
[self move:move];
CCLOG(#"Random move chosen");
}
*/
}
}
-(void) assignThreatLevel:(AIMove *)move {
NSArray *otherPlayerStones = [self otherPlayerLocations];
NSMutableArray *initThreats = [[NSMutableArray alloc] init];
NSMutableArray *potThreats = [[NSMutableArray alloc] initWithCapacity:4];
for (NSNumber *location in otherPlayerStones) {
//get threat levels for potential moves
if (move.tileTag > [location intValue]) {
int dif = move.tileTag - [location intValue];
CCLOG(#"dif: %i",dif);
//threat level conditions:
// 1 >> 5 = 70% of journey
// 6 >> 9 = 40% of journey
// 10 & 11 = 50% of journey
// 12 >> 24 = 20% of journey
// 25 && 26 = 50% of journey
// 27+ = 20% of journey
if (move.tileTag < 9) {
[initThreats addObject:[NSNumber numberWithFloat:0.0f]];
}
else if (dif >= 1 && dif <= 5) {
[initThreats addObject:[NSNumber numberWithFloat:k1to5]];
}
else if (dif >= 6 && dif <= 9) {
[initThreats addObject:[NSNumber numberWithFloat:k6to9]];
}
else if (dif == 10 || dif == 11) {
[initThreats addObject:[NSNumber numberWithFloat:k10to11]];
}
else if (dif >= 12 && dif <= 24) {
[initThreats addObject:[NSNumber numberWithFloat:k12to24]];
}
else if (dif == 25 || dif == 26) {
[initThreats addObject:[NSNumber numberWithFloat:k25to26]];
}
else if (dif > 26) {
[initThreats addObject:[NSNumber numberWithFloat:k27plus]];
}
//-------------------------------------
}
//get Threat levels for current positions
if (move.tileTag > [location intValue]) {
int dif = move.tileTag - [location intValue];
//threat level conditions:
// 1 >> 5 = 70% of journey
// 6 >> 9 = 40% of journey
// 10 & 11 = 50% of journey
// 12 >> 24 = 20% of journey
// 25 && 26 = 50% of journey
// 27+ = 20% of journey
if (move.journey < 8 || move.journey > 75)
[potThreats addObject:[NSNumber numberWithFloat:0.0f]];
else if (dif >= 1 && dif <= 5)
[potThreats addObject:[NSNumber numberWithFloat:k1to5]];
else if (dif >= 6 && dif <= 9)
[potThreats addObject:[NSNumber numberWithFloat:k6to9]];
else if (dif == 10 || dif == 11)
[potThreats addObject:[NSNumber numberWithFloat:k10to11]];
else if (dif >= 12 && dif <= 24)
[potThreats addObject:[NSNumber numberWithFloat:k12to24]];
else if (dif == 25 || dif == 26)
[potThreats addObject:[NSNumber numberWithFloat:k25to26]];
else if (dif > 26)
[potThreats addObject:[NSNumber numberWithFloat:k27plus]];
//-------------------------------------
}
}
float initThreat = 0.0f;
float potThreat = 0.0f;
for (NSNumber *number in initThreats) {
if ([number floatValue] > initThreat)
initThreat = [number floatValue];
}
for (NSNumber *number in potThreats) {
if ([number floatValue] > potThreat)
potThreat = [number floatValue];
}
move.initThreat = initThreat;
move.potThreat = potThreat;
[initThreats release];
}
-(void) move:(AIMove *)move {
CCLOG(#"Moves count: %i",[moves count]);
if ([moves count] > 0) {
BOOL isOtherStoneOnPreviousTile = NO;
int total;
if (move.tileTag > 8)
total = move.tileTag - move.journey;
else if (move.tileTag < 9 && move.journey > 8)
total = ((9-move.tileTag)+75) - move.journey;
else if (move.tileTag < 9)
total = move.tileTag - move.journey;
CCLOG(#"Total: %i",total);
GameScene *game = [GameScene sharedGameScene];
Board *board = [game board];
BoardTile *tile = [[game board] tileForBTag:move.tileTag];
CCSequence *sequence;
NSMutableArray *actions = [[[NSMutableArray alloc] init] autorelease];
id delay = [CCDelayTime actionWithDuration:0.5];
[actions addObject:delay];
if (move.journey > 0) {
for (int i = 1; i < total+1; i++) {
if (move.journey+i < 76)
tile = [board tileForBTag:move.journey+i];
else
tile = [board tileForBTag:9-((move.journey+i)-75)];
id moveAction = [CCMoveTo actionWithDuration:0.2 position:tile.position];
[actions addObject:moveAction];
}
}
else {
id moveAction = [CCMoveTo actionWithDuration:0.2 position:tile.position];
[actions addObject:moveAction];
}
// id moveFunc = [CCCallFunc actionWithTarget:self selector:#selector(moveMotion)];
//id moveAction = [CCMoveTo actionWithDuration:0.3 position:tile.position];
id killFunc = [CCCallFuncND actionWithTarget:self selector:#selector(killStonesForTile:data:) data:tile];
//id callfunc = [CCCallFunc actionWithTarget:self selector:#selector(move)];
[actions addObject:killFunc];
//[actions addObject:callfunc];
sequence = [CCSequence actionMutableArray:actions];
[actions removeAllObjects];
CGPoint exitPos = ccp(exitPosition.x-(completeStones*30),exitPosition.y-(completeStones*30));
id move2 = [CCMoveTo actionWithDuration:0.2f position:exitPos];
id sequence2 = [CCSequence actions:move2, nil];
Stone *stone = [self getStoneForJourney:move.journey];
//-
//------------------------------------------
//clean tracks
for (Stone *stone in stones) {
if (stone.journey == (move.tileTag - move.journey))
isOtherStoneOnPreviousTile = YES;
}
if (!isOtherStoneOnPreviousTile) {
BoardTile *prevTile = [board tileForBTag:[self convertJourneyToTileTag:move.journey]];
prevTile.occupiedBy = 0;
}
//===========================================
//-------------------------------------------
//set stone status
if (move.tileTag < 9 && stone.crossedEntry)
stone.journey = (9 - move.tileTag) + 75;
else
stone.journey = move.tileTag;
stone.onBoard = YES;
tile.occupiedBy = player2;
if (stone.journey > 8 && !stone.crossedEntry)
stone.crossedEntry = YES;
//============================================
if (stone.journey < 84)
[stone runAction:sequence];
else {
[stone runAction:sequence2];
completeStones++;
stone.isJourneyComplete = YES;
}
CCLOG(#"Stone Journey:%i",stone.journey);
NSArray *rollTypesArray = [move rollTypes];
[self removeRollTypes:rollTypesArray];
[moves removeAllObjects];
[game updateRollResults];
[self updateMoveAvailability];
}
else {
GameScene *game = [GameScene sharedGameScene];
[moves removeAllObjects];
[game nextTurn];
[game updateRollResults];
}
}
-(Stone *)getStoneForJourney:(int)journey {
Stone *theStone;
for (Stone *stone in stones) {
if (stone.journey == journey)
theStone = stone;
}
return theStone;
}
-(void)dealloc {
[moves release];
[rollTypes release];
[results release];
[super dealloc];
}
-(void)killStonesForTile:(id)sender data:(BoardTile *)tile {
GameScene *game = [GameScene sharedGameScene];
int tileTag;
Player *otherPlayer;
if (playerNumber == player1) {
tileTag = tile.aTag;
otherPlayer = [game playerTwo];
}
else {
tileTag = tile.bTag;
otherPlayer = [game playerOne];
}
CCArray *currentStones = [otherPlayer stones];
for (Stone *stone in currentStones) {
if (!stone.isJourneyComplete) {
int journey = stone.journey;
if (tileTag == tile.aTag) {
if (journey > 0 && [self convertJourneyToTileTag:journey] == tile.bTag) {
CCLOG(#"blue stone killed");
[self returnStoneToOrigin:stone];
}
}
if (tileTag == tile.bTag) {
if (journey > 0 && [self convertJourneyToTileTag:journey] == tile.aTag) {
CCLOG(#"gold stone killed");
[self returnStoneToOrigin:stone];
}
}
}
}
}
-(void)removeRollTypes:(NSArray *)theRollTypes {
GameScene *game = [GameScene sharedGameScene];
NSMutableArray *rolls = [game rolls];
for (NSNumber *roll in theRollTypes) {
NSUInteger index = [rolls indexOfObject:[game convertIntToRoll:roll]];
CCLOG(#"rolltypes count: %i",[theRollTypes count]);
CCLOG(#"roll integer: %i",[roll intValue]);
[rolls removeObjectAtIndex:index];
}
}
#pragma mark -
#pragma mark enumerations
- (NSArray*)getSums:(NSArray*)numbers {
NSMutableArray *result = [self getSumsHelper:numbers startingFrom:0];
[result removeObjectAtIndex:0];
return result;
}
- (NSMutableArray*)getSumsHelper:(NSArray*)numbers startingFrom:(NSUInteger)index {
/* (1) */
if (index >= [numbers count])
return [NSMutableArray arrayWithObject:[NSNumber numberWithFloat:0]];
/* (2) Generate all the subsets where the `index`th element is not included */
NSMutableArray* result = [self getSumsHelper:numbers startingFrom:index+1];
// NSSortDescriptor *mySorter = [[NSSortDescriptor alloc] initWithKey:#"floatValue" ascending:YES];
// [result sortUsingDescriptors:[NSArray arrayWithObject:mySorter]];
/* (3) Add all the cases where the `index`th element is included */
NSUInteger i, n = [result count];
float element = [[numbers objectAtIndex:index] floatValue];
for (i = 0; i < n; i++) {
float element2 = [[result objectAtIndex:i] floatValue];
float sum = element+element2;
BOOL numberPresent = NO;
for (NSNumber *number in result) {
if ([number floatValue] == sum)
numberPresent = YES;
}
if (!numberPresent)
[result addObject:[NSNumber numberWithFloat:sum]];
}
return result;
}
-(NSArray *) getCombsforNumbers:(NSArray *)numbers withTarget:(int)target{
NSMutableArray *partial = [[NSMutableArray alloc] init];
[partial addObject:[NSNumber numberWithInt:0]];
[results removeAllObjects];
NSArray *result = [self getCombsHelper:numbers target:target partial:partial];
NSUInteger minCount = [[result objectAtIndex:0] count];
NSUInteger index = 0;
NSMutableArray *combArray = [result objectAtIndex:0];
for (NSMutableArray *array in result) {
if ([array count] < minCount) {
minCount = [array count];
index = [result indexOfObject:array];
combArray = array;
}
}
//remove number 0 from array
[combArray removeObject:[NSNumber numberWithInt:0]];
return combArray;
}
-(NSMutableArray *) getCombsHelper:(NSArray *)numbers target:(int)target partial:(NSMutableArray *)partial {
int s = 0;
for (NSNumber *number in partial) {
s += [number intValue];
}
if (s == target) {
[results addObject:partial];
}
if (s >= target) {
return results;
}
for (int i = 0; i < [numbers count]; i++) {
NSMutableArray *remaining = [[[NSMutableArray alloc] init] autorelease];
int n = [[numbers objectAtIndex:i] intValue];
for (int j = i+1; j<[numbers count]; j++) {
[remaining addObject:[numbers objectAtIndex:j]];
}
NSMutableArray *partialRec = [[[NSMutableArray alloc] init] autorelease];
[partialRec addObjectsFromArray:partial];
[partialRec addObject:[NSNumber numberWithInt:n]];
[self getCombsHelper:remaining target:target partial:partialRec];
}
return results;
}
-(void) getMovesForJourney:(int)journey withRolls:(NSArray *)rolls {
GameScene *game = [GameScene sharedGameScene];
Board *board = [game board];
NSArray *sums = [self getSums:rolls];
for (NSNumber *number in sums) {
if ([number intValue]+journey <= 84) {
BoardTile *tile = [board tileForBTag:[self convertJourneyToTileTag:journey+[number intValue]]];
if (tile.isSafeTile) {
if (tile.occupiedBy != player1) {
AIMove *move = [AIMove moveWithJourney:journey tileTag:tile.bTag];
NSArray *theRollTypes = [[self getCombsforNumbers:rolls withTarget:[number intValue]] copy];
//Checking rolltypes, remove later
NSLog(#"%i = ",[number intValue]);
for (NSNumber *comb in theRollTypes) {
NSLog(#"%i",[comb intValue]);
}
NSLog(#"-----------");
//----------------------------------
move.moveType = safeMoveType;
move.initThreat = 0.0;
CCLOG(#"move initThreat: %f",move.initThreat);
[move setRollTypes:theRollTypes];
[moves addObject:move];
}
}
else {
AIMove *move = [AIMove moveWithJourney:journey tileTag:tile.bTag];
NSArray *theRollTypes = [[self getCombsforNumbers:rolls withTarget:[number intValue]] copy];
//Checking rolltypes, remove later
NSLog(#"%i = ",[number intValue]);
for (NSNumber *comb in theRollTypes) {
NSLog(#"%i",[comb intValue]);
}
NSLog(#"-----------");
//-----------------------------------
[move setRollTypes:theRollTypes];
//assing threat level
[self assignThreatLevel:move];
CCLOG(#"move initThreat: %f",move.initThreat);
//check for kill move
NSArray *otherPlayerPositions = (NSArray *)[self otherPlayerLocations];
for (NSNumber *location in otherPlayerPositions) {
if (move.tileTag == [location intValue])
move.moveType = killMoveType;
}
[moves addObject:move];
}
}
//int i = [number intValue];
//NSArray *combs = [self getCombsforNumbers:numbers withTarget:i];
}
}
-(void) getEntryMovesForJourney:(int)journey withRolls:(NSArray *)rolls {
GameScene *game = [GameScene sharedGameScene];
Board *board = [game board];
NSArray *sums = [self getSums:rolls];
for (NSNumber *number in sums) {
if ([number intValue]+journey <= 84) {
BoardTile *tile = [board tileForBTag:[self convertJourneyToTileTag:journey+[number intValue]]];
if (tile.isSafeTile) {
if (tile.occupiedBy != player1) {
NSArray *theRollTypes = [[self getCombsforNumbers:rolls withTarget:[number intValue]] copy];
BOOL containsEntry = NO;
for (NSNumber *rollType in theRollTypes) {
if ([rollType intValue] == 1) {
containsEntry = YES;
}
}
if (containsEntry) {
AIMove *move = [AIMove moveWithJourney:journey tileTag:tile.bTag];
//Checking rolltypes, remove later
NSLog(#"%i = ",[number intValue]);
for (NSNumber *comb in theRollTypes) {
NSLog(#"%i",[comb intValue]);
}
NSLog(#"-----------");
move.moveType = safeMoveType;
move.initThreat = 0.0;
CCLOG(#"move initThreat: %f",move.initThreat);
[move setRollTypes:theRollTypes];
[moves addObject:move];
//----------------------------------
}
}
}
else {
NSArray *theRollTypes = [[self getCombsforNumbers:rolls withTarget:[number intValue]] copy];
BOOL containsEntry = NO;
for (NSNumber *rollType in theRollTypes) {
if ([rollType intValue] == 1) {
containsEntry = YES;
}
}
if (containsEntry) {
AIMove *move = [AIMove moveWithJourney:journey tileTag:tile.bTag];
[move setRollTypes:theRollTypes];
//check for kill move
NSArray *otherPlayerPositions = (NSArray *)[self otherPlayerLocations];
for (NSNumber *location in otherPlayerPositions) {
if (move.tileTag == [location intValue])
move.moveType = killMoveType;
}
//assing threat level
[self assignThreatLevel:move];
[moves addObject:move];
//Checking rolltypes, remove later
NSLog(#"%i = ",[number intValue]);
for (NSNumber *comb in theRollTypes) {
NSLog(#"%i",[comb intValue]);
}
NSLog(#"-----------");
CCLOG(#"move initThreat: %f",move.initThreat);
[move setRollTypes:theRollTypes];
[moves addObject:move];
//----------------------------------
}
}
}
}
}
Technically, methodB is called after methodA is finished. In methodA you are apparently doing things that are backgrounded, meaning that the method finishes while some tasks are performed on another thread (or scheduled on the runloop). There's nothing we can say about this unless you do share the inner workings of methodA.