aboutsummaryrefslogtreecommitdiff
path: root/engines/sludge/variable.h
blob: 1ae2acacc7fd3f7117520d219cd816d84a20b4d0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
/* ScummVM - Graphic Adventure Engine
 *
 * ScummVM is the legal property of its developers, whose names
 * are too numerous to list here. Please refer to the COPYRIGHT
 * file distributed with this source distribution.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 */
#ifndef SLUDGE_VARIABLE_H
#define SLUDGE_VARIABLE_H

namespace Sludge {

struct Persona;
struct PersonaAnimation;
struct Variable;
struct VariableStack;

enum VariableType {
	SVT_NULL,
	SVT_INT,
	SVT_FUNC,
	SVT_STRING,
	SVT_BUILT,
	SVT_FILE,
	SVT_STACK,
	SVT_OBJTYPE,
	SVT_ANIM,
	SVT_COSTUME,
	SVT_FASTARRAY,
	SVT_NUM_TYPES
};

struct FastArrayHandler {
	struct Variable *fastVariables;
	int size;
	int timesUsed;

	Variable *fastArrayGetByIndex(uint theIndex);
};

struct StackHandler {
	struct VariableStack *first;
	struct VariableStack *last;
	int timesUsed;

	int getStackSize() const;
	bool getSavedGamesStack(const Common::String &ext);
};

union VariableData {
	signed int intValue;
	const char *theString;
	StackHandler *theStack;
	PersonaAnimation *animHandler;
	Persona *costumeHandler;
	FastArrayHandler *fastArray;
};

struct Variable {
	VariableType varType;
	VariableData varData;

	Variable() {
		varType = SVT_NULL;
		varData.intValue = 0;
	}

	void unlinkVar();
	void setVariable(VariableType vT, int value);

	// Copy from another variable
	bool copyFrom(const Variable &from);
	bool copyMain(const Variable &from); // without variable unlink

	// Load & save
	bool save(Common::WriteStream *stream);
	bool load(Common::SeekableReadStream *stream);

	// Text variable
	void makeTextVar(const Common::String &txt);
	bool loadStringToVar(int value);

	// Animation variable
	void makeAnimationVariable(PersonaAnimation *i);
	struct PersonaAnimation *getAnimationFromVar();

	// Custome variable
	void makeCostumeVariable(Persona *i);
	struct Persona *getCostumeFromVar();

	// Fast array variable
	bool makeFastArrayFromStack(const StackHandler *stacky);
	bool makeFastArraySize(int size);

	// Stack variable
	bool copyStack(const Variable &from);

	// Add variables
	void addVariablesInSecond(const Variable &other);
	void compareVariablesInSecond(const Variable &other);
	int compareVars(const Variable &other) const;

	// General getters
	Common::String getTextFromAnyVar() const;
	bool getBoolean() const;
	bool getValueType(int &toHere, VariableType vT) const;
};

struct VariableStack {
	Variable thisVar;
	VariableStack *next;

	// Variable getter & setter
	bool stackSetByIndex(uint, const Variable &);
	Variable *stackGetByIndex(uint);

	// Find last
	VariableStack *stackFindLast();
};

// Stacky stuff

bool addVarToStack(const Variable &va, VariableStack *&thisStack);
bool addVarToStackQuick(Variable &va, VariableStack *&thisStack);
void trimStack(VariableStack *&stack);
int deleteVarFromStack(const Variable &va, VariableStack *&thisStack, bool allOfEm = false);

// load & save
void saveStack(VariableStack *vs, Common::WriteStream *stream);
VariableStack *loadStack(Common::SeekableReadStream *stream, VariableStack **last);
bool saveStackRef(StackHandler *vs, Common::WriteStream *stream);
StackHandler *loadStackRef(Common::SeekableReadStream *stream);
void clearStackLib();

} // End of namespace Sludge

#endif