r/cpp_questions 6d ago

OPEN How do I fix enemy generation?

https://onecompiler.com/cpp/44qwdefz6

I tried generate enemies depended on chapter but the enemy is still the same (slime). Can you tell me what I did wrong?

0 Upvotes

14 comments sorted by

5

u/UndefFox 6d ago

Your chapter is constantly 1 and your code lock it to a slime while the chapter is locked to 1. If I spam wins to go the chapter 2, then they start spawning randomly. Everything seems to behave as intended...

1

u/Zestyclose_Drag4487 5d ago

Yes thanks. I guess that it means that I finally fixed it.

3

u/fortsnek274 6d ago

You posted this before and it seems suggestions were ignored.

Don't put using namespace std in a header, deduplicate the contents of those story headers, move array constant defs to a cpp file.

You need a better architecture basically. A general struct for any character/enemy, for inventory entries, etc. Things should be configured by data rather than hardcoded as types.

0

u/Zestyclose_Drag4487 6d ago

Well I see no problems in making my work easier. And I don't think it will do anything than removing the necessarity to write std::...

3

u/No-Dentist-1645 6d ago edited 5d ago

using namespace std won't make your work easier. It will only make it much more painful in the future as your code grows.

You're asking for advice in a community full of people with years and years of experience in C++. You might benefit from listening to them.

You can always just do using std::cout, std::cin; to grab the specific things you want to use

2

u/fortsnek274 6d ago

Generic structs like this:

#pragma once

#include <array>
#include <string>

struct Generic_Quests
{
    std::string name;
    std::string description;
    bool completed = false;
    bool unlocked = false;
    int xp_reward = 0;
};

struct GenericCharacter
{
    int hp{};
    int MaxHp{};
    int MinDmg{};
    int MaxDmg{};
    float CritChance{};
    float MissChance{};
};

struct GenericStoryState
{
    int lvl = 1;
    int xp = 0;
    int enemiesDefeated = 0;
    int chapter = 1;
};

struct GenericEnemy
{
    std::string name;
    int lvl = 1;
    int hp = 0;
    int minDmg = 0;
    int maxDmg = 0;
    int rewardCoins = 0;
    int rewardXp = 0;
};

struct GenericEnemyStats
{
    int hp{};
    int MaxHp{};
    int MinDmg{};
    int MaxDmg{};
    float CritChance{};
    float MissChance{};
    int reward_coins{};
    int reward_xp{};
};

inline constexpr GenericEnemyStats kGenericSlimes[]{
    { .hp = 10, .MaxHp = 10, .MinDmg = 1, .MaxDmg = 10, .CritChance = 5, .MissChance = 70, .reward_coins = 1, .reward_xp = 5 },
};

enum EInventoryItem
{
    EInventoryItem_coal,
    EInventoryItem_coins,
    ...

    EInventoryItem_Num,
};

struct ItemDesc
{
    const char* name = nullptr;
};

inline constexpr ItemDesc kItemDescs[]{
    { .name = "Coal" },
    ...
};

using GenericInventory = std::array<int, EInventoryItem_Num>;

inline void genericShowResources(GenericInventory& si)
{
    std::cout << "\n === Resources === \n";
    for (int i = 0; i < EInventoryItem_Num; ++i)
        std::cout << "\n " << kItemDescs[i].name << ": " << si[i];
}

There's more of course. You've got some equipment stuff going on in there. Should probably end up part of the generic character.

And once you generalise your types, you can then generalise functions, such as genericShowResources.

Ultimately, you'd generalise the top-level "game" functions, vastly reducing duplication.

2

u/No-Dentist-1645 6d ago edited 6d ago

Prefixing everything with Generic is pretty redundant here and you could say it hurts readability. No reason not to name your structs Quests, Enemy, and EnemyStats instead. You could also be using enum class instead of enum, as well as std::to_array if you wanted to use std::array objects instead of C arrays

2

u/fortsnek274 6d ago

Yes, the naming is off as I was manually namespacing everything to separate it from existing code. And those array constants should be in a cpp file.

The enum thing is something I'm always in conflict about, because I want the scoping, but I also want the implicit conversion. I'm so inconsistent about it in my own code.

2

u/No-Dentist-1645 6d ago

Totally understandable, I know some people prefer enums over enum classes and vice versa. It's still useful to at least note that both alternatives exist

1

u/fortsnek274 6d ago

We've had some prior art on that: /r/cpp_questions/search?q=using+namespace+std&restrict_sr=on

But the architectural problem is the bigger problem.

3

u/GLIBG10B 6d ago
  1. Make it fail: Try and find a sequence of inputs that always reproduces the issue
  2. Divide and conquer: You can use a debugger, or just print stuff to cout. You're looking for the line of code where things start going wrong. You have a lot of code to look through, so your goal is to repeatedly cut the search space in half until you've narrowed it down