Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Best way for organizing turn-based game  (Read 7023 times)

0 Members and 1 Guest are viewing this topic.

MrOnlineCoder

  • Jr. Member
  • **
  • Posts: 87
    • View Profile
Best way for organizing turn-based game
« on: August 11, 2016, 02:51:18 pm »
Hello. I am making a game where player control a bunch of heroes on a field (15*7 squares), and player has to destory enemy's heroes. It will be turn-based game, so hero can walk/attack in 1 turn. What is the best way for designing this game, so heroes will be easily interacted with UI (choose a hero to attack, choose a field square to go) and with in-game logic.
Thanks in advance.  ;)

Ziburinis

  • Newbie
  • *
  • Posts: 15
    • View Profile
Re: Best way for organizing turn-based game
« Reply #1 on: August 11, 2016, 10:06:26 pm »
That's a big question! I'm something of a newbie myself, but here's the approach I would take:

1. Find the right tools for the job. You know you'll be drawing 105 sprites, and probably a window with a combo box to choose attacks, a unit description, and hp bar. So make a quick prototype that draws some lorem ipsum content. I'm in the process of choosing a GUI library myself, and I'm currently leaning towards Crazy Eddie's, which is very popular and has tutorials. There are also SFML tutorials for drawing a grid of tiles.

2. Model your data as if there was no user interface at all. So you might have:
Code: [Select]
struct Tile {
  unsigned int sprite;
  bool is_passable;
};

struct Map {
  Tile tiles[size_x][size_y];
};

Class Player {
  vector<Hero> heroes;
  void doStatusEffects();
};
3. Tie the underlying data and the interface together. This will likely involve following examples for the GUI you pick. You may want to have a "Game" class, with a gamestate member that lets the flow of the game be altered to various menus, world maps, etc.

There is no substitute for just trying and getting frustrated when it comes to software architecture. You just have to try to think ahead, and refactor when something doesn't work. But here are some tips:
- Don't use inheritance just for the sake of it. Especially in C++, it creates more headaches than composition, especially if you aren't very experienced with the syntax.
- Code reuse is very important. Start with small functions and structures, use them to make bigger functions and structures.
- Have a good attitudes towards figuring out things you don't understand. It's better to be eager to learn, and be patient with getting your project done, than to rush and get angry at the computer for not understanding you.
« Last Edit: August 11, 2016, 10:13:27 pm by Ziburinis »

MrOnlineCoder

  • Jr. Member
  • **
  • Posts: 87
    • View Profile
Re: Best way for organizing turn-based game
« Reply #2 on: August 11, 2016, 10:43:51 pm »
That's a big question! I'm something of a newbie myself, but here's the approach I would take:

..........

Thanks for reply! Can you tell me cons in using inheritance (I already used inheritance in Game States (screens)) and alternative for it. Also, that the game will be multiplayer too, so in what way I should store data of ~50 heroes (max HP, speed, attack damage, texture), so player can't edit it.

Ziburinis

  • Newbie
  • *
  • Posts: 15
    • View Profile
Re: Best way for organizing turn-based game
« Reply #3 on: August 11, 2016, 11:46:29 pm »
On inheritance you might read this:
https://gamedev.stackexchange.com/questions/14158/how-object-oriented-are-videogames

For your game states it may be perfectly fine, the worst problems tend to come from complex trees where it's simpler and easier to just have "bool can_swim" instead of "Class SwimmingMonster : public Monster".

As for storing data, the only certain way (that I know of) to keep someone from cheating is to have everything validated by a central server. If someone tries to hack the memory that the game is in to set their hero's attack to 5,000, the server has to catch it.

You can keep the casual player from editing their save files by using Cereal to save your classes in binary, then save a hash value of the file at the beginning. The game would check the hash when it loads the data. I play a lot of games on Kongregate, and almost any game that's remotely popular has a "save file hacker" online to get around this, so for this approach to work you'd have to get funky with moving bits around, as well as adding padding to the game binary itself, the way a virus does to resist disassembly.

So, if it's possible, stick with the server that validates data from clients.

MrOnlineCoder

  • Jr. Member
  • **
  • Posts: 87
    • View Profile
Re: Best way for organizing turn-based game
« Reply #4 on: August 14, 2016, 09:16:16 pm »
So, there is another question. The game screen contains battle field, some buttons and player status.
What is the best way to move the heroes? The field is made of 32x32 squares, so hero can move only 32 * n pixels in one direction. Should I store original position in Hero class or it's position in grid (cols and rows)?

Ziburinis

  • Newbie
  • *
  • Posts: 15
    • View Profile
Re: Best way for organizing turn-based game
« Reply #5 on: August 14, 2016, 10:00:29 pm »
I would have something like this in Hero:
unsigned int xpos;
unsigned int ypos;

Then at some point:
for i < player.size
   for j < player.heros.size
      <whatever sets up the sprite to be drawn>

In my game I have a drawable vertex array holder class, much like the one in the tutorial. It has some quads set aside for sprites that move about, and a second array for un-textured vertexes. The class has a member function called update, which gets called when the player scrolls around or something has decided to move. This is the function where I would run the for loops above.

If I wanted to animate something, I would have a list of animation vectors, the vectors would hold indexes which would translate to rectangles in my texture. But I don't do much graphics intensive stuff in my games. SFML gives you a lot of flexibility, and there are probably other ways which are just as good, or better, than what I've suggested.

MrOnlineCoder

  • Jr. Member
  • **
  • Posts: 87
    • View Profile
Re: Best way for organizing turn-based game
« Reply #6 on: August 14, 2016, 10:19:15 pm »
Thanks again, you helped me a lot.

Ziburinis

  • Newbie
  • *
  • Posts: 15
    • View Profile
Re: Best way for organizing turn-based game
« Reply #7 on: August 14, 2016, 10:25:22 pm »
No problem  :)

Also the first for loop should have been "players.size" not "player.size", where players is the list of all players.

I should also add that if I wanted units to smoothly slide, I'd add int x_offset and y_offset, measured in pixels, to the Hero class, and if I was going to animate them, they'd have some enum members like "walk_right", "walk_left" which I'd use as indexes to my vector of animation vectors, that vector would then get copied into the animation list, and popped once the last frame was reached.

MrOnlineCoder

  • Jr. Member
  • **
  • Posts: 87
    • View Profile
Re: Best way for organizing turn-based game
« Reply #8 on: August 28, 2016, 06:23:57 pm »
After some weeks of work, I ended in next design:
Hero class, that contains it HP, Damage, Speed, Range, Position in field coords (just Cell X and Cell Y) and sprite and has method for checking if sprite was clicked or not.
HeroCollection, which is shared between all game screens, and contains all heroes data.
Some pseudocode of game screen logic:
if (window.pollEvent()) {
  Vector2f = Mouse.getMousePos();
  if (wasUIUsed) //Check if menu, end turn button etc. were clicked  {
   doAction();
  }

  int cellX = transformRealX();
  int cellY = transformRealY();

  if (!clickInField) {
     continue;
  }

  for (Hero hero : players heroes vectors) {
   if (hero.isClicked() && hero.isEnemy()) {
    hero.damage();
    endTurn();
   }
  }

  if (getCurrentHero()->canMoveTo(cellX, cellY)) {
    getCurrentHero()->moveTo(cellX, cellY);
    endTurn();
  }

  //draw everything
}
 

Is it OK?

Sorry, maybe question can be too broad.

MrOnlineCoder

  • Jr. Member
  • **
  • Posts: 87
    • View Profile
Re: Best way for organizing turn-based game
« Reply #9 on: September 10, 2016, 09:55:02 am »
I found a nice solution for protecting Lua scripts (spells logic, AI) - LUAC compiler. https://www.lua.org/manual/5.1/luac.html

DarkRoku12

  • Full Member
  • ***
  • Posts: 203
  • Lua coder.
    • View Profile
    • Email
Re: Best way for organizing turn-based game
« Reply #10 on: September 12, 2016, 03:47:21 am »
I found a nice solution for protecting Lua scripts (spells logic, AI) - LUAC compiler. https://www.lua.org/manual/5.1/luac.html

You can protec them from a normal user using luac, but any lua decompiler will decompile your code at 100% of readability. Because luac will only turn it into bytecode, if you donĀ“t add -s option will be even easier to understand when decompiled.

For better but not infalliable security try to compite it to bytecode and the encrypt it.
Try to do your own encrytation system for make it even harder to get.
I would like a spanish/latin community...
Problems building for Android? Look here

MrOnlineCoder

  • Jr. Member
  • **
  • Posts: 87
    • View Profile
Re: Best way for organizing turn-based game
« Reply #11 on: September 14, 2016, 07:31:21 pm »
I ended in next solution:
1.Use luac to protect from normal users
2.Store on server the checksum of spells scripts folder.
3.When going in matchmaking, check the checksums on the client and server. If they are equal - let player start search for opponent. If no - deny.

Also, I have a problem in game design. So all my heroes are stored in every player's vector. The problem is that to check what hero was clicked, I have to iterate through all vectors and call isClicked function. That also makes not easy to interact with other heroes, e.g. find heroes in specified area. Should all heroes be stored in 2d array (field,  in a map <Vector2, Hero> or other way?
« Last Edit: September 14, 2016, 07:35:00 pm by MrOnlineCoder »

 

anything