102 Dalmatians: Puppies to the Rescue
Decompilation Source Reference | GitLab repository
Loading...
Searching...
No Matches
progress.c
Go to the documentation of this file.
1/* Recovered game implementation; original addresses identify provenance. */
12#include "pcdogs/level/types.h"
13#include "pcdogs/math/types.h"
14#include "pcdogs/menu/globals.h"
16#include "pcdogs/save/globals.h"
17#include "pcdogs/save/types.h"
18#include "pcdogs/text/globals.h"
19#include <string.h>
20
21// Original pcdogs.exe: 0x426dd0
22// Returns the supplied level identifier unchanged.
23int32_t Level_PassthroughIndex(int32_t level_id)
24{
25 return level_id;
26}
27
28// Original pcdogs.exe: 0x426e90
29// Maps campaign and bonus level identifiers to their save-array indices, returning -1 for unsupported levels and campaign bosses.
30int32_t Level_GetSaveArrayIndex(int32_t level_id)
31{
32 if (level_id < LEVEL_CAMPAIGN_FIRST)
34
35 if (level_id < LEVEL_BONUS_FIRST)
36 {
37 if (level_id == LEVEL_WORLD_ONE_BOSS || level_id == LEVEL_WORLD_TWO_BOSS || level_id == LEVEL_WORLD_THREE_BOSS || level_id == LEVEL_WORLD_FOUR_BOSS)
39
40 return level_id - LEVEL_CAMPAIGN_FIRST - (level_id - LEVEL_CAMPAIGN_FIRST) / 5;
41 }
42
43 if (level_id < LEVEL_AFTER_BONUS)
44 return level_id - LEVEL_BONUS_FIRST;
45
47}
48
49// Original pcdogs.exe: 0x426ee0
50// Selects the saved or default bonus initials and publishes their display pointer and three character codes. The level-32 save slot aliases level-31 time data, and some defaults retain small integer pointers that shipped text records do not dereference.
51void Level_InitializeBonusData(int32_t level_id, int32_t save_index)
52{
53 uint16_t packed_bonus_initials = g_collectiblesData.bonus_initials[save_index];
54 char *bonus_display_name = (char *)(intptr_t)save_index;
55
56 if (packed_bonus_initials)
57 bonus_display_name = &g_collectiblesData.bonus_name_entry_buffer[save_index * 4];
58 else
59 {
60 switch (level_id)
61 {
63 bonus_display_name = g_sz_Level_BonusRFF;
64 packed_bonus_initials = 0x16;
65 break;
67 bonus_display_name = g_sz_Level_BonusPR3;
68 packed_bonus_initials = 0xb41;
69 break;
71 bonus_display_name = g_sz_Level_BonusRFF;
72 packed_bonus_initials = 0x5882;
73 break;
75 bonus_display_name = g_sz_Level_BonusTOB;
76 packed_bonus_initials = 0x641;
77 break;
78 }
79 }
80
81 g_dynamicStringSlot0 = bonus_display_name;
82
83 g_bonusInitialCode0 = packed_bonus_initials & 0x3f;
84 g_bonusInitialCode1 = packed_bonus_initials >> 6 & 0x3f;
85 g_bonusInitialCode2 = packed_bonus_initials >> 0xc & 0xf;
86}
87
88// Original pcdogs.exe: 0x427110
89// Returns the campaign progress ordinal for level identifiers 7 through 26, or -1 outside that range.
90int32_t Level_GetCampaignProgressOrdinal(int32_t level_id)
91{
92 if (level_id < LEVEL_CAMPAIGN_FIRST || level_id >= LEVEL_BONUS_FIRST)
94
95 return level_id - 6;
96}
97
98// Original pcdogs.exe: 0x4271b0
99// Resets current-level progress and restores its saved score, completion flags, and bonus data when the level is eligible.
101{
102 g_backupLives = g_collectiblesData.lives_backup;
106
107 int32_t campaign_ordinal = Level_GetCampaignProgressOrdinal(g_currentLevelID);
108 int32_t save_index = Level_GetSaveArrayIndex((int32_t)g_currentLevelID);
109 int16_t level_id = g_currentLevelID;
110
111 if (level_id >= LEVEL_CAMPAIGN_FIRST && level_id < LEVEL_BONUS_FIRST && level_id != LEVEL_WORLD_ONE_BOSS && level_id != LEVEL_WORLD_TWO_BOSS && level_id != LEVEL_WORLD_THREE_BOSS && level_id != LEVEL_WORLD_FOUR_BOSS)
112 {
113 if ((int32_t)g_collectiblesData.max_world_reached < campaign_ordinal - 1)
114 return;
115
116 uint8_t saved_best_score = g_collectiblesData.level_best_scores[save_index];
117
118 g_currentLevelScore = saved_best_score;
119
120 if (saved_best_score == LEVEL_PERFECT_SCORE)
121 {
124 }
125
126 uint8_t saved_completion_flags = g_collectiblesData.level_completion_flags[save_index];
127
128 g_currentLevelCompletionBits = saved_completion_flags;
129
130 if (saved_completion_flags)
131 {
132 if (saved_completion_flags == LEVEL_COMPLETION_ALL)
134
135 for (size_t i = 0; i < sizeof(g_menuItems) / sizeof(g_menuItems[0]); ++i)
136 {
137 if (saved_completion_flags & (1u << i))
138 {
139 g_menuItems[i] = 1;
141 }
142 }
143 }
144
145 char saved_bonus_flags = g_collectiblesData.level_bonus_item_flags[save_index];
146
147 if (saved_bonus_flags)
148 {
149 for (size_t i = 0; i < sizeof(g_bonusProgressVariableOffsets); ++i)
150 {
151 if (saved_bonus_flags & (1u << i))
153 }
154 }
155
156 return;
157 }
158
159 if (level_id == LEVEL_WORLD_ONE_BOSS || level_id == LEVEL_WORLD_TWO_BOSS || level_id == LEVEL_WORLD_THREE_BOSS || level_id == LEVEL_WORLD_FOUR_BOSS)
160 {
161 if ((int32_t)g_collectiblesData.max_world_reached > campaign_ordinal - 1)
163 }
164 else if (level_id < LEVEL_BONUS_FIRST || level_id >= LEVEL_AFTER_BONUS)
165 {
166 if (level_id == LEVEL_WORLD_HUB)
167 {
168 int32_t highest_menu_ordinal = Save_GetGameHighestWorld();
169
170 if (highest_menu_ordinal > LEVEL_CAMPAIGN_LAST_MENU_ORDINAL)
171 highest_menu_ordinal = LEVEL_CAMPAIGN_LAST_MENU_ORDINAL;
172
173 g_menuItems[0] = highest_menu_ordinal;
174
175 return;
176 }
177
178 if (level_id == LEVEL_AFTER_BONUS)
179 {
182
188 }
189 }
190 else
191 {
192 Level_InitializeBonusData((int32_t)level_id, save_index);
193
195 {
196 int32_t saved_best_time = (uint16_t)(g_collectiblesData.level_best_time);
197
198 g_menuItems[0] = saved_best_time;
199 }
200 }
201}
202
203// Original pcdogs.exe: 0x4273d0
204// Builds the four campaign-world completion masks and the final aggregate mask from saved progress, returning whether every completion requirement is met.
206{
207 int32_t *world_masks[] = {
209 unsigned perfect_levels = 0;
210 unsigned all_collectible_levels = 0;
211 bool worlds_complete = true;
212
213 for (unsigned world = 0; world < 4; ++world)
214 {
215 uint32_t world_mask = 0;
216 for (unsigned stage = 0; stage < 4; ++stage)
217 {
218 unsigned index = world * 4 + stage;
219 unsigned perfect = g_collectiblesData.level_best_scores[index] == LEVEL_PERFECT_SCORE;
220 unsigned collected = g_collectiblesData.level_completion_flags[index] == LEVEL_COMPLETION_ALL;
221 perfect_levels += perfect;
222 all_collectible_levels += collected;
223
224 uint32_t bonus = (uint8_t)g_collectiblesData.level_bonus_item_flags[index] & 15;
225 uint32_t flags = perfect | (collected << 1) | (bonus << 2);
226 unsigned rotation = (world + stage) & 3;
227 uint32_t rotated = ((flags << rotation) | (flags >> (6 - rotation))) & 63;
228 world_mask |= rotated << (stage * 6);
229 }
230 *world_masks[world] = world_mask;
231 if (world_mask != LEVEL_WORLD_COMPLETION_ALL)
232 worlds_complete = false;
233 }
234
235 int32_t completed_worlds = (int8_t)g_collectiblesData.max_world_reached / 5;
236 uint32_t final_mask = (1u << ((uint32_t)completed_worlds & 31)) - 1;
237 if (perfect_levels == LEVEL_CAMPAIGN_SAVE_COUNT)
239 if (all_collectible_levels == LEVEL_CAMPAIGN_SAVE_COUNT)
241 g_world4CompletionBits = final_mask;
242 return worlds_complete && final_mask == LEVEL_COMPLETION_ALL;
243}
244
245// Original pcdogs.exe: 0x428b90
246// Finds an active world bonus checkpoint and records its traced respawn entity when it differs from the current checkpoint.
247void Level_CheckBonusUnlock(struct Math_Vec3I32 *player_position)
248{
249 for (size_t i = 0; i < sizeof(g_worldBonusStateArray) / sizeof(g_worldBonusStateArray[0]); ++i)
250 {
252 continue;
253 if (g_bonusCheckpointIndex1Based != (int32_t)i + 1)
254 {
255 struct Entity entity;
256 entity.runtime_actor = NULL;
257 entity.position_ptr = player_position;
258 entity.bonus_respawn_mode = 0;
259 int32_t selector = Actor_TracePath(&entity, LEVEL_BONUS_UNLOCK_COMMAND_TARGET,
260 INT32_MAX, INT32_MAX, NAVIGATION_COMMAND_DEFAULT_SELECTOR);
261 if (selector)
262 {
263 g_bonusRespawnEntitySelector = (int16_t)selector;
264 g_bonusCheckpointIndex1Based = (char)(i + 1);
265 }
266 }
267 break;
268 }
269}
270
271// Original pcdogs.exe: 0x428c20
272// Marks the current bonus checkpoint for reset and returns its respawn selector, or initializes the first bonus state and returns -1 when no checkpoint is active.
274{
275 int32_t checkpoint = g_bonusCheckpointIndex1Based;
276
277 if (!checkpoint)
278 {
280
281 return -1;
282 }
283
285
286 return (int32_t)g_bonusRespawnEntitySelector;
287}
288
289// Original pcdogs.exe: 0x428c80
290// Calculates save completion from campaign advancement, scores, collectibles, and bonus flags. Only an exact score of 800 returns 100 percent; other positive scores are bounded below full completion.
292{
293 int32_t score = (int8_t)save->max_world_reached * 20;
294
295 for (unsigned level = 0; level < 16; level++)
296 {
299
300 if (save->level_best_scores[level] == LEVEL_PERFECT_SCORE)
302
303 for (unsigned bit = 1; bit < 4; bit++)
304 if (save->level_bonus_item_flags[level] & (1u << bit))
306 }
307
310
311 int32_t percent = score / LEVEL_PROGRESS_POINTS_PER_PERCENT;
312
313 if (!percent && score)
314 return 1;
315
317}
int32_t Actor_TracePath(struct Entity *entity, int32_t target_selector, int32_t max_path_distance, int32_t best_path_distance, int32_t trace_flags)
#define g_bonusRespawnEntitySelector
Definition globals.h:24
@ LEVEL_BONUS_CHECKPOINT_RESET
Definition constants.h:134
@ LEVEL_BONUS_CHECKPOINT_FIRST
Definition constants.h:135
#define LEVEL_START_PUPPY_COUNT
Definition constants.h:112
#define LEVEL_CAMPAIGN_LAST_MENU_ORDINAL
Definition constants.h:113
#define LEVEL_CAMPAIGN_SAVE_COUNT
Definition constants.h:23
@ LEVEL_CAMPAIGN_FIRST
Definition constants.h:85
@ LEVEL_AFTER_BONUS
Definition constants.h:95
@ LEVEL_BONUS_SECOND
Definition constants.h:91
@ LEVEL_WORLD_TWO_BOSS
Definition constants.h:87
@ LEVEL_WORLD_HUB
Definition constants.h:84
@ LEVEL_BONUS_FIFTH
Definition constants.h:94
@ LEVEL_WORLD_ONE_BOSS
Definition constants.h:86
@ LEVEL_WORLD_FOUR_BOSS
Definition constants.h:89
@ LEVEL_BONUS_FIRST
Definition constants.h:90
@ LEVEL_BONUS_THIRD
Definition constants.h:92
@ LEVEL_WORLD_THREE_BOSS
Definition constants.h:88
@ LEVEL_BONUS_UNLOCK_COMMAND_TARGET
Definition constants.h:141
#define LEVEL_PROGRESS_POINTS_PER_PERCENT
Definition constants.h:147
#define LEVEL_FINAL_WORLD_ALL_COLLECTIBLES
Definition constants.h:20
#define LEVEL_COMPLETION_ALL
Definition constants.h:18
#define LEVEL_INVALID_SAVE_INDEX
Definition constants.h:114
#define LEVEL_PROGRESS_INCOMPLETE_MAX_PERCENT
Definition constants.h:149
#define LEVEL_PROGRESS_COMPLETE_POINTS
Definition constants.h:146
#define LEVEL_WORLD_COMPLETION_ALL
Definition constants.h:21
#define LEVEL_PROGRESS_AWARD_POINTS
Definition constants.h:145
#define LEVEL_PERFECT_SCORE
Definition constants.h:22
#define LEVEL_PROGRESS_COMPLETE_PERCENT
Definition constants.h:148
#define LEVEL_FINAL_WORLD_PERFECT_SCORES
Definition constants.h:19
#define g_sz_Level_BonusTOB
Definition globals.h:34
#define g_sz_Level_BonusPR3
Definition globals.h:37
#define g_bonusLevelUnlocked
Definition globals.h:109
static int32_t * Level_BonusProgressVariable(size_t bonus)
Definition globals.h:28
#define g_currentLevelCompletionBits
Definition globals.h:61
#define g_sz_Level_BonusRFF
Definition globals.h:40
#define g_bonusProgressVariableOffsets
Definition globals.h:24
#define g_currentLevelScore
Definition globals.h:64
#define g_levelStartPuppyCount
Definition globals.h:58
#define g_currentLevelID
Definition globals.h:43
int32_t Level_GetCampaignProgressOrdinal(int32_t level_id)
Definition progress.c:90
int32_t Level_GetSaveArrayIndex(int32_t level_id)
Definition progress.c:30
int32_t Level_PassthroughIndex(int32_t level_id)
Definition progress.c:23
void Level_CheckBonusUnlock(struct Math_Vec3I32 *player_position)
Definition progress.c:247
bool Level_BuildCompletionTable()
Definition progress.c:205
void Level_InitializeBonusData(int32_t level_id, int32_t save_index)
Definition progress.c:51
void Level_InitializeSaveState()
Definition progress.c:100
int32_t Level_CalculateCompletionPercent(struct Save_GameSlot *save)
Definition progress.c:291
int32_t Level_ResetBonusState()
Definition progress.c:273
#define g_menuItems
Definition globals.h:162
#define NAVIGATION_COMMAND_DEFAULT_SELECTOR
Definition constants.h:11
int32_t Save_GetGameHighestWorld(void)
Definition progress.c:142
#define g_collectiblesData
Definition globals.h:21
Definition types.h:22
uint8_t bonus_respawn_mode
Definition types.h:39
struct Math_Vec3I32 * position_ptr
Definition types.h:78
struct Actor * runtime_actor
Definition types.h:107
uint8_t level_completion_flags[0x10]
Definition types.h:18
uint8_t level_bonus_item_flags[0x10]
Definition types.h:19
char max_world_reached
Definition types.h:12
uint8_t level_best_scores[0x10]
Definition types.h:20
#define g_dynamicStringSlot0
Definition globals.h:33
#define g_bonusInitialCode1
Definition values.h:858
#define g_savedWorld2CompletionBits
Definition values.h:417
#define g_savedWorld4CompletionBits
Definition values.h:423
#define g_world4CompletionBits
Definition values.h:852
#define g_world0CompletionBits
Definition values.h:840
#define g_bonusCheckpointIndex1Based
Definition values.h:405
#define g_world2CompletionBits
Definition values.h:846
#define g_backupLives
Definition values.h:426
#define g_savedWorld3CompletionBits
Definition values.h:420
#define g_bonusInitialCode0
Definition values.h:855
#define g_submenuCount
Definition values.h:822
#define g_world1CompletionBits
Definition values.h:843
#define g_savedWorld0CompletionBits
Definition values.h:411
#define g_bonusInitialCode2
Definition values.h:861
#define g_worldBonusStateArray
Definition values.h:828
#define g_world3CompletionBits
Definition values.h:849
#define g_savedWorld1CompletionBits
Definition values.h:414