I'm working on making a löve client for the AI game vindinium, and the source code for my client can be found on github.
I'm currently having issues with drawing the map. As the map itself never changes, I figured I'd save on resources by drawing it to a canvas on the first turn instead of redrawing it—your bot only gets one second to get it's move to the server, so it's best not to waste time drawing! However, the canvas seems to be going blank (or something), after it is drawn the first time. At first I thought this had something to do with the window re-sizing stuff that clears canvases, so I turned the canvas into an image to protect it ... but it's still disappearing!
Other images seem to be okay, and I have one blinking on and off each turn. Does anyone have any ideas why this might be?
The most relevant portions of the code are below, and there are no other calls made to the draw_map() function or to gfx.map where the image is stored, so I doubt it's an issue with writing over it.
Code: Select all
-- Draw the map to a canvas, as it never changes.
local function draw_map()
local map_canvas = love.graphics.newCanvas(map.size*gfx.tile_size,
map.size*gfx.tile_size),
love.graphics.setCanvas(map_canvas)
for y = 1, map.size do
if map[y] then
for x = 1, map.size do
if map[y][x] ~= 11 then
love.graphics.draw(gfx[0], x*gfx.tile_size,
y*(gfx.tile_size-gfx.tile_offset))
end
end
end
end
love.graphics.setCanvas()
gfx.map = love.graphics.newImage(map_canvas:getImageData())
end
---
function love.load()
assert(type(nbot) == "table", "Settings table is invalid.")
game.key = assert(nbot.key, "A key must be provided")
game.mode = nbot.mode or "training"
game.loop = nbot.loop or random_move
game.pre_loop = nbot.pre_loop
assert(valid_modes[game.mode], "Invalid game mode: " .. game.mode)
-- Initial connection request
local response = request{key = game.key}
parse_first_response(response)
-- Run pre_loop function if availiable
if game.pre_loop then game.pre_loop() end
end
function love.update()
if not game.finished then
local move = game.loop()
response = request{key = game.key, dir = move}
parse_response(response)
else
love.event.quit()
end
end
function love.draw()
-- init the map if it doesn't exist
if not gfx.map then
draw_map()
end
-- draw the map
love.graphics.draw(gfx.map, 1, 1)
-- draw a test sprite every other turn, because I
-- can't figure out why the map disappears and this doesn't.
if game.turn % 8 == 0 then
love.graphics.draw(gfx[10], 24, 25)
end
end