[Lua] MapParser (High Level API)

This is a Map parser library for CS2D’s lua engine. Benchmarked to process 6,000 tiles per second. (A 2,000% improvement over the older parser). This also demonstrates that it is possible to have object-orientation within a lua script and to work with CTypes.

Code/Download:
http://pastebin.com/f33323412
MapParser
Future implementations will also add caching.

API:

Map:new(mapfile)
Creates a new instance of the Map object
IE: map = Map:new("maps/de_dust.map")

Map:invert(table)
Inverts the x and the y of a given table (must be in matrix form)
IE: dumpable = map:invert(map.tiles)

Map:dumpMeta()
Creates a dump of all of the meta-information stored in a map
IE: map:dumpMeta()
Output:
[Table:(Anonymous)]
code = 8100×79$123225%4595857
loadtime = 1.77
header = Unreal Software’s Counter-Strike 2D Map File (max)

Map:mapogram()
Prints out a graphical display of the collision gradients
IE: map:mapogram() — See above for output

Map.tiles[x][y]
A table of tile data, accessed through map.tiles[x][y]
IE: tile_3_4 = map.tiles[3][4] — stores the tile at 3,4 into tile_3_4

Map.modes[x][y]
A table of the tile modes, accessed through map.modes[x][y], note here that modes will tell you whether a tile is a wall or not
IE: mode_20_32 = map.modes[20][32]

Map.entities[id]
A table of the individual entities, accessed through map.entities[id]
IE: entity_3 = map.entities[3]

Map.collisions[x][y]
A table of walls and obstacles. 0 if not an obstacle, 1 through 4 if it is.
IE: map.collisions[7][8]

Map.gradient, Map.antigradient – use ipairs
Table of collisions and non-collisions that can be iterated via ipairs(), stored in {x, y, mode} format
IE: for _i, coordinate in ipairs(map.gradient) do blahblah() end

Example implementation of http://unrealsoftware.de/lab_cat.php?ca … =#entry499

dofile(core_dir.."map.lua")
current_map = Map:new("maps/"..map("name")..".map")

function tile(x,y,prop)
     local args = {}

     args.entity = current_map.entities [x] [y]
     args.tile = current_map.tiles[x] [y]
     args.mode= current_map.modes[x] [y]
     args.collision= current_map.collisions[x] [y]

     if args.collision == 0 then args.collision = false end

     if prop then
               if args[prop] then return args[prop] end
     end

     return args
end

if tile(12,30,"collision") then
     --Custom Code Here.
end

Notices
Requires the struct.lua, string.lua, and table.lua libraries. (Look inside amx2d/core)

This entry was posted in Blog, Lua, Programming, Snippets and tagged , , , , , , , , , , , , , . Bookmark the permalink.

This website uses IntenseDebate comments, but they are not currently loaded because either your browser doesn't support JavaScript, or they didn't load fast enough.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>