I'm developing my game "AsciiRPG" and I'm wondering if you can detect if a character ('!') is in the spot where the player walks using tables. If so. How?
Thanks for help // Mackan90096
Good to see you have taken my advice from previous times.But the code for storing the item and then looking it up in a table.. Can you share that?
Good to see you have taken my advice from previous times.But the code for storing the item and then looking it up in a table.. Can you share that?
This is one of the easiest things to do in Lua.
Please, please, please do some research and attempt to learn Lua.
Tables Tutorial
local playerX,playerY = 1,1
function loadMap(filePath)
if type(filePath) ~= "string" then
error("Not a path")
elseif not fs.exists(filePath) then
error("File doesn't exists")
end
local mapFile = fs.open(filePath, "r")
local sLine = mapFile.readLine()
local line = 1
while sLine do
table.insert(currentMap, {}
sLine = mapFile.readLine()
end
mapFile.close()
mapFile = fs.open(filePath, "r")
local contLine =mapFile.readLine()
while contLine do
for i = 1, contLine:len() do
table.insert(mapFile[line], contLine:sub(i,i)
end
contLine = mapFile.readLine()
line = line+1
end
end
--I hope, I don't forget an end ^^
local screenX, screenY = term.getSize() --# Get term size
local chars = {} --# Create a table
for i = 1, screenX do --# Create subtables for the x and y
chars[i] = {}
for j = 1, screenY do
chars[i][j] = " "
end
end
local nWrite = term.write --# Backup the old term.write
local term.write = function( s, bool ) --# create new term.write
if not bool then --# Check if a second argument is unavailable, if there is you dont want it to 'recognise'
local x, y = term.getCursorPos() --# Get cursor position
for i = 0, ( screenX - s:len() < x and screenX - x or s:len() - 1) do --# Check if there is space enough on the screen
chars[x + i][y] = s:sub( 1+i, 1+i ) --# Add the character to the table
end
nWrite( s ) --# Call the old write
else
nWrite( s )
end
end
how are you storing the map ??I am not putting the map ibto the table! Only the x and y!
how are you storing the map ??I am not putting the map ibto the table! Only the x and y!
how are you storing the map ??
I'm going to assume he is hoping it will materialise from nothing. :P/>So in what format and where is the map in a file in a table where?
I'm guessing the player is walking around a map and you want to know when the player is at a "!" symbol on that map. So in what format and where is the map in a file in a table where?
I think I need to see what code you have already or look more closely at the examples I gave earlier.Yes that's what I want!
I have a function that draws the player character at its correct position.
I also have functions that update the players X and Y
Topic. Had a quick scan, there is no map.I think I need to see what code you have already or look more closely at the examples I gave earlier.
I think I need to see what code you have already or look more closely at the examples I gave earlier.Yes that's what I want!
I have a function that draws the player character at its correct position.
I also have functions that update the players X and Y
xpTable = {
[1] = 100,
[2] = 200,
[3] = 500,
[4] = 1000,
[5] = 2500,
[6] = 4000,
[7] = 5000,
[8] = 7500,
[9] = 10000,
[10] = 11500}
xpTable = {100, 200, 500, 1000, 2500, 4000, 5000, 7500, 10000, 11500}
local map = {
{2,2,2,2,2,2,2,2,2,2,2},
{2,1,1,2,1,1,1,2,1,1,2},
{2,1,1,2,1,4,1,3,1,1,2},
{2,3,2,2,1,1,1,2,1,1,2},
{2,1,1,3,1,4,1,2,2,2,2},
{2,3,2,2,1,1,1,2,1,1,2},
{2,1,1,2,1,1,1,2,1,1,2},
{2,1,1,2,1,4,1,3,1,1,2},
{2,1,1,2,1,1,1,2,1,1,2},
{2,2,2,2,2,2,2,2,2,2,2}}
-- mapkey[i][1] = Character representing that item.
-- mapkey[i][2] = Text colour to use when drawing that item.
-- mapkey[i][3] = Background colour to use when drawing that item.
-- mapkey[i][4] = Whether characters can move onto that item.
-- You could add other properties, like an amount of damage to deal to characters
-- moving onto a given tile type...
local mapkey = {
{" ", colors.white, colors.white, true }, -- Empty space
{"0", colors.gray, colors.black, false}, -- Wall
{"]", colors.yellow, colors.brown, true }, -- Door
{"T", colors.green, colors.white, false}} -- Tree
local function drawMap()
for yy=1,table.getn(map) do -- Loop from 1 to the amount of tables in "map" (map height).
for xx=1,table.getn(map[1]) -- Loop from 1 to the amount of items in the "map[1]" table (map width).
term.setTextColor(mapkey[map[yy][xx]][2])
term.setBackgroundColor(mapkey[map[yy][xx]][3])
term.setCursorPos(xx,yy)
print(mapkey[map[yy][xx]][1])
end
end
end
Anyway then.. How do I store the map then.
(I'm kind of a noob)
This is one way to do it.
http://pastebin.com/2wYkgXga
This is another way.
http://pastebin.com/mzVXTRFR
[EDIT]
and this loads a map from a separate file
http://pastebin.com/k3rWDfiY
local function canMove( x, y )
return map[y]:sub(x,x) == ' '
end
this would make sure it is an empty space on the map. Alternatively you could also use the same way to detect whatever other character you wish.One thread in particular comes to mind when you say that, and oh how painful a thread that was. :/posting a writeup detailing how a question-asker seems to be refusing help and studiously ignoring good advice.
One thread in particular comes to mind when you say that, and oh how painful a thread that was. :/posting a writeup detailing how a question-asker seems to be refusing help and studiously ignoring good advice.