Bay 12 Games Forum

Please login or register.

Login with username, password and session length
Advanced search  
Pages: 1 ... 713 714 [715] 716 717 ... 796

Author Topic: if self.isCoder(): post() #Programming Thread  (Read 890122 times)

Parsely

  • Bay Watcher
    • View Profile
    • My games!
Re: if self.isCoder(): post() #Programming Thread
« Reply #10710 on: March 01, 2018, 11:57:59 pm »

Good stuff milo, thanks! This is a great beginner's rundown it looks like.

If there's anything else even remotely related people can think of I'd love to hear about it.
Logged

milo christiansen

  • Bay Watcher
  • Something generic here
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #10711 on: March 02, 2018, 03:25:14 am »

Some more resources I dug up:

http://www.iwriteiam.nl/Ha_HTCABFF.html
https://en.wikibooks.org/wiki/Reverse_Engineering/File_Formats
http://matthewekenstedt.com/73-06/reverse-engineering-a-file-format/

Googling "file format reverse engineering" should get you quite a bit.

If you have cash to spend Hexinator or a similar tool would probably be pretty helpful too (it looks nice, but I have never used it since all the useful stuff need to be bought).
Logged
Rubble 8 - The most powerful modding suite in existence!
After all, coke is for furnaces, not for snorting.
You're not true dwarven royalty unless you own the complete 'Signature Collection' baby-bone bedroom set from NOKEAS

Urist McScoopbeard

  • Bay Watcher
  • Damnit Scoopz!
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #10712 on: March 05, 2018, 06:17:29 pm »

EDIT: In Python.

Alright. In the code below I am trying to take a (multi-line) .txt file, stored in my_heightmap, and turn it into a list, which I then reformat as an array (such that heightarray is separately holding every value in each row of what will become a two-dimensional, text-based heightmap.) I'm getting the "not subscriptable" error on this line:

Code: [Select]
heightarray[y[x]] = heightlist[count + x]
I'm not sure where I messed up, but as I understand it, the code should take each line of the .txt file and convert it into its own list within heightarray. I'm not sure why I can't take a single instance of a list of values and turn it into a single instance of an array (which is just a list within a list)?

This is possibly woefully inefficient, but I'm just trying to earn my chops on a project that interests me.

Code: [Select]
#converting the heightmap into an array
    heightlist = list(my_heightmap)
    heightarray = []
    count = 0

    for y in range(0,self.size):
        for x in range(0, self.size):
            heightarray[y[x]] = heightlist[count + x]
        count += self.size

    #peak generation
    for peaks in range(0, round(self.size/30)):
        y = random.randomint(0,size + 1)
        x = random.randomint(0,size + 1)
        for dist in range(0, random.randomint(size/50, size/10)):
            heightarray[y[x]] = 9
            y += random.randomint(-1,2)
            x += random.randomint(-1,2)

    my_heightmap.write(heightarray)
    my_heightmap.close()
My apologies if this is a fairly simple fix (I didn't really find another similar question that made sense to me) and I'm probably not using the correct jargon. Thank you for any and all help!

P.S. I hope I've explained the situation enough. I am happy to post more code or explain further if needed!
Logged
This conversation is getting disturbing fast, disturbingly erotic.

bloop_bleep

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #10713 on: March 05, 2018, 06:24:31 pm »

Ah, I see. The problem with this line:
Code: [Select]
heightarray[y[x]] = heightlist[count + x]
is that y is an integer, and you're trying to use the [] operator on it, which is giving you the error. I think you want
Code: [Select]
heightarray[y][x] = heightlist[count + x]
This will first subscript heightarray with y, to get the row, and then subscript the row with x to get the desired entry.

EDIT: Actually, looking at it, even that won't work, because you're trying to assign to a nonexistent entry in heightarray (since you assign heightarray to the empty list before the loop.) Python doesn't know how to create new elements when assigning using = (unlike with dicts). To fix this, replace this line:
Code: [Select]
heightarray = []
with:
Code: [Select]
heightarray = [[None for i in range(self.size)] for j in range(self.size)]

This uses list comprehension to create an array of dimensions (self.size, self.size) that is filled out with None, preventing your code from throwing an error in the loop.
« Last Edit: March 05, 2018, 06:29:29 pm by bloop_bleep »
Logged
Quote from: KittyTac
The closest thing Bay12 has to a flamewar is an argument over philosophy that slowly transitioned to an argument about quantum mechanics.
Quote from: thefriendlyhacker
The trick is to only make predictions semi-seriously.  That way, I don't have a 98% failure rate. I have a 98% sarcasm rate.

Urist McScoopbeard

  • Bay Watcher
  • Damnit Scoopz!
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #10714 on: March 05, 2018, 06:26:56 pm »

Aaaah. Okay, a little syntactical mistake on my part. That makes more sense. Thank you.

EDIT: Thank you for that second part too!
« Last Edit: March 05, 2018, 06:30:46 pm by Urist McScoopbeard »
Logged
This conversation is getting disturbing fast, disturbingly erotic.

bloop_bleep

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #10715 on: March 05, 2018, 06:30:21 pm »

Make sure to check my edit as well, since you wrote your reply before I finished writing it, so you might have missed it.
Logged
Quote from: KittyTac
The closest thing Bay12 has to a flamewar is an argument over philosophy that slowly transitioned to an argument about quantum mechanics.
Quote from: thefriendlyhacker
The trick is to only make predictions semi-seriously.  That way, I don't have a 98% failure rate. I have a 98% sarcasm rate.

Urist McScoopbeard

  • Bay Watcher
  • Damnit Scoopz!
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #10716 on: March 05, 2018, 06:32:13 pm »

Make sure to check my edit as well, since you wrote your reply before I finished writing it, so you might have missed it.

Progress, but now it's giving me a list index out of range error.
Logged
This conversation is getting disturbing fast, disturbingly erotic.

bloop_bleep

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #10717 on: March 05, 2018, 06:33:25 pm »

Make sure to check my edit as well, since you wrote your reply before I finished writing it, so you might have missed it.

Progress, but now it's giving me a list index out of range error.
On which line is it throwing it?
Logged
Quote from: KittyTac
The closest thing Bay12 has to a flamewar is an argument over philosophy that slowly transitioned to an argument about quantum mechanics.
Quote from: thefriendlyhacker
The trick is to only make predictions semi-seriously.  That way, I don't have a 98% failure rate. I have a 98% sarcasm rate.

Urist McScoopbeard

  • Bay Watcher
  • Damnit Scoopz!
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #10718 on: March 05, 2018, 06:34:44 pm »

Code: [Select]
heightarray[y][x] = heightlist[count + x]The same one. It gives off the error both before and after changing how height array is initially defined.

EDIT: It's a problem with heightlist. I'll try and figure it out on my own.
« Last Edit: March 05, 2018, 06:37:11 pm by Urist McScoopbeard »
Logged
This conversation is getting disturbing fast, disturbingly erotic.

bloop_bleep

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #10719 on: March 05, 2018, 06:38:02 pm »

Hmm. Want to share the contents of the text file you're reading from? The error is most likely in the second part of the statement, not the first.
EDIT: Oops, didn't see your edit.
Logged
Quote from: KittyTac
The closest thing Bay12 has to a flamewar is an argument over philosophy that slowly transitioned to an argument about quantum mechanics.
Quote from: thefriendlyhacker
The trick is to only make predictions semi-seriously.  That way, I don't have a 98% failure rate. I have a 98% sarcasm rate.

Urist McScoopbeard

  • Bay Watcher
  • Damnit Scoopz!
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #10720 on: March 05, 2018, 06:46:40 pm »

Right now, it should look something like this:
Code: [Select]
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000/
n00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000/
n
Except there's 100 lines. (Eventually however many the user inputs via self.size)

At first, I didn't realize that I wasn't writing OVER the file every time I tested the map generator, but after resetting it, it still appears to be throwing the index error.

EDIT: Still confused. I THINK that something is going wrong in converting the file to a list. When you open() a file like so:
Code: [Select]
my_heightmap = open(os.path.expanduser("~/Desktop/pythongame/map/height.txt"), "w+")does it convert it to a string?
« Last Edit: March 05, 2018, 06:53:40 pm by Urist McScoopbeard »
Logged
This conversation is getting disturbing fast, disturbingly erotic.

bloop_bleep

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #10721 on: March 05, 2018, 06:53:56 pm »

Ah, there's the problem. See, list(my_heightmap) returns the list of lines, not the list of characters. Therefore, the index count+x is way out of bounds, since count+x is the index of the character, not the line. I think you want to use this:
Code: [Select]
heightarray[y][x] = heightlist[y][x]
instead, and just do away with the count variable.
Logged
Quote from: KittyTac
The closest thing Bay12 has to a flamewar is an argument over philosophy that slowly transitioned to an argument about quantum mechanics.
Quote from: thefriendlyhacker
The trick is to only make predictions semi-seriously.  That way, I don't have a 98% failure rate. I have a 98% sarcasm rate.

Urist McScoopbeard

  • Bay Watcher
  • Damnit Scoopz!
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #10722 on: March 05, 2018, 07:03:27 pm »

Ah, that is some good-to-know information about list(), lol!

After replacing the problematic code, it's STILL giving me a gosh darn list index out of range error.

It's still heightlist too, as setting heightarray equal to a constant like 0 or 1 does not throw an error.

FAKEEDIT: I cleaned up some code below what we're working through right now. It appears to work pretty smoothly otherwise.

EDIT: The code after some revision: EDIT2: Included the whole function for clarity.
Code: [Select]
def generate(self, filename):
        #flat map generation
        my_heightmap = open(os.path.expanduser("~/Desktop/pythongame/map/height.txt"), "w+")
        count = 0
       
        while(count <= self.size):
            count1 = 0
            while(count1 <= self.size):
                my_heightmap.write("0")
                count1 += 1
            my_heightmap.write("/n")
            count += 1
           
        #further functions go here
        #converting the heightmap into an array
        heightlist = list(my_heightmap)
        heightarray = [[None for i in range(self.size)] for j in range(self.size)]
        count = 0
       
        for y in range(self.size):
            for x in range(self.size):
                heightarray[y][x] = heightlist[y][x]
           
        #peak generation
        for peaks in range(0, round(self.size/30)):
            y = random.randint(0,self.size + 1)
            x = random.randint(0,self.size + 1)
            for dist in range(0, random.randint(self.size/50, self.size/10)):
                heightarray[y][x] = 9
                y += random.randint(-1,2)
                x += random.randint(-1,2)
       
        for y in range(self.size):
            for x in range(self.size):       
                my_heightmap.write(heightarray[y][x])

        my_heightmap.close()

EDIT: I think I may just be making things needlessly complicated. I think I've found a way to bypass my problems. I will return.

EDIT2: Since there's really no reason to write a flat map that's going to be iterated upon to a file anyways, I simply started by filling heightarray with 0's. After some fiddling with peak generation constants, I have a good start to a map generator.

EDIT3: Success!
Spoiler (click to show/hide)
« Last Edit: March 05, 2018, 08:51:49 pm by Urist McScoopbeard »
Logged
This conversation is getting disturbing fast, disturbingly erotic.

Parsely

  • Bay Watcher
    • View Profile
    • My games!
Re: if self.isCoder(): post() #Programming Thread
« Reply #10723 on: March 06, 2018, 03:43:44 pm »

So I'm trying to learn how to use MySQL so I can read this database to figure out what fields I need for a web app. I don't use command line much but it's easy enough to figure out how to get MySQL commands working there.

These tables are so massive though that it's not practical to browse using command line. Options would help if I knew what to look for. So I ask my teammate, our web programmer (I'm just an associate who helps wherever it's needed), if it would be easier to use a GUI like Workbench for this purpose, and his answer is something like "I bet you there's some guy at Google who makes millions of dollars who only uses command line". It was kind of a frustrating answer and I didn't really know how to respond.
« Last Edit: March 06, 2018, 04:09:16 pm by Parsely »
Logged

Telgin

  • Bay Watcher
  • Professional Programmer
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #10724 on: March 06, 2018, 04:51:36 pm »

That is indeed a pretty worthless response.

As a web programmer and someone who has used MySQL's command line interface, I'll gladly take Workbench if I have the option.  The CLI is something I use only if I have no other choice.  SQL is one of those things where you're supposed to think for a while about what you're doing before you do it, so even if the CLI might be faster for some people it's not much benefit compared to the headaches.

Not to mention Workbench has safeties in it to keep you from doing stupid things by accident, like running an update without a WHERE clause.  Sometimes, rarely, you want that, but not usually.
Logged
Through pain, I find wisdom.
Pages: 1 ... 713 714 [715] 716 717 ... 796