Browse Source

bot improvements

Lurkars 5 years ago
parent
commit
3567fffe88
  1. 4
      project.godot
  2. 1
      scenes/menus/SettingsMenu.tscn
  3. 2
      scenes/road/roadCornerSmall.tscn
  4. 2
      scenes/road/roadCornerSmallFlipped.tscn
  5. 2
      scenes/road/roadRamp.tscn
  6. 4
      scenes/road/roadRampLong.tscn
  7. 2
      scenes/road/roadRampLongCurved.tscn
  8. 2
      scenes/road/roadRampLongFlipped.tscn
  9. 31
      scripts/game/gamestate.gd
  10. 13
      scripts/game/route.gd
  11. 55
      scripts/player/bot_controls.gd
  12. 14
      scripts/road/road.gd

4
project.godot

@ -120,7 +120,7 @@ controls_break={
controls_add_road={ controls_add_road={
"deadzone": 0.5, "deadzone": 0.5,
"events": [ Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":0,"alt":false,"shift":false,"control":false,"meta":false,"command":false,"pressed":false,"scancode":32,"unicode":0,"echo":false,"script":null) "events": [ Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":0,"alt":false,"shift":false,"control":false,"meta":false,"command":false,"pressed":false,"scancode":32,"unicode":0,"echo":false,"script":null)
, Object(InputEventJoypadButton,"resource_local_to_scene":false,"resource_name":"","device":0,"button_index":5,"pressure":0.0,"pressed":false,"script":null)
, Object(InputEventJoypadButton,"resource_local_to_scene":false,"resource_name":"","device":0,"button_index":4,"pressure":0.0,"pressed":false,"script":null)
] ]
} }
controls_next_road_type={ controls_next_road_type={
@ -150,7 +150,7 @@ controls_prev_road_variant={
controls_reset={ controls_reset={
"deadzone": 0.5, "deadzone": 0.5,
"events": [ Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":0,"alt":false,"shift":false,"control":false,"meta":false,"command":false,"pressed":false,"scancode":69,"unicode":0,"echo":false,"script":null) "events": [ Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":0,"alt":false,"shift":false,"control":false,"meta":false,"command":false,"pressed":false,"scancode":69,"unicode":0,"echo":false,"script":null)
, Object(InputEventJoypadButton,"resource_local_to_scene":false,"resource_name":"","device":0,"button_index":4,"pressure":0.0,"pressed":false,"script":null)
, Object(InputEventJoypadButton,"resource_local_to_scene":false,"resource_name":"","device":0,"button_index":3,"pressure":0.0,"pressed":false,"script":null)
] ]
} }
debug_camera_1={ debug_camera_1={

1
scenes/menus/SettingsMenu.tscn

@ -122,6 +122,7 @@ margin_bottom = 27.0
margin_bottom = 27.0 margin_bottom = 27.0
[node name="action_button" parent="menu/tabs/controls/settings_controls/key_dialog" index="3"] [node name="action_button" parent="menu/tabs/controls/settings_controls/key_dialog" index="3"]
margin_right = 302.0
margin_bottom = 110.0 margin_bottom = 110.0
[node name="graphics" type="Tabs" parent="menu/tabs"] [node name="graphics" type="Tabs" parent="menu/tabs"]

2
scenes/road/roadCornerSmall.tscn

@ -11,7 +11,7 @@ material/1 = null
material/2 = null material/2 = null
script = ExtResource( 2 ) script = ExtResource( 2 )
end_rotation = Vector3( 0, -1.571, 0 ) end_rotation = Vector3( 0, -1.571, 0 )
speed_constrains = PoolVector3Array( 0.1, 0.79, 0.08 )
speed_constrains = PoolVector3Array( 0.1, 0.6, 0.08 )
force_penalties = PoolVector3Array( 2, 0, 8 ) force_penalties = PoolVector3Array( 2, 0, 8 )
torque_penalties = PoolVector3Array( 0.1, 0.1, -0.5 ) torque_penalties = PoolVector3Array( 0.1, 0.1, -0.5 )

2
scenes/road/roadCornerSmallFlipped.tscn

@ -11,7 +11,7 @@ material/1 = null
material/2 = null material/2 = null
script = ExtResource( 2 ) script = ExtResource( 2 )
end_rotation = Vector3( 0, 1.571, 0 ) end_rotation = Vector3( 0, 1.571, 0 )
speed_constrains = PoolVector3Array( 0.1, 0.79, 0.08 )
speed_constrains = PoolVector3Array( 0.1, 0.6, 0.08 )
force_penalties = PoolVector3Array( 2, 0, 8 ) force_penalties = PoolVector3Array( 2, 0, 8 )
torque_penalties = PoolVector3Array( 0.1, 0.1, 0.5 ) torque_penalties = PoolVector3Array( 0.1, 0.1, 0.5 )

2
scenes/road/roadRamp.tscn

@ -11,7 +11,7 @@ material/1 = null
material/2 = null material/2 = null
script = ExtResource( 2 ) script = ExtResource( 2 )
reset_index = 1 reset_index = 1
speed_constrains = PoolVector3Array( 0, 1, -0.05 )
speed_constrains = PoolVector3Array( 0.5, 0.9, -0.025 )
path_penalties = [ 1.0 ] path_penalties = [ 1.0 ]
[node name="Path" type="Path" parent="."] [node name="Path" type="Path" parent="."]

4
scenes/road/roadRampLong.tscn

@ -11,8 +11,10 @@ material/1 = null
material/2 = null material/2 = null
script = ExtResource( 2 ) script = ExtResource( 2 )
reset_index = 1 reset_index = 1
speed_constrains = PoolVector3Array( 0, 2, -0.05 )
speed_constrains = PoolVector3Array( 0.7, 1.9, -0.03 )
path_penalties = [ 2.0 ] path_penalties = [ 2.0 ]
[node name="Path" type="Path" parent="."] [node name="Path" type="Path" parent="."]
curve = ExtResource( 3 ) curve = ExtResource( 3 )
[node name="PathFollow" type="PathFollow" parent="Path"]

2
scenes/road/roadRampLongCurved.tscn

@ -11,7 +11,7 @@ material/1 = null
material/2 = null material/2 = null
script = ExtResource( 2 ) script = ExtResource( 2 )
reset_index = 1 reset_index = 1
speed_constrains = PoolVector3Array( 0.25, 1.5, -0.05 )
speed_constrains = PoolVector3Array( 0.7, 1.5, -0.03 )
path_penalties = [ 1.25 ] path_penalties = [ 1.25 ]
[node name="Path" type="Path" parent="."] [node name="Path" type="Path" parent="."]

2
scenes/road/roadRampLongFlipped.tscn

@ -10,7 +10,7 @@ material/0 = null
material/1 = null material/1 = null
material/2 = null material/2 = null
script = ExtResource( 2 ) script = ExtResource( 2 )
speed_constrains = PoolVector3Array( 0, 0.3, 0.12 )
speed_constrains = PoolVector3Array( 0.1, 0.3, 0.12 )
force_penalties = PoolVector3Array( 0, 0, 4 ) force_penalties = PoolVector3Array( 0, 0, 4 )
[node name="Path" type="Path" parent="."] [node name="Path" type="Path" parent="."]

31
scripts/game/gamestate.gd

@ -24,11 +24,11 @@ signal game_ended()
remote func set_player(id, new_name:String = " ", new_color:Color = Color.black, new_ready:bool = false): remote func set_player(id, new_name:String = " ", new_color:Color = Color.black, new_ready:bool = false):
players[id] = {"name" : new_name, "color" : new_color, "ready" : new_ready, "prepared" : false} players[id] = {"name" : new_name, "color" : new_color, "ready" : new_ready, "prepared" : false}
emit_signal("players_changed", players) emit_signal("players_changed", players)
if get_tree().is_network_server(): if get_tree().is_network_server():
var all_ready = true var all_ready = true
for p_id in players:
rpc_id(id, "set_player", p_id, players[p_id].name, players[p_id].color, players[p_id].ready)
for p_id in players:
rpc_id(id, "set_player", p_id, players[p_id].name, players[p_id].color, players[p_id].ready)
rpc_id(p_id, "set_player", id, new_name, new_color, new_ready) rpc_id(p_id, "set_player", id, new_name, new_color, new_ready)
all_ready = all_ready && players[p_id].ready all_ready = all_ready && players[p_id].ready
rpc("game_ready", all_ready) rpc("game_ready", all_ready)
@ -43,12 +43,12 @@ func update_player(new_name:String = " ", new_color:Color = Color.black, new_rea
remotesync func remove_player(id): remotesync func remove_player(id):
players.erase(id) players.erase(id)
if game != null: if game != null:
var game_players = game.get_node("players") var game_players = game.get_node("players")
if game_players.has_node(str(id)): if game_players.has_node(str(id)):
game_players.remove_child(game_players.get_node(str(id))) game_players.remove_child(game_players.get_node(str(id)))
emit_signal("players_changed", players) emit_signal("players_changed", players)
@ -63,34 +63,35 @@ remotesync func game_ready(all_ready:bool):
remotesync func prepare_game(): remotesync func prepare_game():
get_tree().set_pause(true) get_tree().set_pause(true)
game = Game.instance() game = Game.instance()
get_tree().get_root().add_child(game) get_tree().get_root().add_child(game)
get_tree().get_root().get_node("main_menu").hide() get_tree().get_root().get_node("main_menu").hide()
for p_id in players: for p_id in players:
var controls var controls
var player = players[p_id] var player = players[p_id]
var player_scene = Player.instance() var player_scene = Player.instance()
player_scene.set_network_master(p_id) player_scene.set_network_master(p_id)
player_scene.set_name(str(p_id)) player_scene.set_name(str(p_id))
game.get_node("players").add_child(player_scene) game.get_node("players").add_child(player_scene)
player_scene.set_player_name(player.name) player_scene.set_player_name(player.name)
player_scene.set_player_color(player.color) player_scene.set_player_color(player.color)
if p_id == get_tree().get_network_unique_id(): if p_id == get_tree().get_network_unique_id():
controls = HumanPlayer.instance() controls = HumanPlayer.instance()
controls.set_name("HumanControl_" + str(p_id)) controls.set_name("HumanControl_" + str(p_id))
game.add_child(controls) game.add_child(controls)
controls.set_player(player_scene.get_path()) controls.set_player(player_scene.get_path())
controls.set_network_master(p_id) controls.set_network_master(p_id)
player_scene.set_start(player_scene.get_index()) player_scene.set_start(player_scene.get_index())
if bots: if bots:
for index in range(MAX_PEERS-players.size()):
# for index in range(MAX_PEERS-players.size()): # DEBUG
for index in range(1):
var bot_index = players.size() + index var bot_index = players.size() + index
var player_scene = Player.instance() var player_scene = Player.instance()
player_scene.set_name("bot_player" + str(bot_index)) player_scene.set_name("bot_player" + str(bot_index))
@ -119,7 +120,7 @@ remotesync func player_prepared(id):
if get_tree().is_network_server(): if get_tree().is_network_server():
players[id].prepared = true players[id].prepared = true
var all_prepared = true var all_prepared = true
for p_id in players:
for p_id in players:
all_prepared = all_prepared && players[p_id].prepared all_prepared = all_prepared && players[p_id].prepared
if all_prepared: if all_prepared:
rpc("post_start_game") rpc("post_start_game")
@ -135,7 +136,7 @@ remotesync func set_bots(has_bots:bool):
func quit_game(): func quit_game():
if game != null:
if game != null:
game.queue_free() game.queue_free()
game = null game = null
@ -148,5 +149,5 @@ func quit_game():
game_running = false game_running = false
bots = false bots = false
game_name = "" game_name = ""
emit_signal("game_ended") emit_signal("game_ended")

13
scripts/game/route.gd

@ -14,7 +14,10 @@ func get_last_road():
func get_road(road_index): func get_road(road_index):
return get_child(road_index)
if get_child_count() > road_index:
return get_child(road_index)
else:
return null
remotesync func add_road(type, creator): remotesync func add_road(type, creator):
@ -26,19 +29,19 @@ remotesync func add_road(type, creator):
transform = transform.translated(curve.get_point_position(curve.get_point_count() - 1)) transform = transform.translated(curve.get_point_position(curve.get_point_count() - 1))
if last_road.get_end_rotation().length() != 0: if last_road.get_end_rotation().length() != 0:
transform.basis = transform.basis.rotated(last_road.get_end_rotation().normalized(), last_road.get_end_rotation().length()) transform.basis = transform.basis.rotated(last_road.get_end_rotation().normalized(), last_road.get_end_rotation().length())
var new_road = roads_factory.get_road_instance(type) var new_road = roads_factory.get_road_instance(type)
new_road.set_global_transform(transform) new_road.set_global_transform(transform)
new_road.set_preview(false) new_road.set_preview(false)
new_road.set_creator(creator) new_road.set_creator(creator)
if creator > 0: if creator > 0:
var player = gamestate.game.get_node(str(creator)) var player = gamestate.game.get_node(str(creator))
if player != null: if player != null:
new_road.set_color(player.get_player_color()) new_road.set_color(player.get_player_color())
add_child(new_road) add_child(new_road)
emit_signal("road_added", new_road) emit_signal("road_added", new_road)

55
scripts/player/bot_controls.gd

@ -2,9 +2,13 @@ extends Spatial
var player:Player var player:Player
const MAX_ROAD_INDEX = 1
var error_rate:float = 0.00
var underspeed_rate:float = 0.00
const MAX_OFFSET = 8.0
const OFFSET_STEPS = 0.01
var break_constrain = 0.0
export var error_rate:float = 0.00
export var underspeed_rate:float = 0.00
func _ready(): func _ready():
@ -18,27 +22,36 @@ func _physics_process(delta):
player.reset() player.reset()
else: else:
if player.get_road() != null: if player.get_road() != null:
var rate = randf()
var road = player.get_road() var road = player.get_road()
var road_index = 0
var speed_constrain = 0.0
while road != null && road_index < MAX_ROAD_INDEX:
for index in range(road.speed_constrains.size()):
var constrain = road.speed_constrains[index]
if constrain.z > 0:
speed_constrain = max(speed_constrain, constrain.z)
elif constrain.z < 0:
speed_constrain = min(speed_constrain, constrain.z)
road = player.route.get_road(road.get_index() + 1)
road_index += 1
var rate = randf()
var speed = player.current_speed var speed = player.current_speed
if speed_constrain < 0:
player.thrust = 1
elif speed_constrain > 0 && speed > speed_constrain:
var offset = 0
var distance = player.follow.get_offset()
var tmp_break_constrain = 0.0
break_constrain = 0.0
while tmp_break_constrain == 0.0 and offset < MAX_OFFSET && road != null:
var constrain_index = road.get_constrain_index(distance)
var road_length = road.get_lane_curve(player.lane).get_baked_length()
if constrain_index >= 0:
tmp_break_constrain = road.speed_constrains[constrain_index].z
if tmp_break_constrain > break_constrain:
break_constrain = tmp_break_constrain
distance += OFFSET_STEPS
offset += OFFSET_STEPS
if distance >= road_length:
distance = 0
road = gamestate.game.route.get_road(road.get_index()+1)
if break_constrain > 0 && speed > break_constrain - 0.05:
player.thrust = -1 player.thrust = -1
elif rate > underspeed_rate:
player.thrust = 1
elif speed - 0.1 < break_constrain:
player.thrust = 1
elif break_constrain == 0:
player.thrust = 1
else: else:
player.thrust = 0 player.thrust = 0

14
scripts/road/road.gd

@ -85,12 +85,20 @@ func get_next_lane(lane):
return lane return lane
func penalty_index(distance:float, speed:float):
func get_constrain_index(distance:float):
for index in range(speed_constrains.size()): for index in range(speed_constrains.size()):
var constrain = speed_constrains[index] var constrain = speed_constrains[index]
if constrain.x <= distance && constrain.y >= distance: if constrain.x <= distance && constrain.y >= distance:
if constrain.z < 0 && speed < constrain.z * -1 || constrain.z > 0 && speed > constrain.z:
return index
return index
return -1
func penalty_index(distance:float, speed:float):
var constrain_index = get_constrain_index(distance)
if constrain_index >= 0:
var constrain = speed_constrains[constrain_index]
if constrain.z < 0 && speed < constrain.z * -1 || constrain.z > 0 && speed > constrain.z:
return constrain_index
return -1 return -1

Loading…
Cancel
Save