Bay 12 Games Forum

Please login or register.

Login with username, password and session length
Advanced search  

Author Topic: My First C++ Game - Asking for advice and guidance!  (Read 2181 times)

Jetlaw

  • Bay Watcher
    • View Profile
My First C++ Game - Asking for advice and guidance!
« on: October 24, 2009, 04:35:32 pm »

Yo guys! With a bit of plucky help on pointers from some lovely forum denziens I finally stumbled my way rather unelegantly through a few barriers.

The result of my labours is perhaps the crappiest game ever made still in ultra-super-mega-alpha stage (but I guess all games have to start somewhere).

The game's working name is 2D 'Em Up, because I havn't decided on a final name for the game yet. Programmed in C++, it's a console game.

The (current) story is non-existant and may never exist depending on how the game develops.

The (current) incarnation of the game is very, very, very crude. There's only one map, there's only 11 monsters, they don't fight back and all you can do is walk into them. Three times. And then they die. When they're all dead, nothing happens - The game doesn't give you a victory fanfare, you just walk around until you decide to quit.

The in-game help doesn't exist as I was too lazy to program it in just now.

But I'm releasing it and it's source code to ask for advice and guidance for the forum's veterans. At the moment I'd just like people to read through my source and comment; are the functions laid out in an efficient manner, does the current source leave much scope for expansion, how would you do things better?

Controls:

Up Arrow/Left Arrow/Down Arrow/Right Arrow - Move
Attack/kill by running into the 'm's 3 times
Quit with F1

Project Aims:

2D 'Em Up is aiming to be a futuristic rougelike shooter. The core gameplay mechanic is going to revolve around the use of ranged weaponry primarily, while fighting against hordes of super-fast melee creatures that will close down range on the PC very quickly. The advantage of having a ranged weapon will be countered by the NPC speed where an NPC can rapidly close ground on the PC meaning multiple enemies at once will pose a significant challenge while single enemies will provide much less.

The unique selling point of the game is the planned weapon system; I intend for each weapon to be deconstructable into a number of simplified component parts which can then be rebuilt. A PC could deconstruct a sniper rifle with a clip capacity of 5 bullets to gain a number of rifle components and a scope (which is misc. and can be fitted to all weaponry) - They could then fit the scope to a pistol or deconstruct an assault rifle and hybrid together a burst-fire sniper rifle or a sniper rifle with a 30 bullet clip capacity. Individual components will have modifiers and enchantments and eventually wear (so you will need to deconstruct and reconstruct weapons in order to be combat effective).

I intend to have a number of dynamically generated maps, although I do not know how to accomplish this just yet. The types of maps will vary from standard "Room with 1 tile coridoors" roguelike style to large, open outdoor levels to a hybrid of open indoor levels with interesting rooms to explore.

Combat will eventually contain a number of features including enemies having the capacity to dodge and range effecting accuracy; large and cumbersome weapons would be effective at long ranges but would be far too unwieldy to fire accurately in melee. On the other hand pistols aren't exactly the most powerful weapons with the longest ranges in the world, but attaching a scope can increase the effective range before accuracy drops off and they're much more handier in a close-quarters fight. Then you've got things like shotguns which are deadly at point blank but lose power at longer ranges.

The objective of the game is to come with the story, but placeholders such as "Rescue the Princess!" "Bring Me a Needle!" or "Placate the Bacon Elementals" can be used in lieu of story until development (and my programming skills) are more advanced.

Link to the mediafire hosted source and .exe
« Last Edit: October 27, 2009, 03:05:22 am by Jetlaw »
Logged

timmeh

  • Bay Watcher
    • View Profile
    • My Portfolio
Re: My First C++ Game - Asking for advice and guidance!
« Reply #1 on: October 24, 2009, 05:27:27 pm »

I'm far from counting as a veteran, but here's my two cents anyways.

One pretty easy thing to do, make the "Help" option just not do anything, instead of quiting.  Once you do write the help, the easiest way would probably be to keep it in a string somewhere easy to find, so you can modify it later.

Another pretty quick change I'd make is allowing the numpad to be used.  This isn't too hard since you're using some form of curses, you should be able to just check for the key code for the numbers, instead of the arrow keys.  You may be able to do this by checking getch() against something like '8' for up, but I'm not 100%.  If that doesn't work, you can find a good chart of the keys and their corresponding numbers here.

Time is short, and I have to eat, but as far as dungeon generation goes, there are some interesting articles on the subject here and here.

The idea is great though, I'll be watching this!
Logged
On the Wall is a Masterfully engraved carving of Urist McHardcastle and Goblins. Urist McHardcastle is surrounded by the Goblins. The Golbins are stamping on Urist McHardcastle. Urist McHardcaste is laughing at the Goblins. The carving related to the prolonged and bloody death of Urist McHardcastle in the Fall of 1659, the Winter of 1659, and the Spring of 1660. On the engraving is an image of Cheese.

Outcast Orange

  • Bay Watcher
  • [SOMETIMES_SQUID]
    • View Profile
    • The Outcast Orange
Re: My First C++ Game - Asking for advice and guidance!
« Reply #2 on: October 24, 2009, 06:37:30 pm »

Anything dynamic is more fun to program and yields greater results.
Replay value increases by a ton when the game is different every time.
The more things that are different every time, the more interested someone will be in starting a new game again.
That is why rogue-likes are so damn addictive.
You never know what you're going to get.

The methods timmeh suggests are good, but I'd try writing my own system for extra fun on the coding side.
You can then compare against other methods and try different things.

I look forward to seeing what you do with this.
Logged
[7:53:55 PM] Armok, why did you demand that I don't eat you?
[7:54:34 PM] [Armok]: woooooo

Burried Houses - Platform Explorer Demo H - Cloud Scream

Jetlaw

  • Bay Watcher
    • View Profile
Re: My First C++ Game - Asking for advice and guidance!
« Reply #3 on: October 25, 2009, 04:45:38 pm »

Having fun and difficulty tackling the random dungeon generator.

My map theoretical boundry is 78 * 21 and realistic boundry is 76 * 19 (due to walls at the edges being impossible to dig).

At the moment I've split the map into a grid of 13 * 3 with 6 * 7 peices.

I then run through for each grid section: rand()%2, output 1 or 0. If 1 a room of size 3-6 * 3-7 is built in that grid section and a value is stored in a array[13][3] of 1 or 0 to indicate if that grid section has a room component or not.

I'm having trouble figuring out how to hook these rooms together though.

I think I'll have to create a class containing the coordinate of the grid and the size of the room within the grid as well as the top left and bottom right coordinates of the room within the grid, the absolute coordinates on the console, for any roomless grids how many connections are running through it and if it is currently connected to any existing room before I have enough information to allow me to actually be able to connect the rooms reliably.

Then I might be able to create something that iterates through all the rooms and those that aren't connected to any other rooms yet can be set as the highest priority candidates for new connections. I'm then planning to create a room connecting system that favours grids without rooms for connections meaning as much of the map area is used as possible. I'm thinking of doing a rand()%4+1 which will run a 1-4 and apply this to the *maximum* number of connections a room should have. I must also allow this maximum to be overridden incase a room has no connections and all rooms near it have their maximum number of connections already. This is important if a room in the bottom right corner of my map is surrounded by rooms with connection limits of max 1.

My room creation algorithm also doesn't leave room for walls between every room, so there will be certain rooms that are L shaped if a 3*7 room is created above a 7*3 room.

The problem of the outside of the room potentially not having walls is fixed by a Fort Knox function that then places wall tiles on every outer edge tile of my map after room generation is completed.

But it's taking awhile to wrap my head around it.

[Edit] I think I can also modify this to generate a dungon floor without any walls and then generate rooms. This would fulfill my open level requirement too and would be far, far easier to implement as I don't need to worry about connecting rooms.
« Last Edit: October 25, 2009, 04:49:47 pm by Jetlaw »
Logged

Jetlaw

  • Bay Watcher
    • View Profile
Re: My First C++ Game - Asking for advice and guidance!
« Reply #4 on: October 25, 2009, 07:59:29 pm »

The current snippet of code on my map generation project is far, far, FAR prettier using classes and is about a thousand times easier to handle.

Code: [Select]
char charmapMap[78][21] = {'\0'};
class clmapMapGrid
{
   public:
      int nGridY;
      int nGridX;
      int nGridCoordY;
      int nGridCoordX;
      int nGridRoomY;
      int nGridRoomX;
      int nGridRoomCoordY;
      int nGridRoomCoordX;
     
      bool nGridRoom;
};

clmapMapGrid clmapMap[39];
clmapMapGrid* pclmapMap;
int nmapMapCellH = 7;
int nmapMapCellW = 6;

int main(int argc, char *argv[])
{
   //Fill the array with "1" characters, which are walls.
   for(int i = 0; i < 78; i++)
   {
      for(int x = 0; x < 21; x++)
      {
         charmapMap[i][x] = '1';
      }
   }
   
   //Populate all 39 instances of the class.
   for(int a = 0; a < 3; a++)
   {
      for(int b = 0; b < 13; b++)
      {
         pclmapMap = &clmapMap[(a*13)+b];
         pclmapMap->nGridX = a;
         pclmapMap->nGridY = b;
         pclmapMap->nGridCoordX = (a*nmapMapCellH);
         pclmapMap->nGridCoordY = (b*nmapMapCellW);
         for(int c = rand()%2;;)
         {
            if(c == 1)
            {
               pclmapMap->nGridRoom = true;
            }
            else
            {
               pclmapMap->nGridRoom = false;
            }
            break;
         }
         if(pclmapMap->nGridRoom == true)
         {
            pclmapMap->nGridRoomX = rand()%3+4;
            pclmapMap->nGridRoomY = rand()%3+3;
            pclmapMap->nGridRoomCoordX = ((pclmapMap->nGridCoordX) + (rand()%(nmapMapCellH - pclmapMap->nGridRoomX)));
            pclmapMap->nGridRoomCoordY = ((pclmapMap->nGridCoordY) + (rand()%(nmapMapCellW - pclmapMap->nGridRoomY)));
         }
      }
   }
   
   // Write the rooms to the array. 2 is "generic floor".
   for(int d = 0; d < 39; d++)
   {
      pclmapMap = &clmapMap[d];
      if(pclmapMap->nGridRoom == true)
      {
         for(int b = pclmapMap->nGridRoomX; b > 0; b--)
         {
            for(int c = pclmapMap->nGridRoomY; c > 0; c--)
            {
               charmapMap[pclmapMap->nGridRoomCoordY+c][pclmapMap->nGridRoomCoordX+b] = '2';
            }
         }
      }
   }
   
   // "Fort Knox" the map, building walls around the entire perimeter.
   for(int iw = 0; iw < nmapMapW; iw++)
   {
      charmapMap[iw][0] = '1';
      charmapMap[iw][nmapMapH-1] = '1';
   }
   for(int ih = 0; ih < nmapMapH; ih++)
   {
      for(int iw = 0; iw == 0; iw++)
      {
         charmapMap[iw][ih] = '1';
      }
      for(int iw = (nmapMapW-1); iw == (nmapMapW-1); iw++)
      {
         charmapMap[iw][ih] = '1';
      }
   }

   /* INSERT YOUR OWN "PRINT CHAR ARRAY TO SCREEN" CODE HERE */
   system("PAUSE");
   return EXIT_SUCCESS;
}

Note this code may need touching up a little as it's just me snipping the important parts of my test project into [.code] blocks. I'm using my own custom function to print the character array to screen with Curses.

From here I can I'm going to attempt to create some way to link the rooms together.
Logged

Alexhans

  • Bay Watcher
  • This is toodamn shortto write something meaningful
    • View Profile
    • Osteopatia y Neurotonia
Re: My First C++ Game - Asking for advice and guidance!
« Reply #5 on: October 26, 2009, 08:24:28 am »

Posting here to check it out later.  You'll definetly have my support even though I'm no veteran either.

Megaupload sucks :( ... Try to upload it to mediafire, 4shared or some other that will allow multiple downloads, please...

I just skimmed the map generation snippet so I can't offer much help:

try to avoid using "magic numbers" throughout the code or you'll be sorry later when you need to change something.  Changing one constant value is less trouble than searching through 500 lines of code to see where that 3 must be changed to a 4.

lol.  It's nice to see some hungarian notation around.  :P

Why do you use charmap and clmap, though?  What do they mean?  usually, a class instance will can be defined with a 'c' prefix and a c-string with 'ch'.   mmm, I guess 'cl' is your prefix for class... and 'p' goes for pointer so pcl is pointer to class.  Ok.  I think I've got it now.

Good luck.
Logged
“Eight years was awesome and I was famous and I was powerful" - George W. Bush.

Jetlaw

  • Bay Watcher
    • View Profile
Re: My First C++ Game - Asking for advice and guidance!
« Reply #6 on: October 26, 2009, 10:38:04 am »

I use "map" on character arrays/string arrays that I print to the screen as part of the UI, in the full source code there's a reference.h which contains only // /**/ comments with lists of my prefixes. I'll also upload it to one of your suggested sites when I've finished bjorking around with the dungeon generator. However long that takes.
Logged

Jetlaw

  • Bay Watcher
    • View Profile
Re: My First C++ Game - Asking for advice and guidance!
« Reply #7 on: October 27, 2009, 03:04:54 am »

I'm mostly face slamming right now. The only method I can get to work with any consistency is more nested if, for and while statements than you can shake a stick at. Plus it's not very efficient. Plus the code is a freakin' nightmare to actually view and work out.

At the base I've got two nested

Code: [Select]
for(int a = 0; a < 39; a++)
{
   pclmapMap = &clmapMap[a];
   
   for(int b = 0; b < 39; b++)
   {
      pclmapMap2 = &clmapMap[b];
      ...
      ...
      ...
   }
}

for statements. So it points to an array using pclmapMap and then cycles through every array with pclmapMap2 comparing every single array to the initial one.

Then it digs deeper and there's if statements for rooms having existing connections, if statements that check the room orientation (if a room is significantly lower or higher then horizontal paths connecting rooms also need vertical distances too) and I REALLY don't want a spider's web of code to have to trawl through or even write - Afterall, these are supposed to be routines that I'm going to be implementing into my main source eventually.

Does anyone have any links that'll help me with putting paths between my rooms in a reliable way? I mean I CAN link them together, but the end result is not pretty, there are still inaccessible rooms as the nested statements are so twisted that if I ever wanted to change, update or adapt it (to create different types of levels than underground dungeons, for example) I'd have to start from scratch.

On a different note, first source is now hosted with mediafire
Logged

Outcast Orange

  • Bay Watcher
  • [SOMETIMES_SQUID]
    • View Profile
    • The Outcast Orange
Re: My First C++ Game - Asking for advice and guidance!
« Reply #8 on: October 27, 2009, 08:23:36 am »

I have a decent plan for connections.
The problem will be traversing vertical passages.
Ladders I'm guessing.

Just make a spoke all the way over in the x direction,
then one that goes in the y directions,
then one that goes in the z direction.
Make a system for adding nodes to them when they hit other rooms,
so they can break off and try to go around.
In that case you will have to get creative.
But you should end up with a bunch of twisty confusing tunnels.
That would be somewhat interesting.
And you can randomize which coord they run through first, second, and last.
Logged
[7:53:55 PM] Armok, why did you demand that I don't eat you?
[7:54:34 PM] [Armok]: woooooo

Burried Houses - Platform Explorer Demo H - Cloud Scream

jplur

  • Bay Watcher
    • View Profile
    • http://www.parker-portfolio.com
Re: My First C++ Game - Asking for advice and guidance!
« Reply #9 on: October 27, 2009, 03:47:13 pm »

I'm a fan of this algorithm:

http://roguebasin.roguelikedevelopment.org/index.php?title=Basic_BSP_Dungeon_generation

It makes a beautiful dungeon, but the hallways are very short and never turn.
Logged
I ended up finding out you can speed up pregnancies by changing the mother to a cat to knock them up and changing back before labor

eerr

  • Bay Watcher
    • View Profile
Re: My First C++ Game - Asking for advice and guidance!
« Reply #10 on: October 28, 2009, 11:09:59 pm »

will you have bats or crowbars?
Logged