Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support multiple cheat codes with the same first letter #96

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion resources/po/planetblupi.pot
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ msgid ""
msgstr ""
"Project-Id-Version: PACKAGE VERSION\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2019-02-17 14:11+0100\n"
"POT-Creation-Date: 2021-02-09 02:45-0600\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <[email protected]>\n"
Expand Down
182 changes: 100 additions & 82 deletions src/event.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,7 @@ typedef struct {
Sint16 reserve2[88];
} DescInfo;

// Toutes les premières lettres doivent
// être différentes !

static char cheat_code[9][20] = {
static char cheat_code[MAXCHEAT][CHEATLENGTH] = {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's a very old and ugly original code. You should prefer "modern" stuff like std::array/std::vector with std::string when replacing old stuff.

"vision", // 0
"power", // 1
"lonesome", // 2
Expand Down Expand Up @@ -1669,6 +1666,9 @@ CEvent::CEvent ()
memset (m_textToolTips, 0, sizeof (m_textToolTips));
memset (m_libelle, 0, sizeof (m_libelle));

for (i = 0; i < MAXCHEAT; i++)
m_bCheatCandidates[i] = true;

for (i = 0; i < MAXBUTTON; i++)
m_lastFloor[i] = 0;

Expand Down Expand Up @@ -5690,7 +5690,7 @@ CEvent::TreatEventBase (const SDL_Event & event)
Sint32 i;
Sounds sound;
char c;
bool bEnable;
bool bIncrement = false;

DemoRecEvent (event);

Expand All @@ -5701,96 +5701,61 @@ CEvent::TreatEventBase (const SDL_Event & event)
{
if (m_posCheat == 0) // première lettre ?
{
m_rankCheat = -1;
for (i = 0; i < 9; i++)
for (i = 0; i < MAXCHEAT; i++)
{
if ((char) event.key.keysym.sym == cheat_code[i][0])
{
m_rankCheat = i;
break;
m_bCheatCandidates[i] = true;
m_posCheat = 1;
}
else
{
m_bCheatCandidates[i] = false;
}
}
}
if (m_rankCheat != -1)
else if (m_posCheat > 0)
{
c = cheat_code[m_rankCheat][m_posCheat];
if (m_posCheat != 0 && m_rankCheat == 8)
c++; // CONSTRUIRE ?
if ((char) event.key.keysym.sym == c)
bIncrement = false;
for (i = 0; i < MAXCHEAT; i++)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Then with std::array/std::vector we can use a for..rang loop instead of all these ugly for loop based on defines

{
m_posCheat++;
if (cheat_code[m_rankCheat][m_posCheat] == 0)
if (m_posCheat >= CHEATLENGTH)
{
bEnable = true;
if (m_phase == EV_PHASE_PLAY)
{
if (m_rankCheat == 0) // vision ?
m_pDecor->EnableFog (false);
else if (
m_rankCheat == 1 || // power ?
m_rankCheat == 2) // lonesome ?
m_pDecor->BlupiCheat (m_rankCheat);
}

switch (m_rankCheat)
{
case 3: // allmissions ?
{
m_bAllMissions = !m_bAllMissions;
bEnable = m_bAllMissions;
break;
}
case 4: // quick ?
{
m_bSpeed = !m_bSpeed;
bEnable = m_bSpeed;
break;
}
case 5: // helpme ?
{
m_bHelp = !m_bHelp;
bEnable = m_bHelp;
break;
}
case 6: // invincible ?
{
m_pDecor->SetInvincible (!m_pDecor->GetInvincible ());
bEnable = m_pDecor->GetInvincible ();
break;
}
case 7: // superblupi ?
{
m_pDecor->SetSuper (!m_pDecor->GetSuper ());
bEnable = m_pDecor->GetSuper ();
break;
}
case 8: // construire ?
{
m_bAccessBuild = !m_bAccessBuild;
bEnable = m_bAccessBuild;
break;
}
}

if (m_phase != EV_PHASE_PLAY)
ChangePhase (m_phase);

pos.x = LXIMAGE () / 2;
pos.y = LYIMAGE () / 2;
if (bEnable)
m_pSound->PlayImage (SOUND_GOAL, pos);
else
m_pSound->PlayImage (SOUND_BOING, pos);

m_rankCheat = -1;
m_posCheat = 0;
m_posCheat = 0;
break;
}

if (!m_bCheatCandidates[i])
continue;

c = cheat_code[i][m_posCheat];

if (i == 8) // CONSTRUIRE ?
c++;

if ((char) event.key.keysym.sym == c)
{
if (!bIncrement)
m_posCheat++;
bIncrement = true;
}
else
{
m_bCheatCandidates[i] = false;
}

if (cheat_code[i][m_posCheat] == 0)
{
HandleCheat (i);
m_posCheat = 0;
}
return true;
}
if (bIncrement)
return true;
else
m_posCheat = 0;
}
}
m_rankCheat = -1;
m_posCheat = 0;

if (m_phase == EV_PHASE_INTRO1)
{
Expand Down Expand Up @@ -6426,3 +6391,56 @@ CEvent::PushUserEvent (Sint32 code, void * data)

SDL_PushEvent (&event);
}

void
CEvent::HandleCheat (Sint32 index)
{
bool bEnable = true;
if (m_phase == EV_PHASE_PLAY)
{
if (index == 0) // vision ?
m_pDecor->EnableFog (false);
else if (
index == 1 || // power ?
index == 2) // lonesome ?
m_pDecor->BlupiCheat (index);
}

switch (index)
{
case 3: // allmissions ?
m_bAllMissions = !m_bAllMissions;
bEnable = m_bAllMissions;
break;
case 4: // quick ?
m_bSpeed = !m_bSpeed;
bEnable = m_bSpeed;
break;
case 5: // helpme ?
m_bHelp = !m_bHelp;
bEnable = m_bHelp;
break;
case 6: // invincible ?
m_pDecor->SetInvincible (!m_pDecor->GetInvincible ());
bEnable = m_pDecor->GetInvincible ();
break;
case 7: // superblupi ?
m_pDecor->SetSuper (!m_pDecor->GetSuper ());
bEnable = m_pDecor->GetSuper ();
break;
case 8: // construire ?
m_bAccessBuild = !m_bAccessBuild;
bEnable = m_bAccessBuild;
break;
}

if (m_phase != EV_PHASE_PLAY)
ChangePhase (m_phase);

if (bEnable)
m_pSound->PlayImage (SOUND_GOAL, {LXIMAGE () / 2, 0});
else
m_pSound->PlayImage (SOUND_BOING, {LXIMAGE () / 2, 0});

return;
}
6 changes: 6 additions & 0 deletions src/event.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@

class CMovie;

#define MAXCHEAT 9
#define CHEATLENGTH 20

/////////////////////////////////////////////////////////////////////////////

typedef struct {
Expand Down Expand Up @@ -159,6 +162,8 @@ class CEvent

static void PushUserEvent (Sint32 code, void * data = nullptr);

void HandleCheat (Sint32 index);

protected:
void DrawTextCenter (const char * text, Sint32 x, Sint32 y, Sint32 font = 0);
bool CreateButtons (Sint32 phase);
Expand Down Expand Up @@ -279,6 +284,7 @@ class CEvent
std::string m_updateVersion;
Uint32 shiftDirection;
bool statDisabled;
bool m_bCheatCandidates[MAXCHEAT];
};

/////////////////////////////////////////////////////////////////////////////