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; }
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;
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; }
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; }
if(ares > -1 && MoneyPickups[ares] == pickupid) return index=ares,ACTOR_TYPE;
Shouldn't this
Код:
if(ares > -1 && MoneyPickups[ares] == pickupid) return index=ares,ACTOR_TYPE; |
Already saw your last topic but I am still wondering why do you do it in this roundabout way instead of indexing the array with the pickupid
The only advantage I see is that the access to all ActorPickups and MoneyPickups is faster but I think this is rarely needed / or not needed at all in my opinion |
I want to add other pickup types and a pickup streamer so i guess it could be useful in the feature.
|
new gPickupType[MAX_PICKUPS];
// GenerateRandomPickup
pickupid = CreatePickup(modelid,type,rx1,ry2,rz3,virtualworld);
// some security checks to check that pickupid is valid
gPickupType[pickupid] = MONEY_TYPE;
// OnPlayerPickUpPickup
switch(gPickupType[pickupid])