sturr doggo

This commit is contained in:
Jonas Mucke
2020-04-19 20:31:14 +02:00
parent 1a58a9689e
commit e21a065b60
7 changed files with 126 additions and 69 deletions

View File

@@ -7,6 +7,7 @@ const Grid = preload("res://Maps/Grid.gd")
var grid
enum{
LENGTH,
WAY
@@ -24,7 +25,7 @@ enum{
}
var ExecutionState = AI_MOVE
var movmentState = NOTHING
var ai_movement_state = NOTHING
var numbers = [0,0,0,0,0,0,0,0,0,0]
var prios = [7,0,6,5,4,3,2,1,1,4]
@@ -33,8 +34,14 @@ var totalPrioTurn = 0
var executesTurn = false
var abortProb = 0.01
var targetField = [0,0]
var targetFieldCur = [0,0]
var targetFieldUsed = false
var actionField = [0,0]
var actionFieldUsed = false
var threadTime = 0.8
var threadDelta = 0.0
#calculates the sum of all present prios
func calcTotalPrio():
var sum = 0
@@ -66,10 +73,10 @@ func calcPrioTable():
i += 1
return table
#14+7 0.999
#updates heart and bonfire prio
func adjustPrio(currentHealth, maxHealth):
var prioVal = 1000 - (float(currentHealth)/float(maxHealth))*1000
var prioVal = 40.0 - (float(currentHealth)/float(maxHealth))*40.0
var bonfire = prioVal
var hearts = prioVal - 1
if(hearts < 0):
@@ -93,23 +100,23 @@ func getMoveDescription(myPosition : Vector2, targetPositions):
func getCost(field):
var cost = 0
for i in grid.prio_grid[field.x][field.y]:
for i in grid.object_grid[field.x][field.y]:
match i:
Grid.Kind.DAMAGE:
cost += prios[Grid.Kind.BONFIRE] * 6
cost += prios[Grid.Kind.BONFIRE] * 32
Grid.Kind.SLOW:
cost += 2
cost += 1
return cost
#return an heurestic of distance
# curr - current position
# targ - a target position
func h(curr, target):
return min(abs(target[0]-curr[0]),abs(target[0]-curr[0]))
func h_fn(curr, target):
return sqrt(pow(target[0]-curr[0],2)+pow(target[0]-curr[0],2))
# currCost - currentCost
# target - position of the field to move to
func g(currCost, target):
func g_fn(currCost, target):
return currCost + getCost(target)
# Returns the list of adjacent nodes
@@ -117,15 +124,17 @@ func adjacent(currentPosition, can_roll = false):
var adj := []
#adj.append([STEP, Vector2(0,0)])
var p = currentPosition
var pot_adj_step = [[p[0]-1, p[1]-1], [p[0]-1, p[1]-0], [p[0]-1, p[1]+1],
var pot_adj_step = [[p[0]-1, p[1]-1], [p[0]-1, p[1]-0], [p[0]-1, p[1]+1],
[p[0]+0, p[1]-1], [p[0]+0, p[1]+1],
[p[0]+1, p[1]-1], [p[0]+1, p[1]+0], [p[0]+1, p[1]+1]]
var pot_adj_roll = [[p[0]-2, p[1]-2], [p[0]-2, p[1]-0], [p[0]-2, p[1]+2],
[p[0]+0, p[1]-2], [p[0]+0, p[1]+2],
[p[0]+2, p[1]-2], [p[0]+2, p[1]+0], [p[0]+2, p[1]+2]]
for next in pot_adj_step:
for i in range(pot_adj_step.size()):
var next = pot_adj_step[i]
if(next[0]<0):
continue
if(next[0]>13):
@@ -137,10 +146,30 @@ func adjacent(currentPosition, can_roll = false):
if(grid.used_grid[next[0]][next[1]]):
continue
if(grid.object_grid[next[0]][next[1]][0]!=Grid.Kind.WALL):
adj.append([STEP, Vector2(next[0],next[1])])
if(i==0):
if(grid.object_grid[pot_adj_step[1][0]][pot_adj_step[1][1]][0]!=Grid.Kind.WALL &&
grid.object_grid[pot_adj_step[3][0]][pot_adj_step[3][1]][0]!=Grid.Kind.WALL):
adj.append([STEP, Vector2(next[0],next[1]),1.1])
continue
if(i==2):
if(grid.object_grid[pot_adj_step[1][0]][pot_adj_step[1][1]][0]!=Grid.Kind.WALL &&
grid.object_grid[pot_adj_step[4][0]][pot_adj_step[4][1]][0]!=Grid.Kind.WALL):
adj.append([STEP, Vector2(next[0],next[1]),1.1])
continue
if(i==5):
if(grid.object_grid[pot_adj_step[3][0]][pot_adj_step[3][1]][0]!=Grid.Kind.WALL &&
grid.object_grid[pot_adj_step[6][0]][pot_adj_step[6][1]][0]!=Grid.Kind.WALL):
adj.append([STEP, Vector2(next[0],next[1]),1.1])
continue
if(i==7):
if(grid.object_grid[pot_adj_step[4][0]][pot_adj_step[4][1]][0]!=Grid.Kind.WALL &&
grid.object_grid[pot_adj_step[6][0]][pot_adj_step[6][1]][0]!=Grid.Kind.WALL):
adj.append([STEP, Vector2(next[0],next[1]),1.1])
continue
adj.append([STEP, Vector2(next[0],next[1]),1.0])
if not can_roll:
return adj
return adj
for next in pot_adj_roll:
if(next[0]<0):
@@ -154,7 +183,7 @@ func adjacent(currentPosition, can_roll = false):
if(grid.used_grid[next[0]][next[1]]):
continue
if(grid.object_grid[next[0]][next[1]][0]!=Grid.Kind.WALL):
adj.append([ROLL, Vector2(next[0],next[1])])
adj.append([ROLL, Vector2(next[0],next[1]),2.0])
return adj
@@ -179,14 +208,10 @@ func AStar(source, target):
grid.used_grid[node[2][0]][node[2][1]] = true
var adj_list = adjacent(node[2])
for i in adj_list:
var move_cost = 0
if (i[0] == STEP):
move_cost = 1
else:
move_cost = 2
var move_cost = i[2]
var g_val = g(node[1]+move_cost, i[1])
var h_val = h(i[1], target)
var g_val = g_fn(node[1]+move_cost, i[1])
var h_val = h_fn(i[1], target)
#[g+h(x), g(x), current, from, kind]
var new_node = [g_val+h_val, g_val,i[1], node[2], i[0]]
@@ -194,43 +219,68 @@ func AStar(source, target):
return [NOTHING, [0,0]]
func makeMove(delta):
#if(actionFieldUsed==true):
# var random = randf()
# if(random < mindChangeProbability):
# ExecutionState = AI_MOVE
if ExecutionState == AI_MOVE:
threadDelta = 0
var currentPosition = grid._pixel_to_grid_coords(global_position)
var enemyKind = calcEnemyKind()
if(enemyKind==Grid.Kind.TERMINAL_SYMBOL):
return
var target = grid.get_nearest(currentPosition, enemyKind)
var MoveAdvice = getMoveDescription(currentPosition, target)
var calcNew = false
var target
var MoveAdvice
if(actionFieldUsed==false):
calcNew = true
if(calcNew==true):
var enemyKind = calcEnemyKind()
if(enemyKind==Grid.Kind.TERMINAL_SYMBOL):
return
target = grid.get_nearest(currentPosition, enemyKind)
actionField = target
actionFieldUsed = true
MoveAdvice = getMoveDescription(currentPosition, target)
else:
MoveAdvice = getMoveDescription(currentPosition, actionField)
grid.reset_history()
target = MoveAdvice[1]
if(MoveAdvice[0]==STEP):
run(Vector2(target[0]-currentPosition[0], target[1]-currentPosition[1]), delta*10)
targetField = target
run(Vector2(target[0]-currentPosition[0], target[1]-currentPosition[1]), delta*4)
targetFieldCur = target
targetFieldUsed = true
movmentState = STEP
ai_movement_state = STEP
elif(MoveAdvice[0]==ROLL):
roll(Vector2(target[0]-currentPosition[0], target[1]-currentPosition[1]), delta*10)
roll(Vector2(target[0]-currentPosition[0], target[1]-currentPosition[1]), delta*4)
targetFieldCur = target
targetFieldUsed = true
targetField = target
ExecutionState = EXECUTING
grid.reset_history()
elif ExecutionState == EXECUTING:
if(targetFieldUsed):
var cur = grid._pixel_to_grid_coords(global_position)
var distance = sqrt(pow(cur[0]-targetField[0],2)+ pow(cur[1]-targetField[1],2))
if(distance<0.01):
var distance = sqrt(pow(cur[0]-targetFieldCur[0],2)+ pow(cur[1]-targetFieldCur[1],2))
if(distance<0.4):
targetFieldUsed = false
ExecutionState = AI_MOVE
if(targetFieldCur[0]==actionField[0]&&targetFieldCur[1]==actionField[1]):
actionFieldUsed = false
else:
var currentPosition = grid._pixel_to_grid_coords(global_position)
if(movmentState==STEP):
run(Vector2(targetField[0]-currentPosition[0], targetField[1]-currentPosition[1]), delta*10)
elif(movmentState==ROLL):
roll(Vector2(targetField[0]-currentPosition[0], targetField[1]-currentPosition[1]), delta*10)
else:
if(ai_movement_state==STEP):
run(Vector2(targetFieldCur[0]-currentPosition[0], targetFieldCur[1]-currentPosition[1]), delta*4)
elif(ai_movement_state==ROLL):
run(Vector2(targetFieldCur[0]-currentPosition[0], targetFieldCur[1]-currentPosition[1]), delta*4)
threadDelta = threadDelta + delta
if(threadDelta>threadTime):
ExecutionState = AI_MOVE
pass
actionFieldUsed = false
# API Interface for ai_hero -> methods are handled in player.gd
func attac(direction, delta):