Skip to content

Commit 3f2ec97

Browse files
committed
use /datum/rng for storyteller stuff
1 parent 6fd3bd1 commit 3f2ec97

File tree

4 files changed

+32
-11
lines changed

4 files changed

+32
-11
lines changed

code/__HELPERS/aneri/rand/instanced.dm

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,26 @@
4242
CRASH("Attempted to do a pick from a non-list")
4343
return ANERI_CALL(instanced_pick)(src, choices)
4444

45+
/datum/rng/proc/pick_n_take(list/choices)
46+
RETURN_TYPE(choices[_].type)
47+
var/len = length(choices)
48+
if(!len)
49+
return
50+
var/picked = rand(1, len)
51+
. = choices[picked]
52+
choices.Cut(picked, picked + 1) //Cut is far more efficient that Remove()
53+
4554
/datum/rng/proc/pick_weighted(list/choices)
4655
RETURN_TYPE(choices[_].type)
4756
if(!islist(choices))
4857
CRASH("Attempted to do a weighted pick from a non-list")
4958
return ANERI_CALL(instanced_pick_weighted)(src, choices)
5059

60+
/datum/rng/proc/pick_weighted_n_take(list/choices)
61+
RETURN_TYPE(choices[_].type)
62+
. = pick_weighted(choices)
63+
choices -= .
64+
5165
/datum/rng/proc/chance(percent) // "prob" is a reserved word, so we do "chance" instead
5266
return ANERI_CALL(instanced_prob)(src, percent)
5367

monkestation/code/modules/storytellers/converted_events/_base_event.dm

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,7 @@
264264

265265
while(length(weighted_candidates) && length(candidates) < antag_count) //both of these pick_n_take from weighted_candidates so this should be fine
266266
if(prompted_picking)
267-
var/picked_ckey = pick_n_take_weighted(weighted_candidates)
267+
var/picked_ckey = SSgamemode.rng.pick_weighted_n_take(weighted_candidates)
268268
var/client/picked_client = GLOB.directory[picked_ckey]
269269
if(QDELETED(picked_client))
270270
continue
@@ -283,7 +283,7 @@
283283
show_candidate_amount = FALSE,
284284
)
285285
else
286-
var/picked_ckey = pick_n_take_weighted(weighted_candidates)
286+
var/picked_ckey = SSgamemode.rng.pick_weighted_n_take(weighted_candidates)
287287
var/client/picked_client = GLOB.directory[picked_ckey]
288288
if(QDELETED(picked_client))
289289
continue
@@ -298,7 +298,7 @@
298298
message_admins("A roleset event got fewer antags then its antag_count and may not function correctly.")
299299
break
300300

301-
var/mob/candidate = pick_n_take(candidates)
301+
var/mob/candidate = SSgamemode.rng.pick_weighted_n_take(candidates)
302302
log_storyteller("Antag event spawned mob: [candidate], special role: [candidate.mind?.special_role ? candidate.mind.special_role : "none"]")
303303

304304
candidate.client?.prefs.reset_antag_rep()

monkestation/code/modules/storytellers/gamemode_subsystem.dm

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -162,11 +162,14 @@ SUBSYSTEM_DEF(gamemode)
162162
/// Are we able to run roundstart events
163163
var/can_run_roundstart = TRUE
164164
var/list/triggered_round_events = list()
165+
var/datum/rng/rng
165166

166167
/datum/controller/subsystem/gamemode/Initialize(time, zlevel)
167168
#if defined(UNIT_TESTS) || defined(AUTOWIKI) // lazy way of doing this but idc
168169
CONFIG_SET(flag/disable_storyteller, TRUE)
169170
#endif
171+
rng = new
172+
170173
// Populate event pools
171174
for(var/track in event_tracks)
172175
event_pools[track] = list()
@@ -201,12 +204,16 @@ SUBSYSTEM_DEF(gamemode)
201204
return SS_INIT_NO_NEED
202205
return SS_INIT_SUCCESS
203206

207+
/datum/controller/subsystem/gamemode/Shutdown()
208+
. = ..()
209+
QDEL_NULL(rng)
210+
204211
/datum/controller/subsystem/gamemode/fire(resumed = FALSE)
205212
if(SSticker.round_start_time && (world.time - SSticker.round_start_time) >= ROUNDSTART_VALID_TIMEFRAME)
206213
can_run_roundstart = FALSE
207214
else if(current_roundstart_event && length(current_roundstart_event.preferred_events)) //note that this implementation is made for preferred_events being other roundstart events
208215
var/list/preferred_copy = current_roundstart_event.preferred_events.Copy()
209-
var/datum/round_event_control/selected_event = pick_weight(preferred_copy)
216+
var/datum/round_event_control/selected_event = rng.pick_weighted(preferred_copy)
210217
var/player_count = get_active_player_count(alive_check = TRUE, afk_check = TRUE, human_check = TRUE)
211218
if(ispath(selected_event)) //get the instances if we dont have them
212219
current_roundstart_event.preferred_events = list()
@@ -221,7 +228,7 @@ SUBSYSTEM_DEF(gamemode)
221228
var/sanity = 0
222229
while(!selected_event && length(preferred_copy) && sanity < 100)
223230
sanity++
224-
selected_event = pick_weight(preferred_copy)
231+
selected_event = rng.pick_weighted(preferred_copy)
225232
if(!selected_event.can_spawn_event(player_count))
226233
preferred_copy -= selected_event
227234
selected_event = null
@@ -392,7 +399,7 @@ SUBSYSTEM_DEF(gamemode)
392399
var/calc_value = base_amt + (gain_amt * ready_players)
393400
calc_value *= roundstart_point_multipliers[track]
394401
calc_value *= current_storyteller.starting_point_multipliers[track]
395-
calc_value *= (rand(100 - current_storyteller.roundstart_points_variance,100 + current_storyteller.roundstart_points_variance)/100)
402+
calc_value *= (rng.ranged_uint(100 - current_storyteller.roundstart_points_variance, 100 + current_storyteller.roundstart_points_variance) / 100)
396403
event_track_points[track] = round(calc_value)
397404

398405
/// If the storyteller guarantees an antagonist roll, add points to make it so.
@@ -689,7 +696,7 @@ ADMIN_VERB(forceGamemode, R_FUN, FALSE, "Open Gamemode Panel", "Opens the gamemo
689696
var/list/possible = subtypesof(/datum/station_goal)
690697
var/goal_weights = 0
691698
while(possible.len && goal_weights < 1) // station goal budget is 1
692-
var/datum/station_goal/picked = pick_n_take(possible)
699+
var/datum/station_goal/picked = rng.pick_n_take(possible)
693700
goal_weights += initial(picked.weight)
694701
GLOB.station_goals += new picked
695702

@@ -773,7 +780,7 @@ ADMIN_VERB(forceGamemode, R_FUN, FALSE, "Open Gamemode Panel", "Opens the gamemo
773780
return
774781
if(length(GLOB.clients) > MAX_POP_FOR_STORYTELLER_VOTE)
775782
secret_storyteller = TRUE
776-
selected_storyteller = pick_weight(get_valid_storytellers(TRUE))
783+
selected_storyteller = rng.pick_weighted(get_valid_storytellers(TRUE))
777784
return
778785
SSvote.initiate_vote(/datum/vote/storyteller, "pick round storyteller", forced = TRUE)
779786

@@ -789,7 +796,7 @@ ADMIN_VERB(forceGamemode, R_FUN, FALSE, "Open Gamemode Panel", "Opens the gamemo
789796
var/added_storytellers = 0
790797
while(added_storytellers < DEFAULT_STORYTELLER_VOTE_OPTIONS && length(pick_from))
791798
added_storytellers++
792-
var/picked_storyteller = pick_weight(pick_from)
799+
var/picked_storyteller = rng.pick_weighted(pick_from)
793800
final_choices[picked_storyteller] = 0
794801
pick_from -= picked_storyteller
795802
return final_choices

monkestation/code/modules/storytellers/storytellers/_storyteller.dm

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@
135135
message_admins("Storyteller failed to pick an event for track of [track].")
136136
mode.event_track_points[track] *= TRACK_FAIL_POINT_PENALTY_MULTIPLIER
137137
return
138-
picked_event = pick_weight(valid_events)
138+
picked_event = SSgamemode.rng.pick_weighted(valid_events)
139139
if(!picked_event)
140140
if(length(valid_events))
141141
var/added_string = ""
@@ -176,7 +176,7 @@
176176
// Perhaps use some bell curve instead of a flat variance?
177177
var/total_cost = bought_event.cost * mode.point_thresholds[track]
178178
if(!bought_event.roundstart)
179-
total_cost *= (1 + (rand(-cost_variance, cost_variance)/100)) //Apply cost variance if not roundstart event
179+
total_cost *= (1 + (SSgamemode.rng.ranged_int(-cost_variance, cost_variance) / 100)) //Apply cost variance if not roundstart event
180180
mode.event_track_points[track] = max(mode.event_track_points[track] - total_cost, 0)
181181
message_admins("Storyteller purchased and triggered [bought_event] event, on [track] track, for [total_cost] cost.")
182182
if(bought_event.roundstart)

0 commit comments

Comments
 (0)