Added Interface for AI control

This commit is contained in:
2020-04-19 14:25:15 +02:00
parent e0e6662309
commit 1c2bb67246
4 changed files with 93 additions and 74 deletions

View File

@@ -1,10 +1,11 @@
extends Node extends Node
const Kind = preload("res://Overlap/Kind.gd") # Relative pat const Kind = preload("res://Overlap/Kind.gd") # Relative path
var object_grid : Array = [] var object_grid : Array = []
var prio_grid : Array = [] var prio_grid : Array = []
var used_grid : Array = []
var time_passed := 0.0 var time_passed := 0.0
var offset var offset
export(float,0,42.0) var refresh_rate = 1.0 export(float,0,42.0) var refresh_rate = 1.0
@@ -42,10 +43,11 @@ func _ready():
for x in range(14): for x in range(14):
object_grid.push_back([]) object_grid.push_back([])
prio_grid.push_back([]) prio_grid.push_back([])
used_grid.push_back([])
for y in range(7): for y in range(7):
object_grid[x].push_back([Kind.FIELD]) object_grid[x].push_back([Kind.FIELD])
prio_grid[x].push_back([Kind.TERMINAL_SYMBOL]) prio_grid[x].push_back([Kind.TERMINAL_SYMBOL])
used_grid[x].push_back(false)
for tile in walls.get_used_cells(): for tile in walls.get_used_cells():
if(_is_in_grid(tile)): if(_is_in_grid(tile)):
@@ -54,6 +56,12 @@ func _ready():
_update_grid() _update_grid()
func _reset_history():
for x in range(14):
for y in range(7):
used_grid[x][y] = false
func _pixel_to_grid_coords(pixel : Vector2) -> Vector2: func _pixel_to_grid_coords(pixel : Vector2) -> Vector2:
var new_coords : Vector2 var new_coords : Vector2
new_coords.x = floor((pixel.x-offset.x)/32.0) new_coords.x = floor((pixel.x-offset.x)/32.0)

View File

@@ -1,18 +1,7 @@
extends Node extends Player
const PrioQueue = preload("prio_queue.gd") # Relative pat
enum{ const PrioQueue = preload("prio_queue.gd") # Relative path
BOSS, const Grid = preload("res://Maps/Grid.gd")
TORCH,
MINION,
RED,
BLUE,
GREEN,
HEART,
BONFIRE,
BARREL,
TERMINAL_SYMBOL
}
enum{ enum{
LENGTH, LENGTH,
@@ -40,9 +29,6 @@ enum{
var ExecutionState = AI_MOVE var ExecutionState = AI_MOVE
var Grid = []
var used_Flags = []
var numbers = [0,0,0,0,0,0,0,0,0] var numbers = [0,0,0,0,0,0,0,0,0]
var prios = [7,6,5,4,3,2,0,0,4] var prios = [7,6,5,4,3,2,0,0,4]
@@ -53,11 +39,11 @@ var abortProb = 0.01
#calculates the sum of all present prios #calculates the sum of all present prios
func calcTotalPrio(): func calcTotalPrio():
var sum = 0 var sum = 0
var i = BOSS var i = Grid.Kind.BOSS
while i != TERMINAL_SYMBOL: while i != Grid.Kind.TERMINAL_SYMBOL:
if(numbers[i]>0): if(numbers[i]>0):
sum += prios[i] sum += prios[i]
i=i+1 i += 1
return sum return sum
#calculates the relative porio #calculates the relative porio
@@ -71,10 +57,10 @@ func calcPrioTable():
var sum = calcTotalPrio() var sum = calcTotalPrio()
var i = 0; var i = 0;
while i != TERMINAL_SYMBOL: while i != Grid.Kind.TERMINAL_SYMBOL:
lower += calcRelPrio(i, sum) lower += calcRelPrio(i, sum)
table[i] = lower table[i] = lower
i = i+1 i += 1
return table return table
@@ -82,11 +68,11 @@ func calcPrioTable():
func adjustPrio(currentHealth, maxHealth): func adjustPrio(currentHealth, maxHealth):
var prioVal = 10 - (currentHealth/maxHealth)*10 var prioVal = 10 - (currentHealth/maxHealth)*10
var bonfire = prioVal var bonfire = prioVal
var hearts = prioVal -1 var hearts = prioVal - 1
if(hearts < 0): if(hearts < 0):
hearts = 0 hearts = 0
prios[BONFIRE]=bonfire prios[Grid.Kind.BONFIRE]=bonfire
prios[HEART]=hearts prios[Grid.Kind.HEART]=hearts
#return the enemie which will be attacked #return the enemie which will be attacked
func calcEnemy(): func calcEnemy():
@@ -97,36 +83,27 @@ func calcEnemy():
i=i+1 i=i+1
return i return i
func getTargetField(currentField): func getTargetField(currentField):
return [0,0] return Grid.prio_grid[currentField.x][currentField.y]
pass
#todo
#returns a move #returns a move
func getMoveDescription(myPosition : Vector2, targetPositions): func getMoveDescription(myPosition : Vector2, targetPositions):
var way = AStar(myPosition, targetPositions[0]) return AStar(myPosition, targetPositions[0])
#TODO choose enemie with loest distance
return 0
func getFieldState(position):
#todo
pass
func getCost(field): func getCost(field):
# var cost = 0 var cost = 0
# for i in Feld[position.x][position.y]: for i in Grid.prio_grid[field.x][field.y]:
# match i: match i:
# DAMAGE : cost += damage*prios[BONFIRE]*3 Grid.kind.DAMAGE : cost += prios[Grid.kind.BONFIRE] * 6
# SLOW : cost += 2 Grid.kind.SLOW : cost += 2
# return cost
#return cost
return 0
#return an heurestic of distance #return an heurestic of distance
# curr - current position # curr - current position
# targ - atarget position # targ - a target position
func h(curr, target): func h(curr, target):
return min(abs(target[0]-curr[0]),abs(target[0]-curr[0])) return min(abs(target[0]-curr[0]),abs(target[0]-curr[0]))
@@ -135,8 +112,8 @@ func h(curr, target):
func g(currCost, target): func g(currCost, target):
return currCost + getCost(target) return currCost + getCost(target)
#returns the list of adjacent nodes # Returns the list of adjacent nodes
func adjacent(currentPosition): func adjacent(currentPosition, can_roll = false):
var adj := [] var adj := []
#adj.append([STEP, Vector2(0,0)]) #adj.append([STEP, Vector2(0,0)])
var p = currentPosition var p = currentPosition
@@ -157,11 +134,14 @@ func adjacent(currentPosition):
continue continue
if(next[1]>6): if(next[1]>6):
continue continue
if(used_Flags[next[0]][next[1]]==true): if(Grid.used_grid[next[0]][next[1]]):
continue continue
if(Grid[next[0]][next[1]][0]!=WALL): if(Grid[next[0]][next[1]][0]!=WALL):
adj.append([STEP, Vector2(next[0],next[1])]) adj.append([STEP, Vector2(next[0],next[1])])
if not can_roll:
return adj
for next in pot_adj_roll: for next in pot_adj_roll:
if(next[0]<0): if(next[0]<0):
continue continue
@@ -171,7 +151,7 @@ func adjacent(currentPosition):
continue continue
if(next[1]>6): if(next[1]>6):
continue continue
if(used_Flags[next[0]][next[1]]==true): if(Grid.used_grid[next[0]][next[1]]):
continue continue
if(Grid[next[0]][next[1]][0]!=WALL): if(Grid[next[0]][next[1]][0]!=WALL):
adj.append([ROLL, Vector2(next[0],next[1])]) adj.append([ROLL, Vector2(next[0],next[1])])
@@ -179,7 +159,6 @@ func adjacent(currentPosition):
return adj return adj
func AStar(source, target): func AStar(source, target):
#swap rtarget and source, when target source istr reached, do inversxse step #swap rtarget and source, when target source istr reached, do inversxse step
# node layout [g+h(x), g(x), current, from, kind] # node layout [g+h(x), g(x), current, from, kind]
@@ -192,16 +171,16 @@ func AStar(source, target):
while !Q.empty(): while !Q.empty():
var node = Q.delMin() var node = Q.delMin()
#check if reached # Check if reached
if(node[2][0]==target[0] and node[2][1]==target[1]): if(node[2][0] == target[0] and node[2][1] == target[1]):
return [node[4], 0]#todo map movement] return [node[4], node[3]] # 4 is kind | 3 is from
#set flag # Set flag
used_Flags[node[2][0]][node[2][1]] = true Grid.used_grid[node[2][0]][node[2][1]] = true
var adj_list = adjacent(node[2]) var adj_list = adjacent(node[2])
for i in adj_list: for i in adj_list:
var move_cost = 0 var move_cost = 0
if(i[0]==STEP): if (i[0] == STEP):
move_cost = 1 move_cost = 1
else: else:
move_cost = 2 move_cost = 2
@@ -213,21 +192,22 @@ func AStar(source, target):
var new_node = [g_val+h_val, g_val,i[1], node, i[0]] var new_node = [g_val+h_val, g_val,i[1], node, i[0]]
Q.insert(new_node) Q.insert(new_node)
return [NOTHING, 0] return [NOTHING, [0,0]]
func makeMove(): func makeMove():
match ExecutionState: match ExecutionState:
EXECUTING: EXECUTING:
pass pass
AI_MOVE: AI_MOVE:
var field = [0.1*prios[BONFIRE], 1] var field = [0.1 * prios[Grid.Kind.BONFIRE], 1]
var decision = randf() var decision = randf()
var i = 0 var i = 0
while field[i] > decision: while field[i] > decision:
i=i+1 i += 1
if(i==0): if (i == 0):
var targetField = getTargetField(currentField) var targetField = getTargetField(Grid.prio_grid)
# Todo: move player
else: else:
pass pass
pass pass

View File

@@ -64,6 +64,34 @@ func _physics_process(delta):
func move(): func move():
move_and_slide(velocity) move_and_slide(velocity)
func set_animation_tree_vector():
animation_tree.set("parameters/idle/blend_position", rollvector)
animation_tree.set("parameters/hit/blend_position", rollvector)
animation_tree.set("parameters/roll/blend_position", rollvector)
animation_tree.set("parameters/run/blend_position", rollvector)
# API Interface for ai_hero
func attac(direction):
rollvector = direction
set_animation_tree_vector()
movementState = moveState.HIT
# API Interface for ai_hero
func roll(direction):
rollvector = direction
set_animation_tree_vector()
movementState = moveState.ROLL
# API Interface for ai_hero
func run(direction):
rollvector = direction
set_animation_tree_vector()
movementState = moveState.MOVE
func movement_move(delta): func movement_move(delta):
var input_vector = Vector2.ZERO var input_vector = Vector2.ZERO
# This is a clever way to handle directional input # This is a clever way to handle directional input

View File

@@ -12,7 +12,7 @@
script = ExtResource( 2 ) script = ExtResource( 2 )
[node name="WallSprite" type="Sprite" parent="."] [node name="WallSprite" type="Sprite" parent="."]
position = Vector2( 352, 184 ) position = Vector2( 351.915, 184 )
texture = ExtResource( 4 ) texture = ExtResource( 4 )
region_enabled = true region_enabled = true
region_rect = Rect2( 0, 0, 1280, 720 ) region_rect = Rect2( 0, 0, 1280, 720 )
@@ -42,6 +42,9 @@ position = Vector2( 296, -16 )
[node name="Boss_template" parent="YSort" instance=ExtResource( 17 )] [node name="Boss_template" parent="YSort" instance=ExtResource( 17 )]
[node name="DebugLabel" parent="YSort/Boss_template" index="6"]
text = "Obermutti"
[node name="Grid" parent="." instance=ExtResource( 18 )] [node name="Grid" parent="." instance=ExtResource( 18 )]
[editable path="YSort/Boss_template"] [editable path="YSort/Boss_template"]