Bay 12 Games Forum

Please login or register.

Login with username, password and session length
Advanced search  
Pages: [1] 2 3

Author Topic: Faffing about with a roguelike  (Read 4598 times)

Simmura McCrea

  • Bay Watcher
    • View Profile
    • My Steam profile
Faffing about with a roguelike
« on: February 16, 2012, 09:26:26 pm »

Yay, more roguelikes. This is unlikely to go very far, since it's mostly practice for me, with C++ as well as using Git. This thread's likely to mostly be me asking for help, especially with libtcod.

Currently I've managed to get it to print a string a move it around the window. However, it's 2AM, so I'm going to bed. I'll work more on this tomorrow, but I figured I'd make a thread for the inevitable points where I have no idea what the hell I'm doing.
Logged

MaximumZero

  • Bay Watcher
  • Stare into the abyss.
    • View Profile
Re: Faffing about with a roguelike
« Reply #1 on: February 17, 2012, 03:43:43 am »

Yay, I like faffing!

...wait...
Logged
  
Holy crap, why did I not start watching One Punch Man earlier? This is the best thing.
probably figured an autobiography wouldn't be interesting

Svarte Troner

  • Bay Watcher
    • View Profile
Re: Faffing about with a roguelike
« Reply #3 on: February 17, 2012, 10:14:09 am »

Yay, I like faffing!

...wait...

I must say, we bay12ers sure can come up with some interesting words.

Oh, darn you brits and your almost sexually suggestive slang words!
Logged
That metal guy that pops up sometimes in places
To put it simply, Dwarf Fortress is the Black Metal of video games.

Simmura McCrea

  • Bay Watcher
    • View Profile
    • My Steam profile
Re: Faffing about with a roguelike
« Reply #4 on: February 17, 2012, 10:34:07 am »

Get your minds out of the gutter. >_> It's a perfectly innocent word. Just means messing about or suchlike.

EDIT: Yay, got a problem now. Following through the python roguelike tutorial and translating it to C++, I find that they've got a function in their class for drawing the object. My attempt at this swears that I haven't initialised the console I'm trying to draw it to. I haven't, since the class is above main(), but either they've somehow avoided the problem or Python is okay with stuff like that.
Code: (Python) [Select]
def draw(self):
        #set the color and then draw the character that represents this object at its position
        libtcod.console_set_foreground_color(con, self.color)
        libtcod.console_put_char(con, self.x, self.y, self.char, libtcod.BKGND_NONE)
Code: (C++) [Select]
void Draw()
{
con->setForegroundColor(colour);
con->printLeft(x, y, TCOD_BKGND_NONE, symbol);
}
As close as I can tell, they're identical.

MOREEDIT: Nevermind, got it working. Needed to pass con in.
« Last Edit: February 17, 2012, 12:56:13 pm by Simmura McCrea »
Logged

Simmura McCrea

  • Bay Watcher
    • View Profile
    • My Steam profile
Re: Faffing about with a roguelike
« Reply #5 on: February 17, 2012, 01:22:29 pm »

I'm trying to get a for loop that goes through all the objects of a certain type I have and calls a function from each. I tried using a list, but it seems that instead of storing the name of the object, it stores the attributes of it instead. Anyone know how this is done?
Logged

Jack_Bread

  • Bay Watcher
  • 100% FRESH ♥HIPPO♥
    • View Profile
Re: Faffing about with a roguelike
« Reply #6 on: February 17, 2012, 06:08:46 pm »

I'm trying to get a for loop that goes through all the objects of a certain type I have and calls a function from each. I tried using a list, but it seems that instead of storing the name of the object, it stores the attributes of it instead. Anyone know how this is done?
I think you can use TCODLists to do that.
Code: [Select]
TCODList<myClass *> myList;
myClass something;
myList.push(&something);

for(myClass **it = myList.begin(); it!= myList.end(); it++)
      (*it)->function();

Simmura McCrea

  • Bay Watcher
    • View Profile
    • My Steam profile
Re: Faffing about with a roguelike
« Reply #7 on: February 17, 2012, 06:35:11 pm »

I'm trying to get a for loop that goes through all the objects of a certain type I have and calls a function from each. I tried using a list, but it seems that instead of storing the name of the object, it stores the attributes of it instead. Anyone know how this is done?
I think you can use TCODLists to do that.
Code: [Select]
TCODList<myClass *> myList;
myClass something;
myList.push(&something);

for(myClass **it = myList.begin(); it!= myList.end(); it++)
      (*it)->function();
Yeah, that worked good. Thankee.

EDIT: Apparently it doesn't work with a list of a list. Frigging python and lack of C++ tutorials on this crap.
« Last Edit: February 17, 2012, 07:20:51 pm by Simmura McCrea »
Logged

Jack_Bread

  • Bay Watcher
  • 100% FRESH ♥HIPPO♥
    • View Profile
Re: Faffing about with a roguelike
« Reply #8 on: February 18, 2012, 01:49:54 am »

-quote snip-
I think you can use TCODLists to do that.
Code: [Select]
TCODList<myClass *> myList;
myClass something;
myList.push(&something);

for(myClass **it = myList.begin(); it!= myList.end(); it++)
      (*it)->function();
Yeah, that worked good. Thankee.

EDIT: Apparently it doesn't work with a list of a list. Frigging python and lack of C++ tutorials on this crap.
List of a list? What do you need that for?
Also, from what I understand, I've been doing the same thing you're doing. :0 I'm on part 7!

Max White

  • Bay Watcher
  • Still not hollowed!
    • View Profile
Re: Faffing about with a roguelike
« Reply #9 on: February 18, 2012, 01:53:33 am »

2d list, most likely to store the tiles that make up a level.

Simmura McCrea

  • Bay Watcher
    • View Profile
    • My Steam profile
Re: Faffing about with a roguelike
« Reply #10 on: March 01, 2012, 03:53:45 pm »

Working on the dungeon generator code. However, I only ever get one room. Seems to be while comparing a newly created room to all the rooms in the list of rooms to make sure they don't intersect, it's comparing the only room to itself. I have no idea how to fix this. Full function below.

Code: [Select]
void MakeMap(TCODList<rect*> rooms, character &player)
{
for (int i = 0;i<MAP_WIDTH;i++){
for (int j = 0;j<MAP_HEIGHT;j++)
map[i][j]=tile(true,true);
}
int num_rooms = 0;
int w,h,x,y,new_x,new_y,prev_x,prev_y,hold;
bool failed = false;
for(int i = 0;i<MAX_ROOMS;i++){
w = default->getInt(ROOM_MIN_SIZE,ROOM_MAX_SIZE);
h = default->getInt(ROOM_MIN_SIZE,ROOM_MAX_SIZE);
x = default->getInt(0,MAP_WIDTH-w-1);
y = default->getInt(0,MAP_HEIGHT-h-1);
rect new_room(x,y,w,h);
if (num_rooms!=0){
for(rect **it = rooms.begin(); it != rooms.end(); it++){
if(new_room.Intersect(**it)){
failed = true;
break;
}
}
}
if(failed == false){
CreateRoom(new_room);
new_x=new_room.centre_x;
new_y=new_room.centre_y;
if(num_rooms==0){
player.x=new_x;
player.y=new_y;
}
else{
rect hold = *rooms.peek();
prev_x = hold.centre_x;
prev_y = hold.centre_y;
if(default->getInt(0,1)==1){
CreateHTunnel(prev_x,new_x,prev_y);
CreateVTunnel(prev_y,new_y,new_x);
}
else{
CreateVTunnel(prev_y,new_y,new_x);
CreateHTunnel(prev_x,new_x,prev_y);
}
}
rooms.push(&new_room);
num_rooms++;
}
}
}
Logged

Levi

  • Bay Watcher
  • Is a fish.
    • View Profile
Re: Faffing about with a roguelike
« Reply #11 on: March 01, 2012, 04:40:37 pm »

Edit:  Misread the question.  Thinking more.

Edit 2:  My c++ is too rusty to help much apparently.

My first inclination is to make new_room a pointer and create it with new each time, but that is more because I don't 100% remember the behavior of references used that way.  I'm probably very wrong about this, but I have a bad feeling that as soon as your for-loop iterates and rect new_room(x,y,w,h) is called, its being written over the old one in memory.  This MIGHT be what is causing your weird compare against itself problem(and also why I used to despise passing things by reference).

Another thing which might help a little is that I'd change your for(int i = 0;i<MAX_ROOMS;i++) into a while loop instead.  Currently whenever a room is failed, you'll still be incrementing up your i until you reach max_rooms.  If you get a little unlucky with the random number generator you could end up with just 1 room.

So I'd use a while loop like:

Code: [Select]
while(num_rooms < MAX_ROOMS)

Note that it probably won't resolve your current problem and it'll likely freeze as it'll never reach MAX_ROOMS until the other problem is fixed.
« Last Edit: March 01, 2012, 05:56:45 pm by Levi »
Logged
Avid Gamer | Goldfish Enthusiast | Canadian | Professional Layabout

RulerOfNothing

  • Bay Watcher
    • View Profile
Re: Faffing about with a roguelike
« Reply #12 on: March 01, 2012, 08:08:55 pm »

It can't be comparing the room to itself because you add the room after you check for intersections with other rooms. I notice that you don't reset failed to false after you create another room, so if failed gets set to true you won't add anymore rooms. By the way, how does rect::Intersect work exactly?
Logged

Jack_Bread

  • Bay Watcher
  • 100% FRESH ♥HIPPO♥
    • View Profile
Re: Faffing about with a roguelike
« Reply #13 on: March 02, 2012, 02:23:07 am »

Working on the dungeon generator code. However, I only ever get one room. Seems to be while comparing a newly created room to all the rooms in the list of rooms to make sure they don't intersect, it's comparing the only room to itself. I have no idea how to fix this. Full function below.

Code: [Select]
-snip-
I think the problem is what Levi said.
Try adding
Code: [Select]
rect *new_room;near the beginning and have
Code: [Select]
new_room = new rect(x,y,w,h);instead of "rect new_room(x,y,w,h);"

Simmura McCrea

  • Bay Watcher
    • View Profile
    • My Steam profile
Re: Faffing about with a roguelike
« Reply #14 on: March 02, 2012, 07:11:45 am »

I think the problem is what Levi said.
Try adding
Code: [Select]
rect *new_room;near the beginning and have
Code: [Select]
new_room = new rect(x,y,w,h);instead of "rect new_room(x,y,w,h);"
Unfortunately, that breaks the entire rest of the function. new_room suddenly isn't a class, so I can't call the Intersect function, get the centre_x or centre_y values or push it onto the rooms list.

It can't be comparing the room to itself because you add the room after you check for intersections with other rooms. I notice that you don't reset failed to false after you create another room, so if failed gets set to true you won't add anymore rooms. By the way, how does rect::Intersect work exactly?
It is, I checked the memory values with the debugger.
Code: (rect::Intersect()) [Select]
bool Intersect(rect other)
{
if(x1 <= other.x2 && x2 >= other.x1 && y1 <= other.y2 && y2 >= other.y1)
return 1;
else
return 0;
}
x1,y1 being the top left corner and x2,y2 being the bottom right.

Another thing which might help a little is that I'd change your for(int i = 0;i<MAX_ROOMS;i++) into a while loop instead.  Currently whenever a room is failed, you'll still be incrementing up your i until you reach max_rooms.  If you get a little unlucky with the random number generator you could end up with just 1 room.

So I'd use a while loop like:

Code: [Select]
while(num_rooms < MAX_ROOMS)

Note that it probably won't resolve your current problem and it'll likely freeze as it'll never reach MAX_ROOMS until the other problem is fixed.
That's okay, less rooms than the maximum is allowed. If it really becomes a problem I might add a MIN_ROOMS const and while loop around that all until num_rooms > MIN_ROOMS.

EDIT: Okay, a bit of fiddling around with Jack's solution, and it sort of works. I now get a bunch of rooms and corridors, although they don't always match up. I'll see what I can do with that, but it's a hell of a lot closer.
« Last Edit: March 02, 2012, 07:25:14 am by Simmura McCrea »
Logged
Pages: [1] 2 3