Skip to content

Commit

Permalink
Add gradient and adjust click timings
Browse files Browse the repository at this point in the history
  • Loading branch information
Bergam0t committed May 19, 2024
1 parent 454f21a commit 225b619
Show file tree
Hide file tree
Showing 6 changed files with 146 additions and 85 deletions.
2 changes: 2 additions & 0 deletions Globals.gd
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ var colors = Gradient.new()
var colors_array = [Color(0, 0, .9, 0.3), Color(.5, 0, 0, 0.6), Color(.9, 0, 0, 0.8)]
var color_step = 1.0 / (len(colors_array) - 1)

var initial_world_prob_array = {}

var final_world_prob_array = {}

var play_speed = 3
Expand Down
189 changes: 105 additions & 84 deletions Player/Player.gd
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ extends Node2D
var allow_click = false
var tile = Vector2.ZERO

var outcome_var = false

var rand_float = 0.0

Expand All @@ -13,7 +14,11 @@ onready var world_array = world.world_prob_array
onready var text_log = $"../LabelControl/Log"

onready var ai_wait_length = Globals.get_wait(Globals.play_speed)
onready var ai_move_cooldown_timer = $AiMoveCooldownTimer

onready var click_cooldown_timer = $ClickCooldownTimer

var ai_can_move = true

onready var sprite = $RobotSprite

Expand Down Expand Up @@ -62,13 +67,95 @@ func run_move_animation():

func _ready():
sprite.visible = false

print(world_array)
var youfound_popup = found_popup_scene.instance()
add_child(youfound_popup)

print("Wait length: " + str(ai_wait_length))
ai_move_cooldown_timer.wait_time = ai_wait_length
#text_log.add_color_override("font_color_selected", Color(0,0,0,0))
#var current_seed = Globals.random_seed_selected
#rng.seed = hash(str(current_seed))
# Update this after

if Globals.play_mode == "ai_simple" or Globals.play_mode == "ai_advanced":
Globals.can_click = false

# Select a random first tile
#if Globals.turns == 0:
tile = str(get_random_tile())
previous_tile = tile
first_turn_msg()
update_after_turn()
digging_outcome()
if Globals.play_mode == "ai_simple":
update_probabilities()
else:
update_probabilities_with_lr()
#get_tree().paused = true
ai_can_move = false
ai_move_cooldown_timer.start()
#yield(get_tree().create_timer(ai_wait_length), "timeout")
#get_tree().paused = false

var exploit_or_explore = 0.0

while Globals.turns <= Globals.max_turns:
ai_move_cooldown_timer.start()
yield(ai_move_cooldown_timer, "timeout")
# Use specified logic to decide what move to make
# Do move
# increment turn
# return to start of while loop
# Begins by random sample between 0 and 1
# if lower than exploitation rate, will find the highest estimated location
# simple solution for ties - just take the first in the list
# if sample is higher than explitation rate, will choose a random
# location and go there
# add a timer while the AI 'thinks', then next turn
exploit_or_explore = randf()

if exploit_or_explore < Globals.agent_exploitation_rate:
# Exploit
# Find highest rate and go there
text_log.text = "Turn " + str(Globals.turns) + ": Exploit!\n" + text_log.text

var current_highest_cell = Vector2.ZERO
var current_highest_prob = 0.0
var cell_prob_observed = 0.0
for cell in world_array.keys():
cell_prob_observed = world_array.get(str(cell))['Prob_Observed']
if cell_prob_observed > current_highest_prob:
current_highest_prob = cell_prob_observed
current_highest_cell = cell

tile = str(current_highest_cell)

else:
# Explore
# Choose random tile and go there
text_log.text = "Turn " + str(Globals.turns) + ": Explore!\n" + text_log.text
tile = str(get_random_tile())

if Globals.turns > 0 and previous_tile == tile:
dug_again_same_tile_msg()
elif Globals.turns == (Globals.max_turns-1) and previous_tile != tile:
penultimate_turn_invalid_selection_msg()
else:
move_tile_and_dig_msg()

update_after_turn()
digging_outcome()
if Globals.play_mode == "ai_simple":
update_probabilities()
else:
update_probabilities_with_lr()
#get_tree().paused = true
#yield(get_tree().create_timer(ai_wait_length), "timeout")
#get_tree().paused = false

on_final_turn()

func _process(delta):
#t += delta * 0.4
Expand Down Expand Up @@ -100,7 +187,6 @@ func penultimate_turn_invalid_selection_msg():
tile = previous_tile
Globals.turns += 1


func move_tile_and_dig_msg():
text_log.text = "Turn " + str(Globals.turns) + ": Moved to new tile " + str(formatted_tile_label) + "\n" + text_log.text
Globals.turns += 1
Expand Down Expand Up @@ -133,7 +219,7 @@ func update_probabilities():
)
emit_signal("probabilities_updated", tile)

func update_probabilities_with_lr(learning_rate):
func update_probabilities_with_lr():
var prev_estimate = world_array.get(str(tile))['Prob_Estimate']

world_array.get(str(tile))['Prob_Estimate'] = ((Globals.agent_learning_rate *
Expand All @@ -145,7 +231,6 @@ func update_probabilities_with_lr(learning_rate):

emit_signal("probabilities_updated", tile)


func on_invalid_dig_location_clicked():
print("Can't click there, mate")
text_log.text = "Invalid digging location selected - select a tile on the island" + "\n" + text_log.text
Expand All @@ -167,15 +252,17 @@ func digging_outcome(popup_label="none", popup="none", popup_timer="none"):

if rand_float < world_array.get(str(tile))['Prob']:
on_treasure_found(popup_label, popup, popup_timer)
return true
else:
on_treasure_not_found()
return false

func get_random_tile():
var tile_x = randi() % Globals.GridSizeX
var tile_y = randi() % Globals.GridSizeY
return Vector2(tile_x, tile_y)

# Turn-taking logic
# Turn-taking logic for manual play
func _input(event):
if Globals.play_mode == "manual":
if (event.is_pressed() and event.button_index == BUTTON_LEFT):
Expand All @@ -199,7 +286,6 @@ func _input(event):
move_tile_and_dig_msg()

Globals.can_click = false
$ClickCooldownTimer.start()

update_after_turn()

Expand All @@ -210,7 +296,15 @@ func _input(event):
# Is it worth seeding with the turn number for reproducibility?
# Or seed * turn number for some variation across seeds, at least?

digging_outcome(popup_label, popup, popup_timer)
outcome_var = digging_outcome(popup_label, popup, popup_timer)

if previous_tile == tile and outcome_var:
click_cooldown_timer.wait_time = popup_timer.wait_time
elif previous_tile == tile:
click_cooldown_timer.wait_time = 0.1
else:
click_cooldown_timer.wait_time = 1.5
click_cooldown_timer.start()

update_probabilities()

Expand All @@ -220,86 +314,13 @@ func _input(event):

if Globals.turns == Globals.max_turns:
on_final_turn()
# Update this after
elif Globals.play_mode == "ai_simple" or Globals.play_mode == "ai_advanced":


# Select a random first tile
tile = str(get_random_tile())
previous_tile = tile
first_turn_msg()
update_after_turn()
digging_outcome()
if Globals.play_mode == "ai_simple":
update_probabilities()
else:
update_probabilities_with_lr(Globals.agent_learning_rate)
#get_tree().paused = true
yield(get_tree().create_timer(ai_wait_length), "timeout")
#get_tree().paused = false



var exploit_or_explore = 0.0

while Globals.turns <= Globals.max_turns:
# Use specified logic to decide what move to make
# Do move
# increment turn
# return to start of while loop
# Begins by random sample between 0 and 1
# if lower than exploitation rate, will find the highest estimated location
# simple solution for ties - just take the first in the list
# if sample is higher than explitation rate, will choose a random
# location and go there
# add a timer while the AI 'thinks', then next turn
exploit_or_explore = randf()

if exploit_or_explore < Globals.agent_exploitation_rate:
# Exploit
# Find highest rate and go there
text_log.text = "Turn " + str(Globals.turns) + ": Exploit!\n" + text_log.text

var current_highest_cell = Vector2.ZERO
var current_highest_prob = 0.0
var cell_prob_observed = 0.0
for cell in world_array.keys():
cell_prob_observed = world_array.get(str(cell))['Prob_Observed']
if cell_prob_observed > current_highest_prob:
current_highest_prob = cell_prob_observed
current_highest_cell = cell

tile = str(current_highest_cell)

else:
# Explore
# Choose random tile and go there
text_log.text = "Turn " + str(Globals.turns) + ": Explore!\n" + text_log.text
tile = str(get_random_tile())

if Globals.turns > 0 and previous_tile == tile:
dug_again_same_tile_msg()
elif Globals.turns == (Globals.max_turns-1) and previous_tile != tile:
penultimate_turn_invalid_selection_msg()
else:
move_tile_and_dig_msg()

update_after_turn()
digging_outcome()
if Globals.play_mode == "ai_simple":
update_probabilities()
else:
update_probabilities_with_lr(Globals.agent_learning_rate)
#get_tree().paused = true
yield(get_tree().create_timer(ai_wait_length), "timeout")
#get_tree().paused = false

on_final_turn()







func _on_ClickCooldownTimer_timeout():
Globals.can_click = true


func _on_AiMoveCooldownTimer_timeout():
ai_can_move = true
4 changes: 4 additions & 0 deletions Player/Player.tscn
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,12 @@ frames = SubResource( 13 )
animation = "Idle"
centered = false

[node name="AiMoveCooldownTimer" type="Timer" parent="."]
one_shot = true

[node name="ClickCooldownTimer" type="Timer" parent="."]
wait_time = 1.5
one_shot = true

[connection signal="timeout" from="AiMoveCooldownTimer" to="." method="_on_AiMoveCooldownTimer_timeout"]
[connection signal="timeout" from="ClickCooldownTimer" to="." method="_on_ClickCooldownTimer_timeout"]
1 change: 0 additions & 1 deletion Popups/YouFoundPopup.tscn
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ size = 24
font_data = SubResource( 1 )

[node name="Popup" type="Popup"]
visible = true
margin_top = 1.0
margin_right = 447.0
margin_bottom = 322.0
Expand Down
20 changes: 20 additions & 0 deletions World.gd
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,19 @@ extends Node2D
onready var selectTilemap = $SelectTilemap
var world_prob_array = {}

onready var gradientScaleRect = $ScaleGradientRect

# https://www.reddit.com/r/godot/comments/telp85/how_to_create_a_gradient_via_script/
#var gradient_data := {
# 0.0: Color.red,
# 0.5: Color.green,
# 0.75: Color.violet,
# 1.0: Color.blue,
#}




# https://www.reddit.com/r/godot/comments/b6wn9j/creating_a_gradient_instance_in_a_script/
# Set grid colors based on observed probability
var colors = Globals.colors
Expand All @@ -26,6 +39,7 @@ func _ready():
#print(world_prob_array)

emit_signal("world_prob_array_created", world_prob_array)
Globals.initial_world_prob_array = world_prob_array

var idx = 0.0
var step = Globals.color_step
Expand All @@ -34,6 +48,12 @@ func _ready():
colors.add_point(idx, color)
idx = min(idx + step, .999)
# setting a color at point 1.0 failed to add it correctly to the end of the gradient

var gradient_texture = GradientTexture.new()
gradient_texture.gradient = colors
gradient_texture.width = 200

gradientScaleRect.texture = gradient_texture

var margin_left = 0
var margin_top = 0
Expand Down
15 changes: 15 additions & 0 deletions World.tscn
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,7 @@ margin_left = 71.0
margin_top = 38.0
margin_right = 221.0
margin_bottom = 67.0
rect_pivot_offset = Vector2( 43, 9 )
theme = ExtResource( 16 )
custom_fonts/font = SubResource( 8 )
text = "ABCDE"
Expand Down Expand Up @@ -219,6 +220,20 @@ margin_bottom = 310.0
[node name="ConfirmReturnHomeDialog" parent="ReturnToHomeButton" index="0"]
visible = false

[node name="ScaleGradientRect" type="TextureRect" parent="."]
margin_left = 273.0
margin_top = 283.0
margin_right = 411.0
margin_bottom = 298.0
expand = true

[node name="Label" type="Label" parent="."]
margin_left = 270.0
margin_top = 299.0
margin_right = 446.0
margin_bottom = 313.0
text = "0 0.2 0.4 0.6 0.8 1"

[connection signal="world_prob_array_created" from="." to="Player" method="_on_World_world_prob_array_created"]
[connection signal="current_tile_signal" from="SelectTilemap" to="LabelControl" method="_on_SelectTilemap_current_tile_signal"]
[connection signal="current_tile_signal" from="SelectTilemap" to="Player" method="_on_SelectTilemap_current_tile_signal"]
Expand Down

0 comments on commit 225b619

Please sign in to comment.