Computer Science Canada

Finding a specific spot in a text file

Author:  Raknarg [ Thu Nov 24, 2011 5:03 pm ]
Post subject:  Finding a specific spot in a text file

Hey guys. I know this is in the Turing Walkthrough, but I didn't quite understand it, so I'm hoping someone could clarify this for me...

Basically I'm starting and RPG (just to see if I can do it, and for fun Razz), and one of my problems is that I'm using a text file for the map, but I want to be able to change the states of a certain place on the map (for instance you would use the code chst+ for a chest that's closed and chst- for a chest that's opened). However, I'm not quite sure how to find a specific spot and then go about changing it.

Author:  ProgrammingFun [ Thu Nov 24, 2011 5:14 pm ]
Post subject:  RE:Finding a specific spot in a text file

Possibly grid the map and assign each chest coordinates on that grid? That's what I did for a game I previously programmed last year.

Author:  Raknarg [ Thu Nov 24, 2011 5:19 pm ]
Post subject:  RE:Finding a specific spot in a text file

Yeah thats what I'm doing, but I mean for the text file itself, because I need to make a way for the computer to remember if the chest was opened.

Author:  Tony [ Thu Nov 24, 2011 5:54 pm ]
Post subject:  RE:Finding a specific spot in a text file

Just overwrite the entire file. Presumably save doesn't happen often.

Alternatively you could use seek and write in binary mode. I'm not sure if that will introduce complications when you start mixing it with text-mode creation/edits of the files.

Author:  Aange10 [ Thu Nov 24, 2011 6:49 pm ]
Post subject:  RE:Finding a specific spot in a text file

I could be totally wrong, but I've seen configure files that would have something looking like

Quote:

-FireSpread = 15
-SpawnPoint = 100, 100, 100
-Speed = 300
-fly = true
-chest = closed


if it is optional for you to do something like that, you could simply 'load' each line and then go through where you store it and have the computer look past the = sign, to get the information it needs.

If that is an applicable way for you to do this.

Author:  Raknarg [ Fri Nov 25, 2011 6:39 pm ]
Post subject:  RE:Finding a specific spot in a text file

Yeah, I think im giving up on putting the chest in the text, I'll probably just use a number array to keep track instead... thanks for the suggestions guys

Author:  Aange10 [ Fri Nov 25, 2011 7:08 pm ]
Post subject:  RE:Finding a specific spot in a text file

^-^ Yup, no problem.

I'm so unsure of myself because I'm not really quite sure what you are doing in the text file.

Anyways, the way I said is simple string manipulation/sorting.


Edit:

But to answer your thread question:

I don't know how to find a specific spot in a text file, but there are ways to organize your data and store it in the text file. The only way I know, however, of finding something specifically in a text file is storing the text file as an array, and then sorting through the array.

Author:  Raknarg [ Fri Nov 25, 2011 7:16 pm ]
Post subject:  RE:Finding a specific spot in a text file

To answer your first question, Imagine a file like this:

grss grss
grss grss

In my program, that would just be read as four grass tiles and it would draw them to the screen. Now what if I have this:

grss chst+
grss grss

chst+ Could be considered as a closed chest. Now lets saw your character is on the top right, and activates the chest. An event occurs, and after that, the chest is forever open. However, I need to find a way to change that old text to this:

grss chst-
grss grss

Noe that would be easy for four tiles. What if I have a hundred tiles or more? I need to make an algorithm to find the spot the chest was in in the text file and then change the data.

And yes, I can sort through the array, but I need the information to be saved, and I dont want to have to rewrite an entire map every time if I dont hafve to.

Author:  Tony [ Fri Nov 25, 2011 7:55 pm ]
Post subject:  Re: RE:Finding a specific spot in a text file

Raknarg @ Fri Nov 25, 2011 7:16 pm wrote:
I dont want to have to rewrite an entire map every time if I dont hafve to.

as was asked above, why not?

Ideally, yes -- that feels like work... suboptimal... wrong. But from a Software Engineering stand point -- how much of a performance hit is that?

Author:  Aange10 [ Fri Nov 25, 2011 8:27 pm ]
Post subject:  RE:Finding a specific spot in a text file

OT:
This reminds me a of Minecraft.

On Topic:

http://www.minecraftwiki.net/wiki/Alpha_Level_Format

might help.

Wait, my off topic was on topic.. hmmm Smile

Author:  Aange10 [ Fri Nov 25, 2011 9:11 pm ]
Post subject:  RE:Finding a specific spot in a text file

Also, could you not assign numbers to each chest? So I wouldnt be changing chst+ I'd be changing chst+1234 to chst-1234

Author:  Raknarg [ Fri Nov 25, 2011 9:31 pm ]
Post subject:  RE:Finding a specific spot in a text file

Theres no need for numbers. The chest is already placed at a certain spot, so it doesnt matter whether or not it's assigned a number. Thats the point of giving each spot its own code; I can pull apart the string and use the information to interact with the game

Author:  Aange10 [ Fri Nov 25, 2011 9:37 pm ]
Post subject:  RE:Finding a specific spot in a text file

Raknarg just wrote:

The chest is already placed at a certain spot


Raknarg earlier wrote:

I'm not quite sure how to find a specific spot



And your point is?


Raknarg wrote:

it doesnt matter whether or not it's assigned a number


wheres the problem?

Author:  ProgrammingFun [ Fri Nov 25, 2011 9:56 pm ]
Post subject:  Re: RE:Finding a specific spot in a text file

Aange10 @ Fri Nov 25, 2011 9:37 pm wrote:
wheres the problem?

Posted Image, might have been reduced in size. Click Image to view fullscreen.

Author:  Raknarg [ Fri Nov 25, 2011 10:10 pm ]
Post subject:  RE:Finding a specific spot in a text file

BECAUSE I need to be able to read one specific string. It doesnt matter what I put on that string, If I cant find any one particular spot and change it, then it's pointless. I'd just have to use a separate text file or something specifically for the chest.

Author:  Raknarg [ Fri Nov 25, 2011 10:11 pm ]
Post subject:  RE:Finding a specific spot in a text file

Like I could just rewrite the file every time it changes, but that doesnt seem like the best option.

Author:  Aange10 [ Fri Nov 25, 2011 11:18 pm ]
Post subject:  RE:Finding a specific spot in a text file

Okay, but your able to keep track of where grass blocks are, so keep track of where chest blocks are too. Just instead of chst, define it by its number. That away you can assign each chest a specific state.

Instead of saying "Theres an 8 by 8 block of grass at 300, 300, 300 with an open chest"

you can say "There's an 8 by 8 block of grass, at 300, 300, 300 with chest 13486"


say your player goes within 20 blocks of a chest, you can tell the computer "If the player is within 20 blocks of a chest, then take the chest's name, and find its state."


You could write the state of any given chest in another txt file, or you could keep an array with the chests state.

** Also in case you're thinking the array would get too big, I have two points.

1) I don't know anybody who would have 10,000 chests. or 100,000 or 1,000,000 chests. If it took a second to get a chest, it would take 11 and a half days straight to get 1,000,000 chests.

2) During loading, you could have it filter through the chests and check to see if they exist. (You could have 4 flags. Open/Close/Locked/Destroyed [or use whatever flag you currently use]) And if they don't exist anymore, free the spot in the array. I'm sure your map file would have already replace the "chest" with "air" or whatever it is that was put there.

Author:  Raknarg [ Sat Nov 26, 2011 10:31 am ]
Post subject:  RE:Finding a specific spot in a text file

Of course I can keep track of them, that is not the problem. The problem is rewriting a text file with the desired data in only one spot without rewriting the entire file, if possible.

It's not teh array thats the problem here.

Author:  ProgrammingFun [ Sat Nov 26, 2011 11:04 am ]
Post subject:  Re: RE:Finding a specific spot in a text file

Tony @ Fri Nov 25, 2011 7:55 pm wrote:
as was asked above, why not?

Ideally, yes -- that feels like work... suboptimal... wrong. But from a Software Engineering stand point -- how much of a performance hit is that?

Author:  DemonWasp [ Sat Nov 26, 2011 1:22 pm ]
Post subject:  RE:Finding a specific spot in a text file

The best way to do this kind of thing involves having the file format itself tell you (or imply) where the record you want to change is found. In all examples, I will assume that your file is binary, but this is also technically possible with a text (ASCII) file. Typically, there are a few solutions:

Solution A:
This solution is relatively easy to visualize and quick to program. It has some inflexibility and limitations, but you can work around those relatively easily.
1. Separate your files into sections. Each section holds exactly one type of data (doors, or chests, but never "doors and chests"; have separate sections for each).
2. Make it so that each record always consumes the exact same number of bytes. For example, a "chest" record takes up 10 bytes -- 4 are "CHST", 2 are X-coord, 2 are Y-coord, 2 are flags (open, destroyed, key, etc).
3. The "header" of your file -- the first bit you read -- is a constant length. It describes the initial location of each section and the number of elements in that section.
4. Now, to change chest 423, you just need to edit starting at (start of chests section) + 423 * (size of a chest record), and you need to write exactly (size of a chest record) bytes.

Solution B:
This kind of thing is more useful when your objects are logically grouped, but contain varying amounts of information -- for example, items include swords and rings and potions, but each stores a different amount of information and is therefore a different length. However, it is considerably more complicated.
1. The header of your file stores the number of items and the offset to an "index table".
2. The "index table" is just a bunch of offsets into the file, describing the start of each item. So the first 4 bytes of the index table describe the offset of the first item.
3. When you go to the offset described in the table, you read a "record header" that tells you the type of the record and how long it is. This gives you enough information to figure out how much to read or write.
4. Now, to change chest (record number 423, chest number 1), you would go to the offset table at index 423 and read 4 bytes of offset. Then, at that offset, you would read "CHST" as the type and 6 as the length. Now you know you can write 6 bytes (X coord, Y coord, flags).

If you absolutely must pursue this, I recommend you start with solution A. However, I recommend that you consider just rewriting the file every time you save -- this is how professionally-developed games do it, and for good reason: small random writes to files are disproportionately expensive compared to large, consecutive writes. Another good reason is that doing it this way is considerably harder than just rewriting the entire file -- and therefore, more prone to bugs that damage saved games so they can't be loaded again.

Author:  Raknarg [ Sat Nov 26, 2011 2:17 pm ]
Post subject:  RE:Finding a specific spot in a text file

Thanks, I'll look into that.

Author:  smool [ Tue Dec 06, 2011 8:50 pm ]
Post subject:  RE:Finding a specific spot in a text file

alright, its like what Tony said earlier i think, just overwrite everything.
Load the file at the start of the session, save all the data to variables instead of the txt file being the data, and then rewrite the text file when the user hits save?


: