This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
Mackan90096's profile picture

[Question] Detecting if a letter is in a spot?

Started by Mackan90096, 05 June 2013 - 02:26 AM
Mackan90096 #1
Posted 05 June 2013 - 04:26 AM
Hi!
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
Tjakka5 #2
Posted 05 June 2013 - 05:29 AM
How I did it was like this:

I stored the map in a table, and I made a fuction that would look at
the table and store the properties of the world in another table (" " would be air, "[]" a block, etc).

Then, at every tick it looked at the map, read the coordinates of the player, and see if the coordinates
of the player matched the coordinates of a event block in the properties table.


I do not know how your game saves the map, so if you could share the code, that would help…
(This was hard to type on a phone…)
Mackan90096 #3
Posted 05 June 2013 - 05:32 AM
Well, I don't have multiple maps yet.

But I might add that..

But the code for storing the item and then looking it up in a table.. Can you share that?
Tjakka5 #4
Posted 05 June 2013 - 05:37 AM
Ill try to make a good example when Im back home.
For now you could go in the programs section and search for 'RPG GAME'.

It has the basic fuctions I just explained.
Mackan90096 #5
Posted 05 June 2013 - 05:39 AM
Alright. Thanks!
theoriginalbit #6
Posted 05 June 2013 - 05:39 AM
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.
This is one of the easiest things to do in Lua.
Please, please, please do some research and attempt to learn Lua.
Tables Tutorial
Mackan90096 #7
Posted 05 June 2013 - 05:45 AM
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.
This is one of the easiest things to do in Lua.
Please, please, please do some research and attempt to learn Lua.
Tables Tutorial

I am… But as I only am 13 years old and from Sweden.. I dont have perfect english skills so most of that I don't understand…
H4X0RZ #8
Posted 05 June 2013 - 08:30 AM
So, you want to save a map in a table, line per line? (Char per char)?

EDIT:
Function to load a map into a table (line per line, char per char):

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 ^^
BigSHinyToys #9
Posted 05 June 2013 - 08:51 AM
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
Mackan90096 #10
Posted 05 June 2013 - 01:24 PM
I am not putting the map ibto the table! Only the x and y!
Engineer #11
Posted 05 June 2013 - 02:09 PM
Why dont you track the screen with chars, and overwrite the term.write function. Like so:


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

If you dont know after reading this how it works, I suggest you start on a smaller scale :P/>
And btw, no guarantees that this works as intended, it is untested
Edited on 05 June 2013 - 12:10 PM
BigSHinyToys #12
Posted 05 June 2013 - 05:38 PM
I am not putting the map ibto the table! Only the x and y!
how are you storing the map ??
Mackan90096 #13
Posted 06 June 2013 - 06:21 AM
I am not putting the map ibto the table! Only the x and y!
how are you storing the map ??

What?
BigSHinyToys #14
Posted 06 June 2013 - 06:25 AM
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?
theoriginalbit #15
Posted 06 June 2013 - 06:27 AM
how are you storing the map ??
So in what format and where is the map in a file in a table where?
I'm going to assume he is hoping it will materialise from nothing. :P/>
Edited on 06 June 2013 - 04:28 AM
Mackan90096 #16
Posted 06 June 2013 - 07:44 AM
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?

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
BigSHinyToys #17
Posted 06 June 2013 - 08:21 AM
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
I think I need to see what code you have already or look more closely at the examples I gave earlier.
theoriginalbit #18
Posted 06 June 2013 - 08:31 AM
I think I need to see what code you have already or look more closely at the examples I gave earlier.
Topic. Had a quick scan, there is no map.
Mackan90096 #19
Posted 06 June 2013 - 08:32 AM
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
I think I need to see what code you have already or look more closely at the examples I gave earlier.

My code is on my pastebin.
Bomb Bloke #20
Posted 06 June 2013 - 08:45 AM
xpTable = {
[1] = 100,
[2] = 200,
[3] = 500,
[4] = 1000,
[5] = 2500,
[6] = 4000,
[7] = 5000,
[8] = 7500,
[9] = 10000,
[10] = 11500}

You don't need to specify the index names if they're all sequential numerics. This does the exact same thing:

xpTable = {100, 200, 500, 1000, 2500, 4000, 5000, 7500, 10000, 11500}

For example, xpTable[3] will be 500 either way.

I would define a map table sorta like this:

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}}

Where 1 is a space, 2 is a wall, 3 is a door, 4 is a tree, etc…

So say my character (player) was at position x=2, y=3. map[y][x] would hence return what was in the map position he's sitting in.

If he tried to go up, I'd check map[y-1][x], and see that it contained 1 (an empty space), and let him move.

But if he tried to go left, I'd see that map[y][x-1] was 2 (a wall), and block him. Trying to go down (to map[y+1][x]) might require opening the door first.

To draw the map, you could create a table defining all the types of terrain you're using. Eg:

-- 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
BigSHinyToys #21
Posted 06 June 2013 - 09:14 AM
ow no map just a table of "mobs" not what I would do but we can work with that. Try This
http://pastebin.com/qsWTLRa4
Mackan90096 #22
Posted 06 June 2013 - 09:37 AM
Well, I looked through the "new" code a bit.. And I think it's better to store the map…
Mackan90096 #23
Posted 07 June 2013 - 03:55 AM
Anyway then.. How do I store the map then.

(I'm kind of a noob)
BigSHinyToys #24
Posted 07 June 2013 - 04:26 AM
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
Lyqyd #25
Posted 07 June 2013 - 01:27 PM
If you're getting frustrated with a question-asker who seems to not be responding to your suggestions as much as you might like, just step out of the topic if you can't help properly. Don't post sarcastic image macros. This is Ask a Pro, not 4chan. Please do your best to keep the signal-to-noise ratio high, so everyone needing help can continue to enjoy the same helpful environment that you asked your first questions in. I don't like having to clean up topics in Ask a Pro. We are better than this.

Please note that there is a difference between posting a sarcastic image macro and posting a writeup detailing how a question-asker seems to be refusing help and studiously ignoring good advice. I don't think it's quite to the second one with this thread just yet.
theoriginalbit #26
Posted 07 June 2013 - 01:47 PM
@OP
definitely have a read through BigSHinyToys code examples, they should definitely help you towards the end goal, if you do have any questions though, just ask.

In addition to his code, you would want to do something like this to make sure the player can move there

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.

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. :/
Lyqyd #27
Posted 07 June 2013 - 03:08 PM
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. :/

I can think of two or three where I've eventually had to do that. I'm very thankful that they're quite infrequent. I think I know which one you're referring to, though.