src/cheat.cpp
author truebrain
Fri, 18 Jul 2008 01:00:03 +0000
branchnoai
changeset 11166 17960948c3af
parent 10776 07203fc29812
permissions -rw-r--r--
(svn r13724) [NoAI] -Fix r13723: in this modern world with all those checks and warnings GCC can give when ever I not ask him to, it fails to see the simplest of all, 'if (cargo_id == cargo_id)'.. makes you wonder, doesn't it? Well, such is life, wondering wondering wandering .. Status Quo, Yeah! Oh yeah, what was I doing ... ah, yes: don't compare one variable with itself, it is always true.. Obiwan by Yexo. (Yexo)
/* $Id$ */

/** @file cheat.cpp Handling (loading/saving/initializing) of cheats. */

#include "stdafx.h"
#include "saveload.h"
#include "cheat_type.h"

Cheats _cheats;

void InitializeCheats()
{
	memset(&_cheats, 0, sizeof(Cheats));
}

static void Save_CHTS()
{
	/* Cannot use lengthof because _cheats is of type Cheats, not Cheat */
	byte count = sizeof(_cheats) / sizeof(Cheat);
	Cheat *cht = (Cheat*) &_cheats;
	Cheat *cht_last = &cht[count];

	SlSetLength(count * 2);
	for (; cht != cht_last; cht++) {
		SlWriteByte(cht->been_used);
		SlWriteByte(cht->value);
	}
}

static void Load_CHTS()
{
	Cheat *cht = (Cheat*)&_cheats;
	size_t count = SlGetFieldLength() / 2;

	for (uint i = 0; i < count; i++) {
		cht[i].been_used = (SlReadByte() != 0);
		cht[i].value     = (SlReadByte() != 0);
	}
}

bool CheatHasBeenUsed()
{
	/* Cannot use lengthof because _cheats is of type Cheats, not Cheat */
	const Cheat* cht = (Cheat*)&_cheats;
	const Cheat* cht_last = &cht[sizeof(_cheats) / sizeof(Cheat)];

	for (; cht != cht_last; cht++) {
		if (cht->been_used) return true;
	}

	return false;
}


extern const ChunkHandler _cheat_chunk_handlers[] = {
	{ 'CHTS', Save_CHTS,     Load_CHTS,     CH_RIFF | CH_LAST}
};