Bay 12 Games Forum

Please login or register.

Login with username, password and session length
Advanced search  
Pages: 1 ... 29 30 [31] 32 33 ... 796

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

Virex

  • Bay Watcher
  • Subjects interest attracted. Annalyses pending...
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #450 on: January 12, 2012, 07:38:06 pm »

Not if you can do it on two lines:

Anything math related like that I just steal from elsewhere.  I stole mine from the libtcod python tutorial:

Code: [Select]
def intersect(self, other):
        #returns true if this rectangle intersects with another one
        return (self.x1 <= other.x2 and self.x2 >= other.x1 and
                self.y1 <= other.y2 and self.y2 >= other.y1)
Logged

Nadaka

  • Bay Watcher
    • View Profile
    • http://www.nadaka.us
Re: if self.isCoder(): post() #Programming Thread
« Reply #451 on: January 12, 2012, 07:42:18 pm »

Not if you can do it on two lines:

Anything math related like that I just steal from elsewhere.  I stole mine from the libtcod python tutorial:

Code: [Select]
def intersect(self, other):
        #returns true if this rectangle intersects with another one
        return (self.x1 <= other.x2 and self.x2 >= other.x1 and
                self.y1 <= other.y2 and self.y2 >= other.y1)

except that doesn't actually work for all cases. The only case that works for is self being entirely inside other. All that code does is check the equivalent of:
other.containsPoint(self.topleft) AND other.containsPoint(self.bottomright)
« Last Edit: January 12, 2012, 07:52:51 pm by Nadaka »
Logged
Take me out to the black, tell them I ain't comin' back...
I don't care cause I'm still free, you can't take the sky from me...

I turned myself into a monster, to fight against the monsters of the world.

Levi

  • Bay Watcher
  • Is a fish.
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #452 on: January 12, 2012, 07:59:31 pm »

except that doesn't actually work for all cases. The only case that works for is self being entirely inside other. All that code does is check the equivalent of:
other.containsPoint(self.topleft) AND other.containsPoint(self.bottomright)

Seems to work alright without being inside each other:

Code: [Select]
Python 2.7.2 (default, Jun 12 2011, 15:08:59) [MSC v.1500 32 bit (Intel)] on win
32
Type "help", "copyright", "credits" or "license" for more information.
>>> def intersect(self, other):
...     return (self[0] <= other[2] and self[2] >= other[0] and
...             self[1] <= other[3] and self[3] >= other[1])
...
>>> intersect([0,0,10,10],[5,5,15,15])
True
>>>
Logged
Avid Gamer | Goldfish Enthusiast | Canadian | Professional Layabout

Max White

  • Bay Watcher
  • Still not hollowed!
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #453 on: January 12, 2012, 08:10:11 pm »

Does anybody know how to check if a character key is being held down in libtcod?
I know of
Code: [Select]
TCODConsole.isKeyPressed(TCODKeyCode.ENUM)However that doesn't differ between each different char. I also know of
Code: [Select]
TCODKey key = TCODConsole.checkForKeypress();But that only checks for when a key is pressed, not held down. Anybody know any solution?

Yes, given examples were c#, but anything from Python should hopefully do.

Levi

  • Bay Watcher
  • Is a fish.
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #454 on: January 12, 2012, 08:16:39 pm »

Does anybody know how to check if a character key is being held down in libtcod?
I know of
Code: [Select]
TCODConsole.isKeyPressed(TCODKeyCode.ENUM)However that doesn't differ between each different char. I also know of
Code: [Select]
TCODKey key = TCODConsole.checkForKeypress();But that only checks for when a key is pressed, not held down. Anybody know any solution?

Yes, given examples were c#, but anything from Python should hopefully do.

For the second one, I think the key object has a pressed attribute.  Maybe checking that will do it?
Logged
Avid Gamer | Goldfish Enthusiast | Canadian | Professional Layabout

Mego

  • Bay Watcher
  • [PREFSTRING:MADNESS]
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #455 on: January 12, 2012, 08:21:19 pm »

Maybe something like:

Code: [Select]
bool checked = false;
TCODKey key;
while(TCODConsole.isKeyPressed(TCODKeyCode.ENUM)) {
    if(!checked) {
        key = TCODConsole.checkForKeypress();
        checked = true;
    } else {
        if(key == TCODConsole.checkForKeyPress()) {
            <code>
        } else break;
    }
}

Stargrasper

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #456 on: January 12, 2012, 08:24:13 pm »

Presumably, the whole game exists in a large game loop.  Keep track of the key press (if any) from the previous tic and compare to what's pressed this tic.  Best I can think of given I've never used libtcod
Logged

Max White

  • Bay Watcher
  • Still not hollowed!
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #457 on: January 12, 2012, 08:25:15 pm »

No, it didn't work, and I might know why...

Each time I go over the main loop, it takes the input again, so for example if I press 'A' then it will take that char do do what ever with it. Then on the next iteration of the loop, while 'A' is still pressed down, it checks for new keys, gets none, then reassigns the variable that was 'A' to a nullObject (No, not null, I'm not getting null pointer exceptions, this is null pattern) and then continues on with that, so 'A' only got a chance to be checked once.
So I need to make sure something changed before I reassign... Ok.

Prepost Edit: Yea, that.

EDIT: Although I am right now running a level with 50,000,000 tiles at 210 fps on my crappy laptop. I think I have room for path finding.

Aqizzar

  • Bay Watcher
  • There is no 'U'.
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #458 on: January 12, 2012, 08:31:38 pm »

And all that being said, I'm writing a new MapTile class right now, which will save me loads of work and headache and duplication of effort in the future, and then I'll make a RoomObject class.  This shouldn't take more than a hour or two of grunt work.

Okay, the MapTile rewrite took an hour, but even that was with distractions.  It was definitely good to do it now, before I had to hunt through a billion lines of code for every related operation.  I've got a pretty good idea for how to implement Rooms as Objects...

Rooms have been implemented as Objects, but I've run into a bizarre problem.  I had to change some of the maths to build the rooms in a different space, and then plug them all back into the total map, and all of that seems to work perfectly fine.  The weird thing is the door-placement has gone all screwy, and I think I've seen this problem before.  I've got this system that after it finishes drawing the room square, it picks a random number of doors and a random list of sides, and then places an appropriate door on a random spot on that side.

The problem is, now that I've converted the Rooms to Objects, which are only referenced inside the map generator, every room has the same number of doors, placed on the same sides, the same distance from the corners, even though every Room is a different size and placement.  I can't see why this happening, because every time the map generator references the Make Room function, it makes a new Room Object.  But I saw this happen in Java once, when I wanted to fill an array with randomly-generated objects - even though the method would make a new object with every cycle, the array would still be filled with the last thing it generated.

That happens to be the unsolvable problem that turned me off from coding in the first place, all those years ago.  This is my White Whale.  Anyone have any experience with a problem like this?
« Last Edit: January 12, 2012, 08:34:42 pm by Aqizzar »
Logged
And here is where my beef pops up like a looming awkward boner.
Please amplify your relaxed states.
Quote from: PTTG??
The ancients built these quote pyramids to forever store vast quantities of rage.

Max White

  • Bay Watcher
  • Still not hollowed!
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #459 on: January 12, 2012, 08:40:32 pm »

You know, it is funny... I have never had the problem you are describing, but I have an idea as to what it might be. Otherwise I will need to see some code.

You are using the random number generator that comes with c#, correct? You will notice that a seed is optional. If you don't use a seed, it will take the current time.
This means that if you make several new random number generators at the same time, they will all produce the same number. Use the same number for the same thing, and get the same outcome.
The solution is to do it all with a single random number generator, that way is twists the last number it produced, and you get something new.

Aqizzar

  • Bay Watcher
  • There is no 'U'.
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #460 on: January 12, 2012, 08:45:48 pm »

You are using the random number generator that comes with c#, correct? You will notice that a seed is optional. If you don't use a seed, it will take the current time.
This means that if you make several new random number generators at the same time, they will all produce the same number. Use the same number for the same thing, and get the same outcome.
The solution is to do it all with a single random number generator, that way is twists the last number it produced, and you get something new.

...I'm not sure that is the problem.  The generators themselves are established in each overall class (with the same name for simplicity), so they can be referenced from anywhere.  Can you show me an implementation of what you're describing?

Code: (This is the door generating code.) [Select]
int doors = random.Next(1,5);
byte[] sides = {1,2,3,4};

for (int i = 3; i >= 0; i--)
{
int n = random.Next(i);
byte move = sides[i];
sides[i] = sides[n];
sides[n] = move;
}

for (int i = doors; i > 0; i--)//count down thru doors
{
switch(sides[(i - 1)])
{
case 1://right
roomOut[(xSize - 1), (random.Next(1, (ySize - 2)))] = new MapTile(201);
break;
case 2://top
roomOut[(random.Next(1, (xSize - 2))), 0] = new MapTile(200);
break;
case 3://left
roomOut[0, (random.Next(1, (ySize - 2)))] = new MapTile(201);
break;
default://bottom
roomOut[(random.Next(1, (xSize - 2))), (ySize - 1)] = new MapTile(200);
break;
}
}

The stupid thing is, it's randomly generating a new Room with a uniquely random size every cycle, but putting the same number of doors in the same places on each one... except even then, it's still somehow catching it so that it never tries to make a door beyond the established boundaries of each Room, they just wind up in very similar distances from the corners.  I'm positively baffled by this.
« Last Edit: January 12, 2012, 08:50:00 pm by Aqizzar »
Logged
And here is where my beef pops up like a looming awkward boner.
Please amplify your relaxed states.
Quote from: PTTG??
The ancients built these quote pyramids to forever store vast quantities of rage.

Max White

  • Bay Watcher
  • Still not hollowed!
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #461 on: January 12, 2012, 09:07:36 pm »

Umm, that is odd to say the least... I havn't the foggiest why that is happening.

You could try using this, but the problem is that then you don't really learn anything, but you can always redo stuff once you have a working prototype.
Spoiler (click to show/hide)
If you add that to your Room class, then calling that method will add a single door to a random side at a random spot. You will still need a loop for the number of doors inside another for each room, but you get the idea.

Sometimes when it comes to algorithms you find yourself out of a limb.

Levi

  • Bay Watcher
  • Is a fish.
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #462 on: January 12, 2012, 09:08:09 pm »

The stupid thing is, it's randomly generating a new Room with a uniquely random size every cycle, but putting the same number of doors in the same places on each one... except even then, it's still somehow catching it so that it never tries to make a door beyond the established boundaries of each Room, they just wind up in very similar distances from the corners.  I'm positively baffled by this.

It really sounds like its the seed that is giving you issues.  Do you have one Random object per room?  If so, try making one Random object before you create the rooms, and then assign the random object to each room, instead of making many of them. 
Logged
Avid Gamer | Goldfish Enthusiast | Canadian | Professional Layabout

Aqizzar

  • Bay Watcher
  • There is no 'U'.
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #463 on: January 12, 2012, 09:48:25 pm »

Okay, this is seriously starting to piss me off.  I've tried moving everything around, I've tried initializing and reintializing Random generators all over the place, giving them seeds, giving them changing seeds, reordering the process, and everything I do creates random rooms with identical doors.

I don't suppose there's anyone game for taking a look at the whole code?  It's still essentially barebones, since I'm implementing everything as I go, so it's not stupendously hard to find where the methods are, and I have everything commented on.  I certainly don't expect anyone to step up and do my work for me, but I am completely out of ideas here, and without showing someone what I'm doing so they can show me what they would do, I don't know how else to ask for help with this.
Logged
And here is where my beef pops up like a looming awkward boner.
Please amplify your relaxed states.
Quote from: PTTG??
The ancients built these quote pyramids to forever store vast quantities of rage.

Mego

  • Bay Watcher
  • [PREFSTRING:MADNESS]
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #464 on: January 12, 2012, 09:51:00 pm »

I'm in. Upload it to dropbox and shoot me a link?
Pages: 1 ... 29 30 [31] 32 33 ... 796