[CODE] Cheat mode (Now Included)

All code submission.
Psyringe
Posts: 1163
Joined: Mon Aug 31, 2009 10:53 am

[CODE] Cheat mode (Now Included)

Post by Psyringe » Tue Oct 20, 2009 10:35 pm

Hi :)

During my last forays into the Wagic code, I implemented something that might be useful for others too - a "Cheat mode" to aid development. It's mainly aimed at AI deck developers, but others may find it useful too. If you try it out, please review my code. I think this one is clean enough to be submitted to SVN (if the feature itself is approved), but I'm only coding in C++ for a week, so I'm in a bad position to judge the quality of my code.

Current feature list:

- "Cheat mode" option. This is a simple on/off setting in the "General Options" tab. However, you'll only see this option if you enter the options menu with a profile called "Cheetah". (I hope the quality of my code is better than the quality of my puns.) So, to access cheat mode, you have to create and activate a profile called "Cheetah", save it, then leave and re-enter the options menu. Then you'll see (and can activate) the "Enable cheat mode" option. It's a bit hidden, but hey, that's what cheat modes are supposed to be. ;)

The cheat mode setting gets saved in the profile together will other settings. The most convenient way to use the cheat mode is to keep the "Cheetah" profile and use it for testing, development, etc. You can still *play* with your regular profile, without any worries that using the cheat mode might screw something up.

Since the cheat mode is an option, it can be tested from anywhere in the code with "if(options[Options::CHEATMODE].number)".

- "Complete Collection" function. When cheat mode is switched on, then a new menu item will appear in the deck editor: Complete Collection. If you click on it, the game will add cards to your collection, so that you have at least 4 of every card in the game. This is basically the functionality of a "Collection Tool" integrated into Wagic. Personally I find it a bit more convenient to use, because I don't have to start an external program. Also, the "Complete Collection" function will never *reduce* the amount of those cards from which you already have more than 4. So if you have 30 Relentless Rats in your collection, now you don't have to worry any more that using a collection tool will reset the number to 4.

Since this function has to do some serious number crunching, I implemented it in the MTGDeck class (instead of DeckDataWrapper) and avoided calls to other methods (like "add"). It takes about 2 seconds on my PC to add 4 instances each of 5400 cards to the collection. Let me know if it's still too slow on the PSP. I could squeeze out some more speed if I had to (if all else fails, setting all card numbers to 4 would be faster).

- "Deck integrity" function. When cheat mode is switched on, and you load a deck for which you don't have all cards in your collection, then the missing cards will not be removed from the deck. Instead they will be added to your collection.

This means you can load and edit a deck that used 20 Swamp cards with id=129756, and you won't be missing a third of a deck even though you previously only had 4 of these swamps.

The added cards will stay in your collection if you save the deck where they came from. So, if you once loaded a deck with 16 Relentless Rats, and clicked "save", you will afterwards have 16 Relentless Rats in your collection, ready to be used in your own deck building.

- Money cheat. When you buy an item in the shop, and cheat mode is switched on, you now have an option to steal 1,000 credits from the shopkeeper. You can click the respective button as often as you want. This function might come in handy to test the contents of booster packs, or if you need money to test something else once the economy aspect of the game has further developed.

Source code is included (sorry about the high number of files - the code changes are actually pretty small, sometimes there's just a single line added to a file, but many files were affected).

Let me know what you think. :) Here's the download (based on r1116):

Edit: Download removed, it's obsolete now. The feature has been integrated into the SVN version of Wagic since r1224.

[EDIT ABRA - renamed topic]
Last edited by Psyringe on Tue Oct 20, 2009 11:50 pm, edited 2 times in total.

wololo
Site Admin
Posts: 3727
Joined: Wed Oct 15, 2008 12:42 am
Location: Japan

Re: Cheat mode - code included, please review

Post by wololo » Tue Oct 20, 2009 10:38 pm

I currently don't have the time to review your code myself, but both this and the"gauntlet" ideas sound interesting to me, so I keep them in mind for when I have more time :)

Psyringe
Posts: 1163
Joined: Mon Aug 31, 2009 10:53 am

Re: Cheat mode - code included, please review

Post by Psyringe » Wed Oct 21, 2009 8:38 am

I'm interested in having this reviewed quickly, so if I can do anything to help speed up the process, please let me know. :)

Long-winded explanation: The reason for this is that I'm currently running into problems because I have different projects (cheat mode, new deckfile layout, gauntlet mode, plus some more I'm currently toying around with) which make changes to the same files. For example, I now have a version (a) of MTGDeck.cpp which uses the new deckfile layout and has the "complete" function that I needed for the cheat mode. However couldnt release this file in the zip attached to my first post, because then the player's deckfiles would be saved in the new format, and then the user would *require* my MTGDeck.cpp to read them in correctly. Obviously my cheat mode test version should not make such changes to a user's files, so I reverted the file to (b) the current official MTFDeck.cpp on SVN, and then (c) copied the "complete" function in that I need for the cheat mode. Version (c) is the one uploaded in the zip above. I then reverted my personal version of the file to (a) so that I could further work on it. However I either made a mistake while doing that, or I forgot to build a new exe after changing the files - in any case my template.exe was build with the wrong MTGDeck.cpp for a while. This resulted in several hours of AI-vs-AI match results having to be trashed, because the program didn't read the deckfiles in correctly.

I realize that this was my own mistake, but I'm afraid that such things will happen again (and more often), since different features working on keep requiring changes to the same files. Getting submission-worthy code reviewed and submitted quickly would definitely improve the situation for me, because then I wouldn't need to worry about changes between the SVN version and my personal version (for the code submitted). (There's probably also some way how I can manage different additions to the same source better, but I don't know about those - if anyone has a tip or two, don't hesitate to mention them. :) )

Anyway, I realize that this is a hobby project and that there are times when other things take priority. There's no doubt that I will have long stretches of next-to-no spare time myself, so please don't take the above paragraph as a complaint, it is none. Just wanted to explain why a quick code review would make my workflow a bit easier (and less error prone ...). Again, if I can do anything to minimize the work that you would have with a code review (like bringing the sources in a specific format format, or adding even more comments), please tell me. :)

wololo
Site Admin
Posts: 3727
Joined: Wed Oct 15, 2008 12:42 am
Location: Japan

Re: Cheat mode - code included, please review

Post by wololo » Wed Oct 21, 2009 8:57 am

In order to manage several projects in parallel, I would suggest that you checkout the code in several working directories. Each directory will be considered as a different user (I hope) for the SVN server, allowing you to stay in sync yet work on different projects.

The "real" way to do this is to create branches, but this is usually a painful process for little benefit unless you have a very huge codebase.

I'll try to review your code this week :)

wololo
Site Admin
Posts: 3727
Joined: Wed Oct 15, 2008 12:42 am
Location: Japan

Re: Cheat mode - code included, please review

Post by wololo » Sat Oct 31, 2009 10:53 am

Hi, I've found one minor code issue, and one "security/user unfriendliness" issue

1) Code issue
MTGDeck.cpp:
for (int it = (int)database->ids.size() -1 ; it >= 0 ; it--) {

replace int it with size_t it, otherwise the game will refuse to compile for the PSP and Linux, and Jean will yell at you

2) User friendliness
you do the following test in the game to check if the cheatmode is activated or not:
if(options[Options::CHEATMODE].number)

I believe that this is not enough to prevent bad side effects

Here's a scenario with a bad side effect, I believe your current code would lead to that:
- Create a Cheetah profile
- Enable Cheat mode in the options
- Switch back your profile to Default
-> you are still in cheat mode, even if you restart the game, AND you can't access the option in the menu to deactivate it, until you switch back again to the Cheetah profile.

Suggested solution: use
if(options[Options::CHEATMODE].number && options[Options::ACTIVE_PROFILE].str == "Cheetah") everytime to check if the profile is Cheetah+cheating is enabled


An alternate solution would be to always display the option in the option menu IF the profile is "Cheetah" OR IF cheatmode is activated. This allows people to deactivate the mode if they are in cheating mode while not in the cheetah profile.
so in gamestateoptions.cpp, instead of:

Code: Select all

  if(options[Options::ACTIVE_PROFILE].str == "Cheetah")
    optionsList->Add(NEW OptionInteger(Options::CHEATMODE, "Enable cheat mode"));
it would be

Code: Select all

  if(options[Options::CHEATMODE].number || options[Options::ACTIVE_PROFILE].str == "Cheetah")
    optionsList->Add(NEW OptionInteger(Options::CHEATMODE, "Enable cheat mode"));
I haven't actually tested so I am not 100% sure that this is what will happen, but browsing at the code feels like it.


Other than these two things, your code looks alright to me. When you think these two things have been corrected, feel free to submit it to the SVN, and I'll code review it again (always easier to code review within the SVN environment) :)

Psyringe
Posts: 1163
Joined: Mon Aug 31, 2009 10:53 am

Re: Cheat mode - code included, please review

Post by Psyringe » Sat Oct 31, 2009 12:18 pm

Thanks for reviewing! :)
wololo wrote:1) Code issue
MTGDeck.cpp:
for (int it = (int)database->ids.size() -1 ; it >= 0 ; it--) {

replace int it with size_t it, otherwise the game will refuse to compile for the PSP and Linux, and Jean will yell at you
Just replacing int with size_t will probably crash the game since the loop will see an integer underrun instead of meeting its break condition, but I'll rewrite it to count upwards instead of downwards so that I can use size_t.

It's good to know that the Linux and PSP compilers are more strict than VC, I'll have to keep that in mind for future development.
wololo wrote: - Create a Cheetah profile
- Enable Cheat mode in the options
- Switch back your profile to Default
-> you are still in cheat mode, even if you restart the game, AND you can't access the option in the menu to deactivate it, until you switch back again to the Cheetah profile.
You shouldn't be in cheat mode under that condition because cheatmode is a profile-specific option. When you switch your profile back to Default, then all profile-specific options get reloaded/reset, so cheatmode will be 0 immediately after switching back.

There's still the potential security hole in that I mentioned in the first post, of course: After switching back from Cheetah to another profile, you'll still have the menu option "Enable cheat mode"? available, in the new profile, as long as you don't leave the options menu. But I think I found a way to fix it (the menu item has to be hidden when a non-Cheetah profile gets loaded).

Jeck pointed out another hole: With the code as I wrote it, people would be able to add the line "cheatmode=1" to their options, and then have cheatmode available in other profiles. But that can be solved by setting cheatmode to 0 after loading the profile if the profile is not "Cheetah".
wololo wrote: Suggested solution: use
if(options[Options::CHEATMODE].number && options[Options::ACTIVE_PROFILE].str == "Cheetah") everytime to check if the profile is Cheetah+cheating is enabled
I try to avoid checking the profile name for every cheat item I add to the code, checking for the profile too is easy to forget and potentially error-prone. But if I'm correct, then it's possible to make sure that *only* the Cheetah profile can be in cheatmode, so it won't be necessary to check the profile name everytime throughout the code.
wololo wrote: Other than these two things, your code looks alright to me. When you think these two things have been corrected, feel free to submit it to the SVN, and I'll code review it again (always easier to code review within the SVN environment) :)
Will do. :) Thanks again for taking the time to review this bloody C++ beginner's code. :)

wololo
Site Admin
Posts: 3727
Joined: Wed Oct 15, 2008 12:42 am
Location: Japan

Re: Cheat mode - code included, please review

Post by wololo » Sat Oct 31, 2009 12:31 pm

Ok, you made good points, I didn't realize the variable was wiped out when loading a new profile!
I'm not too concerned about people adding the line cheatmode=1 in their profile. If people want to cheat, there are so many ways to do so in Wagic that I don't think it matters too much. Either way, they'd have to come here to know about such techniques, which is already a sign that they are interested in the game and might not want to spoil their own fun :)

Psyringe
Posts: 1163
Joined: Mon Aug 31, 2009 10:53 am

Re: Cheat mode - code included, please review

Post by Psyringe » Sat Oct 31, 2009 2:28 pm

I'll update the code and commit it as soon as I find some time - will probably be busy today, but tomorrow should work. Thanks again. :)

flad_nag
Posts: 248
Joined: Mon Aug 31, 2009 7:56 am

Re: Cheat mode - code included, please review

Post by flad_nag » Sun Nov 08, 2009 3:19 pm

Just want to verify if this cheetah profile is already online in the SVN? I made a cheetah profile, but no cheat option appears in the option menu... :?:

Psyringe
Posts: 1163
Joined: Mon Aug 31, 2009 10:53 am

Re: Cheat mode - code included, please review

Post by Psyringe » Sun Nov 08, 2009 3:27 pm

Ah, I should have updated this thread. Thanks for bringing it to my attention.

Cheat mode has been completely submitted into SVN since revision 1224, but the access has changed. I'd like to keep the info about the access away from the forum for now, so that players with little self control don't stumble across it and then rob themselves of the enjoyment of successively building up a stronger collection because they can't refrain from using the cheats. But I will PM it to anyone interested. Check your inbox in three minutes. :)

Post Reply