Vielen Dank!
Ich habe mir das mal angeschaut, bin aber nicht so ganz ueberzeugt von der performance weil irgendwie bei dem quickSort Algorithmus von Slice der Worst Case nicht ausgeschlossen wurde .
Habe deshalb versucht bei mir das Pivot Element aus dem Mittelwert von 2 Werten zu waehlen, um zu gewaehrleisten dass ich immer O(n log n) habe
Habe das ganze nun gebaut bekommen, jedoch noch 1 Problem:
Bekomme irgendwie nicht selten als WeaponInfo[pickupid][wModel] Nullen als Werte, eventuell durch meinen fehlerhaften Umgang mit random?
Weiterhin sind die WeaponPickups da, funktionieren auch, jedoch bekomme ich z.B. wenn ich eine Katana einsammle, eine M4
Wo muss ich da nach dem Problem suchen? Vielleicht beim Sortieren?
Bitte um Rat
OnPlayerPickUpPickUp und dazugehoerige Funktion
public OnPlayerPickUpDynamicPickup(playerid, pickupid)
{
new index;
switch(GetPickupType(pickupid,index))
{
case INVALID_PICKUP_TYPE:
{
}
case MONEY_TYPE:
{
PickupCounter[MONEY_TYPE] -= 1;
GivePlayerMoney(playerid, MoneyVal);
MoneyPickups[index]=-1;
quickSort(MoneyPickups,0,sizeof(MoneyPickups)-1);
DestroyDynamicPickup(pickupid);
}
case ACTOR_TYPE:
{
ShowMenuForPlayer(shopmenu,playerid);
TogglePlayerControllable(playerid,false);
}
case INFO_TYPE:
{
ShowMenuForPlayer(infomenu,playerid);
TogglePlayerControllable(playerid,false);
}
case WEAPONTYPE_M4..WEAPONTYPE_FIRE_EXTINGUSHER:
{
printf("RECEIVED WEAPON");
printf("WEAPON: %d",WeaponInfo[pickupid][wModel]);
printf("WEAPON: Pickupid: %d WeaponID %d Ammo: %d",pickupid,WeaponInfo[pickupid][wModel],WeaponInfo[pickupid][wAmmo]);
PickupCounter[GENERIC_WEAPON] -= 1;
new weaponname;
switch(WeaponInfo[pickupid][wModel])//Gibt nur die ModelID aus
{
case 356:
weaponname = WEAPON_M4;
case 355:
weaponname = WEAPON_AK47;
case 358:
weaponname = WEAPON_SNIPER;
case 357:
weaponname = WEAPON_RIFLE;
case 348:
weaponname = WEAPON_DEAGLE;
case 346:
weaponname = WEAPON_COLT45;
case 347:
weaponname = WEAPON_SILENCED;
case 351:
weaponname = WEAPON_SHOTGSPA;
case 349:
weaponname = WEAPON_SHOTGUN;
case 350:
weaponname = WEAPON_SAWEDOFF;
case 352:
weaponname = WEAPON_UZI;
case 372:
weaponname = WEAPON_TEC9;
case 353:
weaponname = WEAPON_MP5;
case 342:
weaponname = WEAPON_GRENADE;
case 344:
weaponname = WEAPON_MOLTOV;
case 359:
weaponname = WEAPON_ROCKETLAUNCHER;
case 337:
weaponname = WEAPON_SHOVEL;
case 341:
weaponname = WEAPON_CHAINSAW;
case 339:
weaponname = WEAPON_KATANA;
case 336:
weaponname = WEAPON_BAT;
case 325:
weaponname = WEAPON_FLOWER;
case 326:
weaponname = WEAPON_CANE;
case 371:
weaponname = WEAPON_PARACHUTE;
case 366:
weaponname = WEAPON_FIREEXTINGUISHER;
}
GivePlayerWeapon(playerid, weaponname, WeaponInfo[pickupid][wAmmo]);
WeaponPickups[index] = -1;
HeapSort(WeaponPickups);
quickSort2(WeaponInfo,0,0,sizeof(WeaponInfo)-1);
DestroyDynamicPickup(pickupid);
}
}
return 1;
}
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 && ActorPickups[ares] == pickupid) return index=ares,ACTOR_TYPE;
new ires = binarysearch(InfoPickups,pickupid,0,sizeof(InfoPickups)-1);
if(ires > -1 && InfoPickups[ires] == pickupid) return index=ires,INFO_TYPE;
new dres = binarysearch(WeaponPickups,pickupid,0,sizeof(WeaponPickups)-1);
if(dres > -1 && WeaponPickups[dres] == pickupid)
{
switch(WeaponInfo[pickupid][wModel])
{
case 356:
return index=dres,WEAPONTYPE_M4;
case 355:
return index=dres,WEAPONTYPE_AK47;
case 358:
return index=dres,WEAPONTYPE_SNIPERRIFLE;
case 357:
return index=dres,WEAPONTYPE_CUNTGUN;
case 348:
return index=dres,WEAPONTYPE_DESERT_EAGLE;
case 346:
return index=dres,WEAPONTYPE_PISTOL_9MM;
case 347:
return index=dres,WEAPONTYPE_SILENCED_9MM;
case 351:
return index=dres,WEAPONTYPE_SPAS12;
case 349:
return index=dres,WEAPONTYPE_SHOTGUN;
case 350:
return index=dres,WEAPONTYPE_SAWN_OFF;
case 352:
return index=dres,WEAPONTYPE_MICRO_UZI;
case 372:
return index=dres,WEAPONTYPE_TEC9;
case 353:
return index=dres,WEAPONTYPE_MP5;
case 342:
return index=dres,WEAPONTYPE_GRENADE;
case 344:
return index=dres,WEAPONTYPE_MOLOTOW;
case 359:
return index=dres,WEAPONTYPE_RPG;
case 337:
return index=dres,WEAPONTYPE_SHOVEL;
case 341:
return index=dres,WEAPONTYPE_CHAINSAW;
case 339:
return index=dres,WEAPONTYPE_KATANA;
case 336:
return index=dres,WEAPONTYPE_BASEBALL;
case 325:
return index=dres,WEAPONTYPE_FLOWER;
case 326:
return index=dres,WEAPONTYPE_CANE;
case 371:
return index=dres,WEAPONTYPE_PARACHUTE;
case 366:
return index=dres,WEAPONTYPE_FIRE_EXTINGUSHER;
case 0:
return INVALID_PICKUP_TYPE;
}
}
return INVALID_PICKUP_TYPE;
}
Alles anzeigen
stock quickSort2(array[][],idx, left, right)
{
new
tempLeft = left,
tempRight = right,
pivot = array[(left + right) / 2][idx],
tempVar
;
while(tempLeft <= tempRight)
{
while(array[tempLeft][idx] < pivot) tempLeft++;
while(array[tempRight][idx] > pivot) tempRight--;
if(tempLeft <= tempRight)
{
tempVar = array[tempLeft][idx], array[tempLeft][idx] = array[tempRight][idx], array[tempRight][idx] = tempVar;
tempLeft++, tempRight--;
}
}
if(left < tempRight) quickSort(array[idx], left, tempRight);
if(tempLeft < right) quickSort(array[idx], tempLeft, right);
}
Alles anzeigen
Jede 20ms wird eins generiert
public MONEY_MAIN()//MAIN
{
{
new Float:Z;
new Float:rx1=((frandom((floatabs(MAP_WORLDBOUNDS[0][0]-MAP_WORLDBOUNDS[0][1])))+MAP_WORLDBOUNDS[0][1]));
new Float:ry2=((frandom((floatabs(MAP_WORLDBOUNDS[0][2]-MAP_WORLDBOUNDS[0][3])))+MAP_WORLDBOUNDS[0][3]));
MapAndreas_FindZ_For2DCoord(rx1,ry2,Z);
GenerateRandomPickup(1212,19,rx1,rx1+1,ry2,ry2+1,Z,Z+0.5,0);
}
{
new Float:Z;
new Float:rx1=((frandom((floatabs(MAP_WORLDBOUNDS[0][0]-MAP_WORLDBOUNDS[0][1])))+MAP_WORLDBOUNDS[0][1]));
new Float:ry2=((frandom((floatabs(MAP_WORLDBOUNDS[0][2]-MAP_WORLDBOUNDS[0][3])))+MAP_WORLDBOUNDS[0][3]));
MapAndreas_FindZ_For2DCoord(rx1,ry2,Z);
new randWeap = random(VAL_PICKUPTYPES-INFO_TYPE)+INFO_TYPE+1;
GenerateRandomWeaponPickup(randWeap,19,rx1,rx1+1,ry2,ry2+1,Z,Z+0.5,0);
}
}
Alles anzeigen
Die Funktion dazu
stock GenerateRandomWeaponPickup(weapon,type,Float:x_max,Float:x_min,Float:y_max,Float:y_min,Float:z_max,Float:z_min,virtualworld)
{
if(PickupCounter[GENERIC_WEAPON] <= sizeof(WeaponPickups))
{
new Float:rx1=((frandom((floatabs(x_max-x_min)))+x_min));
new Float:ry2=((frandom((floatabs(y_max-y_min)))+y_min));
new Float:rz3=((frandom((floatabs(z_max-z_min)))+z_min));
new modelid;
switch(weapon)
{
case WEAPONTYPE_M4:
modelid = 356;
case WEAPONTYPE_AK47:
modelid = 355;
case WEAPONTYPE_SNIPERRIFLE:
modelid = 358;
case WEAPONTYPE_CUNTGUN:
modelid = 357;
case WEAPONTYPE_DESERT_EAGLE:
modelid = 348;
case WEAPONTYPE_PISTOL_9MM:
modelid = 346;
case WEAPONTYPE_SILENCED_9MM:
modelid = 347;
case WEAPONTYPE_SPAS12:
modelid = 351;
case WEAPONTYPE_SHOTGUN:
modelid = 349;
case WEAPONTYPE_SAWN_OFF:
modelid = 350;
case WEAPONTYPE_MICRO_UZI:
modelid = 352;
case WEAPONTYPE_TEC9:
modelid = 372;
case WEAPONTYPE_MP5:
modelid = 353;
case WEAPONTYPE_GRENADE:
modelid = 342;
case WEAPONTYPE_MOLOTOW:
modelid = 344;
case WEAPONTYPE_RPG:
modelid = 359;
case WEAPONTYPE_SHOVEL:
modelid = 337;
case WEAPONTYPE_CHAINSAW:
modelid = 341;
case WEAPONTYPE_KATANA:
modelid = 339;
case WEAPONTYPE_BASEBALL:
modelid = 336;
case WEAPONTYPE_FLOWER:
modelid = 325;
case WEAPONTYPE_CANE:
modelid = 326;
case WEAPONTYPE_PARACHUTE:
modelid = 371;
case WEAPONTYPE_FIRE_EXTINGUSHER:
modelid = 366;
}
PickupCounter[GENERIC_WEAPON] = PickupCounter[GENERIC_WEAPON] +1;
WeaponPickups[PickupCounter[GENERIC_WEAPON]] = CreateDynamicPickup(modelid,type,rx1,ry2,rz3,virtualworld);
WeaponInfo[PickupCounter[GENERIC_WEAPON]][wModel] = modelid;
WeaponInfo[PickupCounter[GENERIC_WEAPON]][wAmmo] = random(1000)+10;
HeapSort(WeaponPickups);
quickSort2(WeaponInfo,0,0,sizeof(WeaponInfo)-1);
}
}
Alles anzeigen
Die dazugehoerigen enums
enum
{
INVALID_PICKUP_TYPE,
MONEY_TYPE,
ACTOR_TYPE,
INFO_TYPE,
GENERIC_WEAPON,
WEAPONTYPE_M4,
WEAPONTYPE_AK47,
WEAPONTYPE_SNIPERRIFLE,
WEAPONTYPE_CUNTGUN,
WEAPONTYPE_DESERT_EAGLE,
WEAPONTYPE_PISTOL_9MM,
WEAPONTYPE_SILENCED_9MM,
WEAPONTYPE_SPAS12,
WEAPONTYPE_SHOTGUN,
WEAPONTYPE_SAWN_OFF,
WEAPONTYPE_MICRO_UZI,
WEAPONTYPE_TEC9,
WEAPONTYPE_MP5,
WEAPONTYPE_GRENADE,
WEAPONTYPE_MOLOTOW,
WEAPONTYPE_RPG,
WEAPONTYPE_SHOVEL,
WEAPONTYPE_CHAINSAW,
WEAPONTYPE_KATANA,
WEAPONTYPE_BASEBALL,
WEAPONTYPE_FLOWER,
WEAPONTYPE_CANE,
WEAPONTYPE_PARACHUTE,
WEAPONTYPE_FIRE_EXTINGUSHER,
VAL_PICKUPTYPES
};
enum
{
wModel,
wAmmo,
VAL_INFO
};
Alles anzeigen
Die Arrays
new ActorPickups[NUM_ACTORS];
#if !defined NUM_WEAPONS
#error NUM_WEAPONS must be defined!!!
#endif
new WeaponPickups[NUM_WEAPONS];
new WeaponInfo[NUM_WEAPONS][VAL_INFO];
new InfoPickups[sizeof(PlayerInfoPickups)];
new MoneyPickups[MAX_PICKUPS-sizeof(ActorPickups)-sizeof(InfoPickups)-NUM_WEAPONS-1];
Die Zuweisung der Munition geht auch bisher gut.
Jedoch ist halt die Modelid des erstellen Pickups, nicht die Modelid des Pickups bei GetPickUpType, als wuerde da irgendwas durcheinander kommen
Habe daher ueberlegt ob ich hier
Unter OnPlayerPickUpPickup
switch(WeaponInfo[pickupid][wModel])
{
statt der pickupid den Index nehmen muss den ich aus der Binaersuche rauskriege