16.03.2017, 13:58
Ahoy friends.
For my pickup system im using 2 for loops to check arrays if they contain the pickupid of the pickup, the player picked up.
Due to efficiency reasons i want to use binary search instead because the array is sorted, so its the most efficient way.
But unfortutanetly my code doesnt work.
If i try it this way, only this part seems to be executed:
So the MoneyPickups work, but the ActorPickups doesnt.
If i change the order and do it this way
If i do it this way, the ActorPickups part is being executed, and they work well but MoneyPickups doesnt work anymore
It seems like the the second part is always being skipped for some reason, how can i fix this problem?
The for loop code works fine but i dont know why it doesnt work with binary search.
My binsearch function works fine and the return value should be the index of the found element inside of the array.
For my pickup system im using 2 for loops to check arrays if they contain the pickupid of the pickup, the player picked up.
Код:
public OnPlayerPickUpPickup(playerid, pickupid) { new index; switch(GetPickupType(pickupid,index)) { case INVALID_PICKUP_TYPE: { } case MONEY_TYPE: { maxmoney -= 1; GivePlayerMoney(playerid, 1000); printf("ID picked up: %d",maxmoney); MoneyPickups[index]=-1; HeapSort(MoneyPickups); DestroyPickup(pickupid); } case ACTOR_TYPE: { ShowMenuForPlayer(shopmenu,playerid); TogglePlayerControllable(playerid,false); } } return 1; } stock GetPickupType(pickupid, &index) { for(new i; i<sizeof(MoneyPickups); i++) { if(MoneyPickups[i] == pickupid) return index=i,MONEY_TYPE; } for(new i; i<sizeof(ActorPickups); i++) { if(ActorPickups[i] == pickupid) return index=i,ACTOR_TYPE; } return INVALID_PICKUP_TYPE; }
But unfortutanetly my code doesnt work.
Код:
stock GetPickupType(pickupid, &index) { new mres = binarysearch(MoneyPickups,pickupid,0,sizeof(MoneyPickups)-1); if(mres > -1 && MoneyPickups[mres] == pickupid) return index=mres,MONEY_TYPE; new ares = binarysearch(ActorPickups,pickupid,0,sizeof(ActorPickups)-1); if(ares > -1 && MoneyPickups[ares] == pickupid) return index=ares,ACTOR_TYPE; return INVALID_PICKUP_TYPE; }
Код:
new mres = binarysearch(MoneyPickups,pickupid,0,sizeof(MoneyPickups)-1); if(mres > -1 && MoneyPickups[mres] == pickupid) return index=mres,MONEY_TYPE;
If i change the order and do it this way
Код:
stock GetPickupType(pickupid, &index) { new ares = binarysearch(ActorPickups,pickupid,0,sizeof(ActorPickups)-1); if(ares > -1 && MoneyPickups[ares] == pickupid) return index=ares,ACTOR_TYPE; new mres = binarysearch(MoneyPickups,pickupid,0,sizeof(MoneyPickups)-1); if(mres > -1 && MoneyPickups[mres] == pickupid) return index=mres,MONEY_TYPE; return INVALID_PICKUP_TYPE; }
It seems like the the second part is always being skipped for some reason, how can i fix this problem?
The for loop code works fine but i dont know why it doesnt work with binary search.
My binsearch function works fine and the return value should be the index of the found element inside of the array.
Код:
stock binarysearch(a[],key,l,r) { new k; while(r >=l) { k = (l+r)/2; if(key == a[k]) { return k; } if(key < a[k]) { r = k-1; } else { l= k+1; } } return -1; }