Skip to content

Instantly share code, notes, and snippets.

@wheresalice
Created August 20, 2011 10:54
Show Gist options
  • Save wheresalice/1158959 to your computer and use it in GitHub Desktop.
Save wheresalice/1158959 to your computer and use it in GitHub Desktop.
Create a game map in redis from a txt file which imagemagick has conerted from png to txt
require 'redis'
@redis = Redis.new
# width & height of the map
@x = @y = 0
grassdatafile = open('../resources/maps/land3g.txt')
grassdatafile.readlines.each { |line|
linedata = line.scan(/(\d+),(\d+):\s*\(\s*\d+,\s*\d+,\s*\d+,\d+\)\s+#.{6}(.{2})/)[0]
if linedata
@redis.set "map:#{linedata[0]}:#{linedata[1]}", linedata[2]
@x = linedata[0].to_i if linedata[0].to_i > @x
@y = linedata[1].to_i if linedata[1].to_i > @y
#puts "set map:#{linedata[0]}:#{linedata[1]} #{linedata[2].to_i}"
end
}
rockdatafile = open('../resources/maps/land3r.txt')
rockdatafile.readlines.each { |line|
linedata = line.scan(/(\d+),(\d+):\s*\(\s*255,/)[0]
if linedata
@redis.set "map:#{linedata[0]}:#{linedata[1]}", '01'
@x = linedata[0].to_i if linedata[0].to_i > @x
@y = linedata[1].to_i if linedata[1].to_i > @y
#puts "set map:#{linedata[0]}:#{linedata[1]} 01"
end
}
waterdatafile = open('../resources/maps/land3b.txt')
waterdatafile.readlines.each { |line|
linedata = line.scan(/(\d+),(\d+):\s*\(\s*\d+,\s*\d+,\s*255/)[0]
if linedata
@redis.set "map:#{linedata[0]}:#{linedata[1]}", '00'
@x = linedata[0].to_i if linedata[0].to_i > @x
@y = linedata[1].to_i if linedata[1].to_i > @y
#puts "set map:#{linedata[0]}:#{linedata[1]} 00"
end
}
@redis.set "map:width", @x
@redis.set "map:height", @y
#puts "width: #{@x}"
#puts "height: #{@y}"
if @x != 50 || @y != 50
#puts "warning: map is not 50x50"
end
@map = []
1.upto(@x.to_i) do |x|
@line = ""
1.upto(@y.to_i) do |y|
@line += @redis.get "map:#{x}:#{y}"
end
@map.push(@line)
end
puts @map.inspect
@redis.set "map", @map
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment