Skip to content

Instantly share code, notes, and snippets.

@pauldub
Last active August 29, 2015 14:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pauldub/193ff673e264c0899ae3 to your computer and use it in GitHub Desktop.
Save pauldub/193ff673e264c0899ae3 to your computer and use it in GitHub Desktop.
Vindi
Behavior
Ionium := Vindi Bot clone do (
GoldWithinRange := Action clone do(
radius := 10
start := method(game,
hero := game hero pos
name = "GoldWithinRange(x: #{hero x}, y: #{hero y}, r: #{radius})" interpolate
)
run := method(game,
gold := game board around(game hero pos, radius) select(isGoldMine)
if(gold size > 0,
"Found #{gold size} gold tiles" interpolate println)
if(gold size > 0,
game moveTarget := gold first pos
"success",
"failure"))
)
MoveToTarget := Action clone do(
name = "MoveToTarget"
stuck := false
lastPosition := nil
start := method(game,
self lastPosition := nil
self stuck := false
)
run := method(game,
if(stuck == true, "failure",
if(game hasSlot("moveToPointTarget"),
target := Point fromVector((game moveToPointTarget - game hero pos vector) normalize)
if(target x abs > target y abs,
if(target x < 0, game move = "East")
if(target x > 0, game move = "West"),
if(target y < 0, game move = "North")
if(target y > 0, game move = "South"))
if(target x + target y == 0, game move = "Stay")
if(game position distanceTo(game moveToPointTarget) < 2, "success",
if(self lastPosition == nil,
self lastPosition = game position,
if(game position distanceTo(self lastPosition) < 1, self stuck = true)
self lastPosition = game position)
"running"),
"failure")))
)
CurrentPosition := Decorator clone do(
name = "CurrentPosition"
run := method(game,
game position := game hero pos vector
behavior update(game)
))
key = "go9h6u2c"
behavior = CurrentPosition with(
Seq with(
list(
FindWanderPoint clone setRadius(10),
MoveToTarget clone
)))
play := method(Vindi run(self clone))
)
Behavior
Point do(
fromMap := method(map,
if(map == nil,
self with(0, 0),
self with(map at("x"), map at("y"))))
)
Vindi := Object clone
Vindi Hero := Object clone do(
id := nil
name := nil
userId := nil
elo := nil
pos := nil
life := nil
gold := nil
mineCount := nil
spawnPos := nil
crashed := nil
fromMap := method(map,
hero := self clone
hero id = map at("id")
hero name = map at("name")
hero userId = map at("userId")
hero elo = map at("elo")
hero pos = Point fromMap(map at("pos"))
hero life = map at("life")
hero gold = map at("gold")
hero mineCount = map at("mineCount")
hero spawnPos = Point fromMap(map at("spanwPos"))
hero crashed = map at("crashed")
hero)
)
Vindi Tile := Object clone do(
pos ::= Point with(0, 0) vector
name ::= nil
code ::= nil
x := method(pos at(0))
y := method(pos at(0))
asString := method(
if(name != "patate", code,
color := "0m"
if(name == "wood", color = "92m")
"\033[" .. color .. code .. "\\e[0m"))
isFloor := method(code == " ")
isWood := method(code == "##")
isGoldMine := method(code findSeq("$"))
isTavern := method(code == "[]")
with := method(x, y, code,
tile := self clone setCode(code)
if(tile isFloor) then(
name := "floor"
) elseif(tile isWood) then(
name := "wood"
) elseif(tile isGoldMine) then(
name := "gold mine"
) elseif(tile isTavern) then(
name := "tavern"
) else(
name := "unknown"
)
self clone setPos(Point with(x, y) vector) setName(name) setCode(code))
)
Vindi Board := Object clone do(
debug := false
size := nil
tiles := nil
finished := nil
at := method(x, y, tiles at(size * y + x))
around := method(point, radius,
results := List clone
for(x, point x - radius / 2, point x + radius / 2,
if(x < 0, continue)
if(x > size, continue)
for(y, point y - radius / 2, point y + radius / 2,
if(y < 0, continue)
if(y > size, continue)
results appendIfAbsent(self at(x, y))))
results)
fromMap := method(map,
board := self clone
board size = map at("size") asNumber
board finished = map at("finished")
board tiles = List clone
size := board size
for(i, 0, (size * size) + 2,
y := (i / size) floor
x := i - (size * y)
code := map at("tiles") exclusiveSlice(i * 2, i * 2 + 2)
if(debug != false,
"tile(x: #{x}, y: #{y} code: #{code})" interpolate println)
board tiles append(Vindi Tile with(x, y, code)))
board)
asString := method(
buf := List clone
lastY := 0
tiles foreach(tile,
buf append(tile asString)
if(tile x + 1 == size, buf append("\n")))
buf join(""))
)
Vindi Game := Object clone do(
id := nil
key ::= nil
turns ::= 300
mapId ::= nil
turn := nil
maxTurns := nil
heroes := List clone
board := nil
finished := nil
hero := nil
token := nil
viewUrl := nil
playUrl := nil
move := method(dir,
clone fetch(playUrl, Map with("key", key asString, "dir", dir asString)))
with := method(key,
params := Map with("key", key asString, "turns", turns asString)
if(mapId != nil, params atPut("map", mapId asString))
clone fetch(Vindi api, params))
fetch := method(url, params,
err := try(
result := Yajl parseJson(URL with(url) post(params, Map clone)))
if(err != nil, nil,
if(result hasSlot("at"),
readGame(key, turns, mapId, result),
result println
nil)))
readGame := method(key, turns, mapId, params,
game := clone setKey(key) setTurns(turns) setMapId(mapId)
game id = params at("game") at("id")
game turn = params at("game") at("turn")
game maxTurns = params at("game") at("maxTurns")
game heroes = params at("game") at("heroes") map(h, Vindi Hero fromMap(h))
game turn = params at("game") at("turn")
game board = Vindi Board fromMap(params at("game") at("board"))
game finished = params at("game") at("finished")
game hero = Vindi Hero fromMap(params at("hero"))
game token = params at("token")
game viewUrl = params at("viewUrl")
game playUrl = params at("playUrl")
game)
)
Vindi do(
trainingApi := "http://vindinium.org/api/training"
api := trainingApi
newGame := method(key, Vindi Game with(key))
run := method(bot,
game := newGame(bot key)
"game id: #{game id} viewUrl: #{game viewUrl} hero: @#{game hero id}" interpolate println
while(game finished != "false",
"turn: #{game turn}/#{game maxTurns} life: #{game hero life} gold: #{game hero gold}" interpolate println
# game board println
direction := bot move(game)
"direction: #{direction}" interpolate println
game = game move(direction)
if(game == nil, break)
)
game)
)
Vindi Bot := Object clone do(
key := nil
context := Object clone do(
moveToPointTarget := nil
move := nil
hero := method(game hero)
)
behavior := Behavior Nop clone
move := method(
game := call evalArgAt(0)
context game := game
context move := nil
result := behavior update(context)
move := nil
if(result == "success", move = context move)
if(result == "running",
if(context move != nil, move = context move))
("move: " .. move) println
if(move == nil, "Stay", move))
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment