modifying the last GunChest example, Rarity added:
// Project: GunChest
// Created: 2022-07-24
// By: Virtual Nomad
// show all errors
SetErrorMode(2)
// set window properties
SetWindowTitle( "GunChest" )
SetWindowSize( 1280,720, 0 )
SetWindowAllowResize( 1 )
// set display properties
SetVirtualResolution( 1280,720)
SetOrientationAllowed( 1, 1, 1, 1 )
SetSyncRate( 30, 0 )
SetScissor( 0,0,0,0 )
UseNewDefaultFonts( 1 )
Type Gun
GunType$, Damage, RateOfFire#, Rarity, Active //Remmeber this order for Save/LoadGuns()
EndType
GLOBAL GunTypes as Gun []
MakeGuns() //Generate some guns to choose from
GLOBAL MaxGuns = 4 //Chest holds 5 Guns
GLOBAL GunChest as Gun []
GLOBAL CurrentGun
GLOBAL File$ = "GunChest.dat"
If GetFileExists(File$)
LoadGuns()
Else // Add Starter Pistol
ThisGun as Gun
ThisGun.GunType$ = GunTypes[0].GunType$
ThisGun.Damage = GunTypes[0].Damage
ThisGun.RateOfFire# = GunTypes[0].RateOfFire#
ThisGun.Rarity = GunTypes[0].Rarity
ThisGun.Active = 1
CurrentGun = 0
GunChest.Insert(ThisGun)
SaveGuns()
EndIf
do
If GetRawKeyState(27) then End //[ESC]
Print( "[Space]=Save|[F1]=Sort by Rarity|[F12]=DataFile" +CHR(10))
If GetRawKeyPressed(32) then SaveGuns() //[Space]
If GetRawKeyPressed(123) then OpenBrowser( GetWritePath()+"/media/"+File$ ) //[F12]
If GunChest.Length > 0
Print("Select Gun [1-" + STR(GunChest.Length+1) + "]")
CheckSwap( GetCharBuffer())
EndIf
If GunChest.Length > -1
Print( "RMB to Discard Current Gun" )
If GetRawMouseRightPressed() then DropGun()
EndIf
If GunChest.Length < MaxGuns
Print( "LMB to Add Gun (becomes Current)")
If GetPointerPressed() and GunChest.Length < MaxGuns then AddGun()
EndIf
If GetRawKeyPressed(112) then SortByRarity() //[F1]
ShowChest()
Print(GetRawLastKey())
Sync()
loop
Function SortByRarity()
Repeat
SortFlag = 0
For x = GunChest.Length to 1 Step -1
If GunChest[x].Rarity < GunChest[x-1].Rarity
GunChest.swap(x,x-1)
SortFlag = 1
Exit
EndIf
Next x
Until SortFlag = 0
EndFunction
Function DropGun()
If CurrentGun < GunChest.Length
GunChest[CurrentGun+1].Active = 1 //CurrentGun stays the same
GunChest.Remove(CurrentGun)
Else
If GunChest.Length > 0
GunChest[0].Active = 1
GunChest.Remove(CurrentGun)
CurrentGun = 0
Else
GunChest.Remove(CurrentGun)
CurrentGun = -1
EndIf
EndIf
EndFunction
Function SaveGuns()
ThisFile = OpenToWrite(File$)
If GunChest.Length = -1 //if the chest is empty...
WriteLine(ThisFile,"") //overwrite whatever might currently be in the datafile
Else //GunType$, Damage, RateOfFire#, Rarity, Active
For x = 0 to GunChest.Length
GunType$ = GunChest[x].GunType$
Dam$ = STR(GunChest[x].Damage)
RoF$ = STR(GunChest[x].RateOfFire#)
Rar$ = STR(GunChest[x].Rarity)
Active$ = STR(GunChest[x].Active)
WriteLine(ThisFile, GunType$ + "|" + Dam$ + "|" + Rof$ + "|" + Rar$ + "|" + Active$)
next x
EndIf
CloseFile(ThisFile)
EndFunction
Function LoadGuns()
ThisFile = OpenToRead(File$)
Repeat
ThisLine$ = ReadLine(ThisFile) //GunType$, Damage, RateOfFire#, Active
If ThisLine$ <> ""
ThisGun as Gun
ThisGun.GunType$ = GetStringToken(ThisLine$,"|",1)
ThisGun.Damage = VAL( GetStringToken(ThisLine$,"|",2) )
ThisGun.RateOfFire# = VALFloat( GetStringToken(ThisLine$,"|",3) )
ThisGun.Rarity = VAL( GetStringToken(ThisLine$,"|",4) )
ThisGun.Active = VAL( GetStringToken(ThisLine$,"|",5) )
GunChest.Insert(ThisGun)
If ThisGun.Active = 1 then CurrentGun = GunChest.Length
EndIf
Until FileEOF(ThisFile)
CloseFile(ThisFile)
EndFunction
Function CheckSwap(Gun$)
Gun = VAL(Gun$)-1
If Gun > -1 and Gun <= GunChest.Length and Gun <> CurrentGun
GunChest[CurrentGun].Active = 0
GunChest[Gun].Active = 1
CurrentGun = Gun
EndIf
EndFunction
Function AddGun()
GunPool as Integer []
for x = 0 to GunTypes.Length
Count = GunTypes[x].Rarity
For y = 1 to Count
GunPool.Insert(x)
next y
next x
NewGun = GunPool[Random(0,GunPool.Length)]
ThisGun as Gun
ThisGun.GunType$ = GunTypes[NewGun].GunType$
ThisGun.Damage = GunTypes[NewGun].Damage
ThisGun.RateOfFire# = GunTypes[NewGun].RateOfFire#
ThisGun.Rarity = GunTypes[NewGun].Rarity
DeselectCurrentGun()
ThisGun.Active = 1
GunChest.Insert(ThisGun)
CurrentGun = GunChest.Length
EndFunction
Function DeselectCurrentGun()
for x = 0 to GunChest.Length
If GunChest[x].Active = 1
GunChest[x].Active = 0
ExitFunction
EndIf
Next x
EndFunction
Function ShowChest()
Print("")
For x = 0 to GunChest.Length
ThisGun$ = STR(x+1) + "=" + GunChest[x].GunType$ + "|Dam:" + STR(GunChest[x].Damage) + "|RoF:" + STR(GunChest[x].RateOfFire#,2) + "|Rarity:" + STR(GunChest[x].Rarity)
If GunChest[x].Active = 1 then ThisGun$ = ThisGun$ + " <<"
Print(ThisGun$)
Next x
EndFunction
Function MakeGuns()
//Make 3 Pistols
For x = 1 to 3
ThisGun as Gun
ThisGun.GunType$ = "Pistol"
ThisGun.Damage = x
ThisGun.RateOfFire# = 4-x
ThisGun.Rarity = 10-x //(1-10) 10 = Most Common
GunTypes.Insert(ThisGun)
Next x
//Make 3 Rifles
For x = 1 to 3
ThisGun.GunType$ = "Rifle"
ThisGun.Damage = 3+x
ThisGun.RateOfFire# = 1+x
ThisGun.Rarity = 8-x
GunTypes.Insert(ThisGun)
Next x
//Make 3 Machine Guns
For x = 1 to 3
ThisGun.GunType$ = "Machine Gun"
ThisGun.Damage = x
ThisGun.RateOfFire# = x/3.0
ThisGun.Rarity = 6-x
GunTypes.Insert(ThisGun)
Next x
//Make 3 Shotguns
For x = 1 to 3
ThisGun.GunType$ = "Shotgun"
ThisGun.Damage = x*3
ThisGun.RateOfFire# = 2+x
ThisGun.Rarity = 4-x
GunTypes.Insert(ThisGun)
Next x
EndFunction
the pertinent parts:
Function AddGun()
GunPool as Integer []
for x = 0 to GunTypes.Length
Count = GunTypes[x].Rarity
For y = 1 to Count
GunPool.Insert(x)
next y
next x
...
...add instances of the gun type into a growing pool of choices to pick from with more instances of more-common guns increasing odds of more-common guns generated (weighting)
then, sorting the GunChest (10=Most-Common, 1=Most-Rare):
Function SortByRarity()
Repeat
SortFlag = 0
For x = GunChest.Length to 1 Step -1
If GunChest[x].Rarity < GunChest[x-1].Rarity
GunChest.swap(x,x-1)
SortFlag = 1
Exit
EndIf
Next x
Until SortFlag = 0
EndFunction
not thoroughly tested but it's all i have time for ATM and seems to work as intended:
NOTE: that's not the best bubble sort i expect. someone could probably make it more efficient (can i simply remove the Exit?).
i'll re-evaluate when i get a chance unless someone beats me to it