Monsters Using Items, Scrolls and Debugging
I just tracked down a mysterious bug that had me baffled for months. In my game, many items that you would normally expect are stackable, like coins, potions, arrows, and pieces of food, but items like weapons and armour are not stackable. All items have a 'count' variable, but for non-stackable items, count should always be equal to 1.
Well, occasionally while play testing, I would notice that instead of wielding a single weapon, I was somehow holding "40 iron axes", or some other absurd number. Naturally, the weight of carrying so many axes was a great burden that severely hindered my movement. I had no idea where to even start looking for this bug because the count for all weapons was initialized at 1 and should not be changed under any circumstances.
Anyway, I finally noticed that this bug seemed to occur after doing some kind of financial transaction, like buying an item in a shop. I also noticed that the same bug could occur to NPC's after I sold some gems to a treasure dealer and then noticed that he was holding 7 maces at once.
So I dug down into the function used for paying for stuff. In this game, coins are stored in inventory just like any other item, and there are three kinds of coins: copper coins, silver coins (worth 5 copper coins each), and gold coins (worth 25 copper coins each). When you pay for something, the function first goes through your inventory (stored as a dynamic array) to find where in the array you have these three kinds of coins and how many you have of each. It then works out how many of each type of coin are used to pay (trying to pay first using the lowest value coins) and determines how many of each type of coin you should have left. It's possible to cash in a high value coin to get lower value coins in change, e.g., paying 1 gold coin for an item worth 18 copper coins and receiving 1 silver coin and 2 copper coins in change.
Anyway, the next part of the function was to iterate back through the inventory to set the counts for each coin to what they should be after the exchange or deleting the item from inventory of there were no coins remaining. And here's where the problem was. When an empty coin stack was deleted from inventory, I did not update the variables holding the indexes for the other coins. So if you paid for something using multiple coin types and used up all your copper coins, copper coins were deleted and the whole inventory was shifted over one place, then instead of updating the count of silver coins, it would change the count of some other item. Since a creature's melee weapon was usually at the top of the inventory after money, the count for this item would get changed, leaving you with a whole pile of swords. I just had to decrement the placeholders when something was deleted and the problem was fixed.
Aside from that victory, other new changes include:
- Finally added scrolls. Since there are already tonnes of magic items, including around 20 potion types, this feels like a late development. I hitched scrolls onto the code for spells, so a scroll is functionally like a one-shot spell. This made it easy to create a bunch of scrolls right away.
- Added a variation on the invisibility status effect, called "cloak". Being cloaked functions just like being invisible, with the exception that enemies have to turn off cloaking in order to attack the player. There are now cloaking spiders who do just this, spending most of their time cloaked, but suddenly appearing when the player is 2 cells away. They turn it back on when fleeing or when no longer in battle.
- Started adding ways for Monsters to use items other than weapons and armour. Each potion and scroll was assigned a battle value and an escape value. When a Monster begins its turn in a state of battle, it checks its inventory for the item with the highest battle value and uses it (as long as that value is above 0). If the Monster is fleeing, it does the same thing checking for escape value. This simple method is enough for Monsters to take advantage of any sort of buff item.
- Monsters can now consider certain potion types as throwable weapons, namely potions of acid and potions of fire. They will only attempt this if they are standing safely outside the splash radius, but so far do not bother to check that their buddies are safe. Next in line are the gas releasing potions.
Sunlorn
Open world and cave exploration game.
Status | In development |
Author | Tesselation9000 |
Genre | Adventure |
Tags | Roguelike |
More posts
- Robbers, Mazes and Portcullises15 days ago
- Illusions, Camouflage and Monster Relations22 days ago
- Monster Camouflage and Divination Spells22 days ago
- Action Queue70 days ago
- Gas Overhauled70 days ago
- More ContentJun 02, 2024
- Birth of the GodsMay 20, 2024
- Revised City GenerationMar 16, 2024
Leave a comment
Log in with itch.io to leave a comment.