Skip to content

Commit

Permalink
Change GlobalVar.EVENTS_PROBABILITIES to Dictionary, Reorder GlobalVa…
Browse files Browse the repository at this point in the history
…r.EVENTS_CLASSIFICATION to match GlobalEnums.EventType
  • Loading branch information
Lann-hyl committed Aug 2, 2024
1 parent 1207a86 commit a0041f9
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 14 deletions.
14 changes: 10 additions & 4 deletions Global/GLOBAL_VAR.gd
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down
16 changes: 10 additions & 6 deletions Managers/MapManager.gd
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down Expand Up @@ -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:
Expand All @@ -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
Expand Down
8 changes: 4 additions & 4 deletions Tests/test_eventbase.gd
Original file line number Diff line number Diff line change
Expand Up @@ -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)

0 comments on commit a0041f9

Please sign in to comment.