From 3567fffe88fc58b78e2d3c45f61ef9fe2eaabb2f Mon Sep 17 00:00:00 2001 From: Lurkars Date: Fri, 26 Jul 2019 19:23:14 +0200 Subject: [PATCH] bot improvements --- project.godot | 4 +- scenes/menus/SettingsMenu.tscn | 1 + scenes/road/roadCornerSmall.tscn | 2 +- scenes/road/roadCornerSmallFlipped.tscn | 2 +- scenes/road/roadRamp.tscn | 2 +- scenes/road/roadRampLong.tscn | 4 +- scenes/road/roadRampLongCurved.tscn | 2 +- scenes/road/roadRampLongFlipped.tscn | 2 +- scripts/game/gamestate.gd | 31 +++++++------- scripts/game/route.gd | 13 +++--- scripts/player/bot_controls.gd | 55 +++++++++++++++---------- scripts/road/road.gd | 14 +++++-- 12 files changed, 80 insertions(+), 52 deletions(-) diff --git a/project.godot b/project.godot index 1a221e8..8ffa563 100644 --- a/project.godot +++ b/project.godot @@ -120,7 +120,7 @@ controls_break={ controls_add_road={ "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) -, 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={ @@ -150,7 +150,7 @@ controls_prev_road_variant={ controls_reset={ "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) -, 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={ diff --git a/scenes/menus/SettingsMenu.tscn b/scenes/menus/SettingsMenu.tscn index 772ede9..a066ec6 100644 --- a/scenes/menus/SettingsMenu.tscn +++ b/scenes/menus/SettingsMenu.tscn @@ -122,6 +122,7 @@ margin_bottom = 27.0 margin_bottom = 27.0 [node name="action_button" parent="menu/tabs/controls/settings_controls/key_dialog" index="3"] +margin_right = 302.0 margin_bottom = 110.0 [node name="graphics" type="Tabs" parent="menu/tabs"] diff --git a/scenes/road/roadCornerSmall.tscn b/scenes/road/roadCornerSmall.tscn index 23f786a..7807348 100644 --- a/scenes/road/roadCornerSmall.tscn +++ b/scenes/road/roadCornerSmall.tscn @@ -11,7 +11,7 @@ material/1 = null material/2 = null script = ExtResource( 2 ) 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 ) torque_penalties = PoolVector3Array( 0.1, 0.1, -0.5 ) diff --git a/scenes/road/roadCornerSmallFlipped.tscn b/scenes/road/roadCornerSmallFlipped.tscn index 8bf4ea1..6ec010c 100644 --- a/scenes/road/roadCornerSmallFlipped.tscn +++ b/scenes/road/roadCornerSmallFlipped.tscn @@ -11,7 +11,7 @@ material/1 = null material/2 = null script = ExtResource( 2 ) 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 ) torque_penalties = PoolVector3Array( 0.1, 0.1, 0.5 ) diff --git a/scenes/road/roadRamp.tscn b/scenes/road/roadRamp.tscn index 72941b8..9bccaba 100644 --- a/scenes/road/roadRamp.tscn +++ b/scenes/road/roadRamp.tscn @@ -11,7 +11,7 @@ material/1 = null material/2 = null script = ExtResource( 2 ) reset_index = 1 -speed_constrains = PoolVector3Array( 0, 1, -0.05 ) +speed_constrains = PoolVector3Array( 0.5, 0.9, -0.025 ) path_penalties = [ 1.0 ] [node name="Path" type="Path" parent="."] diff --git a/scenes/road/roadRampLong.tscn b/scenes/road/roadRampLong.tscn index 4095b27..e656d73 100644 --- a/scenes/road/roadRampLong.tscn +++ b/scenes/road/roadRampLong.tscn @@ -11,8 +11,10 @@ material/1 = null material/2 = null script = ExtResource( 2 ) reset_index = 1 -speed_constrains = PoolVector3Array( 0, 2, -0.05 ) +speed_constrains = PoolVector3Array( 0.7, 1.9, -0.03 ) path_penalties = [ 2.0 ] [node name="Path" type="Path" parent="."] curve = ExtResource( 3 ) + +[node name="PathFollow" type="PathFollow" parent="Path"] diff --git a/scenes/road/roadRampLongCurved.tscn b/scenes/road/roadRampLongCurved.tscn index 2b3ad8e..40f0414 100644 --- a/scenes/road/roadRampLongCurved.tscn +++ b/scenes/road/roadRampLongCurved.tscn @@ -11,7 +11,7 @@ material/1 = null material/2 = null script = ExtResource( 2 ) 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 ] [node name="Path" type="Path" parent="."] diff --git a/scenes/road/roadRampLongFlipped.tscn b/scenes/road/roadRampLongFlipped.tscn index 66fccbe..f32757d 100644 --- a/scenes/road/roadRampLongFlipped.tscn +++ b/scenes/road/roadRampLongFlipped.tscn @@ -10,7 +10,7 @@ material/0 = null material/1 = null material/2 = null 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 ) [node name="Path" type="Path" parent="."] diff --git a/scripts/game/gamestate.gd b/scripts/game/gamestate.gd index f7bc4bd..d8177ae 100644 --- a/scripts/game/gamestate.gd +++ b/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): players[id] = {"name" : new_name, "color" : new_color, "ready" : new_ready, "prepared" : false} emit_signal("players_changed", players) - + if get_tree().is_network_server(): 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) all_ready = all_ready && players[p_id].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): players.erase(id) - + if game != null: var game_players = game.get_node("players") if game_players.has_node(str(id)): game_players.remove_child(game_players.get_node(str(id))) - + emit_signal("players_changed", players) @@ -63,34 +63,35 @@ remotesync func game_ready(all_ready:bool): remotesync func prepare_game(): get_tree().set_pause(true) - + game = Game.instance() get_tree().get_root().add_child(game) get_tree().get_root().get_node("main_menu").hide() - + for p_id in players: var controls var player = players[p_id] - + var player_scene = Player.instance() player_scene.set_network_master(p_id) player_scene.set_name(str(p_id)) game.get_node("players").add_child(player_scene) player_scene.set_player_name(player.name) player_scene.set_player_color(player.color) - + if p_id == get_tree().get_network_unique_id(): controls = HumanPlayer.instance() controls.set_name("HumanControl_" + str(p_id)) game.add_child(controls) controls.set_player(player_scene.get_path()) controls.set_network_master(p_id) - + player_scene.set_start(player_scene.get_index()) - + 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 player_scene = Player.instance() player_scene.set_name("bot_player" + str(bot_index)) @@ -119,7 +120,7 @@ remotesync func player_prepared(id): if get_tree().is_network_server(): players[id].prepared = true var all_prepared = true - for p_id in players: + for p_id in players: all_prepared = all_prepared && players[p_id].prepared if all_prepared: rpc("post_start_game") @@ -135,7 +136,7 @@ remotesync func set_bots(has_bots:bool): func quit_game(): - if game != null: + if game != null: game.queue_free() game = null @@ -148,5 +149,5 @@ func quit_game(): game_running = false bots = false game_name = "" - + emit_signal("game_ended") diff --git a/scripts/game/route.gd b/scripts/game/route.gd index ca9d12a..627a0c5 100644 --- a/scripts/game/route.gd +++ b/scripts/game/route.gd @@ -14,7 +14,10 @@ func get_last_road(): 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): @@ -26,19 +29,19 @@ remotesync func add_road(type, creator): transform = transform.translated(curve.get_point_position(curve.get_point_count() - 1)) 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()) - + var new_road = roads_factory.get_road_instance(type) new_road.set_global_transform(transform) new_road.set_preview(false) new_road.set_creator(creator) - + if creator > 0: var player = gamestate.game.get_node(str(creator)) if player != null: new_road.set_color(player.get_player_color()) - + add_child(new_road) - + emit_signal("road_added", new_road) diff --git a/scripts/player/bot_controls.gd b/scripts/player/bot_controls.gd index b565ab1..5c68321 100644 --- a/scripts/player/bot_controls.gd +++ b/scripts/player/bot_controls.gd @@ -2,9 +2,13 @@ extends Spatial 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(): @@ -18,27 +22,36 @@ func _physics_process(delta): player.reset() else: if player.get_road() != null: - var rate = randf() 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 - 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 - elif rate > underspeed_rate: - player.thrust = 1 + elif speed - 0.1 < break_constrain: + player.thrust = 1 + elif break_constrain == 0: + player.thrust = 1 else: player.thrust = 0 diff --git a/scripts/road/road.gd b/scripts/road/road.gd index ccdae3e..c92f9c9 100644 --- a/scripts/road/road.gd +++ b/scripts/road/road.gd @@ -85,12 +85,20 @@ func get_next_lane(lane): return lane -func penalty_index(distance:float, speed:float): +func get_constrain_index(distance:float): for index in range(speed_constrains.size()): var constrain = speed_constrains[index] 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