Skip to content

Commit

Permalink
initial
Browse files Browse the repository at this point in the history
  • Loading branch information
Doubleumc committed Jul 18, 2024
1 parent 6138e36 commit 72a7b1e
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 27 deletions.
18 changes: 12 additions & 6 deletions code/__HELPERS/_lists.dm
Original file line number Diff line number Diff line change
Expand Up @@ -128,17 +128,23 @@
* You should only pass integers in.
*/
/proc/pick_weight(list/list_to_pick)
if(length(list_to_pick) == 0)
return null

var/total = 0
var/item
for(item in list_to_pick)
for(var/item in list_to_pick)
if(!list_to_pick[item])
list_to_pick[item] = 0
total += list_to_pick[item]

total = rand(0, total)
for(item in list_to_pick)
total -= list_to_pick[item]
if(total <= 0 && list_to_pick[item])
total = rand(1, total)
for(var/item in list_to_pick)
var/item_weight = list_to_pick[item]
if(item_weight == 0)
continue

total -= item_weight
if(total <= 0)
return item

return null
Expand Down
16 changes: 0 additions & 16 deletions code/__HELPERS/lists.dm
Original file line number Diff line number Diff line change
Expand Up @@ -87,22 +87,6 @@
result = first ^ second
return result

//Pretends to pick an element based on its weight but really just seems to pick a random element.
/proc/pickweight(list/L)
var/total = 0
var/item
for (item in L)
if (!L[item])
L[item] = 1
total += L[item]

total = rand(1, total)
for (item in L)
total -=L [item]
if (total <= 0)
return item
return null

/// Pick a random element from the list and remove it from the list.
/proc/pick_n_take(list/L)
RETURN_TYPE(L[_].type)
Expand Down
4 changes: 2 additions & 2 deletions code/game/machinery/computer/arcade.dm
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@
src.temp = "[src.enemy_name] has fallen! Rejoice!"

if(!length(contents))
var/prizeselect = pickweight(prizes)
var/prizeselect = pick_weight(prizes)
new prizeselect(src.loc)

if(istype(prizeselect, /obj/item/toy/gun)) //Ammo comes with the gun
Expand Down Expand Up @@ -176,5 +176,5 @@
if(2)
num_of_prizes = rand(0,2)
for(num_of_prizes; num_of_prizes > 0; num_of_prizes--)
empprize = pickweight(prizes)
empprize = pick_weight(prizes)
new empprize(src.loc)
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
. = ..()

#ifndef UNIT_TESTS
switch (pickweight(list("small" = 55, "aid" = 25, "tank" = 10, "both" = 10, "nothing" = 0, "delete" = 0)))
switch (pick_weight(list("small" = 55, "aid" = 25, "tank" = 10, "both" = 10, "nothing" = 1, "delete" = 1)))
#else
var/test = "both"
switch (test) // We don't want randomness in tests
Expand Down
2 changes: 1 addition & 1 deletion code/game/supplyshuttle.dm
Original file line number Diff line number Diff line change
Expand Up @@ -532,7 +532,7 @@ GLOBAL_DATUM_INIT(supply_controller, /datum/controller/supply, new())
for(var/datum/supply_packs_asrs/crate in cratelist)
var/weight = (floor(10000/crate.cost))
weighted_crate_list[crate] = weight
return pickweight(weighted_crate_list)
return pick_weight(weighted_crate_list)

//To stop things being sent to centcomm which should not be sent to centcomm. Recursively checks for these types.
/datum/controller/supply/proc/forbidden_atoms_check(atom/A)
Expand Down
2 changes: 1 addition & 1 deletion code/modules/nightmare/nmnodes/flow.dm
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@
remaining = length(pickables) // Force all to be picked for testing (this could potentially make false positives though)
#endif
while(length(pickables) && remaining > 0)
var/datum/nmnode/node = pickweight(pickables)
var/datum/nmnode/node = pick_weight(pickables)
remaining--
pickables -= node
picked += node
Expand Down

0 comments on commit 72a7b1e

Please sign in to comment.