treasure/score.nim

38 lines
1.1 KiB
Nim

import map_type
import input_type
import turtle_type
import math
proc score*(turtle: Turtle, map: Map, input: Input) =
let path = turtle.path
# create a list of treasures
let treasures = map.getTreasures()
# create a list of distances from each treasure
var distances: seq[float] = @[]
for i in 0..treasures.high():
distances.add(-1.0)
# iterate through the path, and change the distance if it decreases
for i in 0..path.ys.high():
let x = path.xs[i]
let y = path.ys[i]
for j in 0..treasures.high():
let (tx, ty) = treasures[j]
let deltaX = abs(tx - x)
let deltaY = abs(ty - y)
# taxicab distance
let distance = (deltaX + deltaY).float()
if distances[j] < 0 or distance < distances[j]:
distances[j] = distance
# every distance contributes to score, exponentially dropping off
# by the formula Ae^(-k * dist)
var treasureScore = 0.0
for distance in distances:
const A = 100.0
const k = 0.2
treasureScore += A * exp(- k * distance)
turtle.score = treasureScore.int() - (turtle.steps div 5)