Alchemy
Loading...
Searching...
No Matches

flags.c

Go to the documentation of this file.
1/// @file
2/// The game flags: bits, bytes and nibbles.
3/// Part of game.c, the game object, compiled only through it.
4
5#include "../game_internal.h"
6
7// The game flags: 4096 bits in gGameFlagBytes. A flag number's low three bits
8// pick the bit and the next nine the byte; byte and nibble flags use the same
9// numbering.
10
11/// Returns 1 when `flag` is set. Every bank tests flags through it, as
12/// GameFlag_TestFar, and the scenes through the event layer.
14 s32 flagMask;
15 u8 *flagBytes;
16 s32 flagValue;
17
18 flagMask = 1 << (7 & flag);
19 flag = GAME_FLAG_BYTE(flag);
20 flagBytes = gGameFlagBytes;
21 flagValue = flagBytes[flag] & flagMask;
22 return (u32)((0 - flagValue) | flagValue) >> 0x1F;
23}
24
25/// Sets `flag` and returns its byte number. Called from every bank, as
26/// GameFlag_SetBitFar, and by the scenes through the event layer.
28 s32 flagMask;
29 u8 *flagBytes;
30
31 flagMask = 1 << (7 & flag);
32 flagBytes = gGameFlagBytes;
33 flag = GAME_FLAG_BYTE(flag);
34 flagBytes[flag] |= flagMask;
35 return flag;
36}
37
38/// Clears `flag`. Called from every bank, as GameFlag_ClearBitFar, and by the
39/// scenes through the event layer.
41 s32 flagMask;
42 u8 *flagBytes;
43
44 flagMask = ~(1 << (7 & flag));
45 flagBytes = gGameFlagBytes;
46 flag = GAME_FLAG_BYTE(flag);
47 flagBytes[flag] &= flagMask;
48}
49
50/// Flips `flag` and returns its new value. Only GAME.S's bare veneer reaches
51/// it; nothing calls it by name.
53 s32 flagMask;
54 u8 *flagBytes;
55 s32 flagValue;
56
57 flagMask = 1 << (7 & flag);
58 flagBytes = gGameFlagBytes;
59 flag = GAME_FLAG_BYTE(flag);
60 flagBytes[flag] ^= flagMask;
61 flagValue = flagBytes[flag] & flagMask;
62 return (u32)((0 - flagValue) | flagValue) >> 31;
63}
64
65/// Returns the whole byte `flag` is in, a counter kept among the flags. Called
66/// by the battle encounter code and, through FieldGameFlag_GetByte, by the
67/// Colosso scenes and the Battle Arena.
69 u32 byteIndex;
70
71 byteIndex = (u32)flag << 20;
72 flag = byteIndex >> 23;
73 return gGameFlagBytes[flag];
74}
75
76/// Sets the whole byte `flag` is in. Reached through FieldGameFlag_SetByte by
77/// the Colosso scenes, the world map and the Battle Arena's win counts.
78void GameFlag_SetByte(s32 flag, s32 value) {
79 u32 shifted = (u32)flag << 20;
80 flag = shifted >> 23;
81 gGameFlagBytes[flag] = value;
82}
83
84/// Adds 1 to the byte `flag` is in, stopping at 255, and returns it. Only
85/// GAME.S's bare veneer reaches it; nothing calls it by name.
87 u8 *flagBytes;
88
89 flagBytes = gGameFlagBytes;
90 flag = GAME_FLAG_BYTE(flag);
91 if (flagBytes[flag] <= 0xFE) {
92 flagBytes[flag]++;
93 }
94 return flagBytes[flag];
95}
96
97/// Takes 1 from the byte `flag` is in, stopping at 0, and returns it. Only
98/// GAME.S's bare veneer reaches it; nothing calls it by name.
100 u8 *flagBytes;
101
102 flagBytes = gGameFlagBytes;
103 flag = GAME_FLAG_BYTE(flag);
104 if (flagBytes[flag] != 0) {
105 flagBytes[flag] += 0xFF;
106 }
107 return flagBytes[flag];
108}
109
110/// Returns the four bits `flag` is in (bit 2 of the number picks the half).
111/// Only GAME.S's bare veneer reaches it; nothing calls it by name.
113 u32 shifted = (u32)flag << 20;
114 u32 shift;
115 u32 mask;
116 s32 result;
117
118 shift = flag & 4;
119 mask = 15;
120 flag = shifted >> 23;
121 result = gGameFlagBytes[flag];
122 result &= mask << shift;
123 return result >> shift;
124}
125
126/// Sets the four bits `flag` is in. Only GAME.S's bare veneer reaches it;
127/// nothing calls it by name.
128void GameFlag_SetNibble(s32 flag, s32 value) {
129 s32 fieldMask = 0xF;
130 s32 shift = 4 & flag;
131 s32 mask = fieldMask << shift;
132 u8 *bytes = gGameFlagBytes;
133
134 flag = GAME_FLAG_BYTE(flag);
135 bytes[flag] = (bytes[flag] & ~mask) | ((value & fieldMask) << shift);
136}
s32 GameFlag_Test(s32 flag)
Returns 1 when flag is set.
Definition flags.c:13
u8 GameFlag_DecrementByte(s32 flag)
Takes 1 from the byte flag is in, stopping at 0, and returns it.
Definition flags.c:99
u32 GameFlag_Toggle(s32 flag)
Flips flag and returns its new value.
Definition flags.c:52
s32 GameFlag_SetBit(s32 flag)
Sets flag and returns its byte number.
Definition flags.c:27
void GameFlag_SetNibble(s32 flag, s32 value)
Sets the four bits flag is in.
Definition flags.c:128
void GameFlag_SetByte(s32 flag, s32 value)
Sets the whole byte flag is in.
Definition flags.c:78
void GameFlag_ClearBit(s32 flag)
Clears flag.
Definition flags.c:40
s32 GameFlag_GetByte(s32 flag)
Returns the whole byte flag is in, a counter kept among the flags.
Definition flags.c:68
s32 GameFlag_GetNibble(u32 flag)
Returns the four bits flag is in (bit 2 of the number picks the half).
Definition flags.c:112
u8 GameFlag_IncrementByte(s32 flag)
Adds 1 to the byte flag is in, stopping at 255, and returns it.
Definition flags.c:86
u8 gGameFlagBytes[]
#define GAME_FLAG_BYTE(flag)
The byte of gGameFlagBytes that holds flag: bits 3 to 11 of its number, isolated by shifting up and b...
Definition game_flags.h:12
What every part of the game object shares: its includes and the inventory helper two parts use.
signed int s32
Definition types.h:14
unsigned int u32
Definition types.h:15
unsigned char u8
Definition types.h:11