diff --git a/Global/GLOBAL_VAR.gd b/Global/GLOBAL_VAR.gd index a1312836..d98993d2 100644 --- a/Global/GLOBAL_VAR.gd +++ b/Global/GLOBAL_VAR.gd @@ -13,11 +13,17 @@ var MODIFIER_KEYS: Dictionary = { # Keys can be accessed with the syntax: GlobalVar.MODIFIER_KEYS.ADD_PERMANENT # this is useful if we need to change the reference value in multiple places -## A list of all the possible events -var EVENTS_CLASSIFICATION: Array[Resource] = [EventMob, EventRandom, EventShop, EventHeal, EventDialogue] +## A list of all the possible events, ordered the same as GlobalEnums.EventType +var EVENTS_CLASSIFICATION: Array[Resource] = [EventRandom, EventHeal, EventMob, EventShop, EventDialogue] -## A list of the probabilities of all possible events -var EVENTS_PROBABILITIES: Array[int] = [45, 16, 5, 12, 22] +## A list of the probabilities of all possible events, ordered the same as GlobalEnums.EventType +var EVENTS_PROBABILITIES: Dictionary = { + GlobalEnums.EventType.Random:16, + GlobalEnums.EventType.Heal: 12, + GlobalEnums.EventType.Mob: 45, + GlobalEnums.EventType.Shop: 5, + GlobalEnums.EventType.Dialogue: 22 +} ## A list of all the possible movements on the map var POSSIBLE_MOVEMENTS: Dictionary = { diff --git a/Managers/MapManager.gd b/Managers/MapManager.gd index a13f941f..c2820347 100644 --- a/Managers/MapManager.gd +++ b/Managers/MapManager.gd @@ -143,12 +143,14 @@ func is_room_event_correct(current_room: RoomBase, map: MapBase = current_map) - ## Draws room type randomly, with each type having a set probability [br] ## Algorithm taken from this: https://stackoverflow.com/a/1761646 func draw_room_type() -> EventBase: - var sum: int = 0 - for proba in GlobalVar.EVENTS_PROBABILITIES: - sum += proba - var _rand_type_number: int = randi_range(0, sum-1) + var _total_proba: int = 0 + for event: GlobalEnums.EventType in GlobalVar.EVENTS_PROBABILITIES: + # print(event) + _total_proba += GlobalVar.EVENTS_PROBABILITIES[event] - for _rand_type_index: int in range(GlobalVar.EVENTS_CLASSIFICATION.size()): + var _rand_type_number: int = randi_range(0, _total_proba-1) + + for _rand_type_index: GlobalEnums.EventType in GlobalVar.EVENTS_PROBABILITIES: if _rand_type_number < GlobalVar.EVENTS_PROBABILITIES[_rand_type_index]: return GlobalVar.EVENTS_CLASSIFICATION[_rand_type_index].new() @@ -194,6 +196,8 @@ func _ready() -> void: if DebugVar.DEBUG_PRINT_EVENT_COUNT: var events: Dictionary = {} var total_nb_rooms: int = 0 + var expected_probabilities: Dictionary = {"mob": 45, "random": 16, "heal": 12, "dialogue": 22, "shop": 5} + for index_height: int in range(current_map.rooms.size()): for index_width: int in range(current_map.rooms[index_height].size()): if current_map.rooms[index_height][index_width] == null: @@ -205,7 +209,7 @@ func _ready() -> void: events[event] += 1 total_nb_rooms += 1 for k:String in events: - print("Event " + k + " has "+ str(events[k]) + " rooms. (" + str(float(events[k])*100/total_nb_rooms).pad_decimals(2) + "%)") + print("Event " + k + " has "+ str(events[k]) + " rooms. (is " + str(float(events[k])*100/total_nb_rooms).pad_decimals(2) + "%, expected: " + str(expected_probabilities[k]) + "%)") print("Total number of rooms generated: " + str(total_nb_rooms)) ## checks if the map exists diff --git a/Tests/test_eventbase.gd b/Tests/test_eventbase.gd index 0827acd7..418fa513 100644 --- a/Tests/test_eventbase.gd +++ b/Tests/test_eventbase.gd @@ -11,14 +11,14 @@ func after_each() -> void: func test_event_base_array_all_types() -> void: var event0: Resource = GlobalVar.EVENTS_CLASSIFICATION[0] - assert_eq(event0, EventMob) + assert_eq(event0, EventRandom) var event1: Resource = GlobalVar.EVENTS_CLASSIFICATION[1] - assert_eq(event1, EventRandom) + assert_eq(event1, EventHeal) var event2: Resource = GlobalVar.EVENTS_CLASSIFICATION[2] - assert_eq(event2, EventShop) + assert_eq(event2, EventMob) var event3: Resource = GlobalVar.EVENTS_CLASSIFICATION[3] - assert_eq(event3, EventHeal) + assert_eq(event3, EventShop)