-
Notifications
You must be signed in to change notification settings - Fork 2
/
WatchDue.c
418 lines (344 loc) · 12.3 KB
/
WatchDue.c
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
/*
Little Dues
A little simple tamagotchi like game for Mediatek smartwatches.
This is an experiment & mainly just a POC.
Everything is a static method because that helps the compiler reduce the size of nuklear.
The game is simple enough that that does not matter much.
Also no memory allocations (outside of the few nuklear might do).
Version: Super rushed & jumbled mess to test v0.0.2
- Changes:
0.0.2: Added packed sprites, lilac dragon, & slowed time.
*/
#include "vmsys.h"
#include "vmio.h"
#include "vmgraph.h"
#include "vmchset.h"
#include "vmstdlib.h"
#include "ResID.h"
#include "vm4res.h"
#include "vmtimer.h"
#include "macro_utils.h"
#include "undef_stdlib.h"
// #define DEBUG
#define SUPPORT_BG
#if 0
#define SPAM(a) printf a
#else
#define SPAM(a) (void)0
#endif
VMINT layer_hdl[1];
typedef enum {true = 1, false = 0} bool;
//////////// PET SPRITES, STATE, & LOGIC
#define PET_ENTRIES 3
#define ITEM_ENTRIES 9
#define SPRITES_SETUP
#include "sprites.h"
#define PET_ITEMS_MAX 4
#define PET_STAT_MAX 70
#define PETS_SETUP
#include "pet_definitions.h"
#define SAVE_FILE_NAME "ld_pet.sav"
VMWCHAR wide_save_file_name[MRE_FILE_NAME_SIZE];
static struct
current_pet_state {
VMUINT8 mood_trait, hunger_trait;
VMUINT8 mood;
VMUINT8 hunger;
VMUINT8 glitch;
VMINT items[PET_ITEMS_MAX];
float age;
bool dead;
struct WatchDuePet const * def;
struct SpritePtr sprite;
VMINT fed_item;
} current_pet_state;
static int pet_sprite_index = 0;
static void
update_pet_frame(VMINT tid) {
// Assume pet sprites are always packed.
struct SpritePacked const * ps = current_pet_state.sprite.ptr.packed_sprite;
if (!current_pet_state.dead) {
pet_sprite_index = ++pet_sprite_index % ps->frame_count;
} else {
// Dead frame is stored directly after the animation frames.
pet_sprite_index = ps->frame_count;
}
}
static void
save_pet_data(void) {
VMINT file_hdl;
VMUINT written;
VMUINT save_time;
file_hdl = vm_file_open(wide_save_file_name, MODE_CREATE_ALWAYS_WRITE, true);
if (file_hdl < 0)
return; // error (not much can be done)
(void) vm_get_curr_utc(&save_time);
vm_file_write(file_hdl, (VMINT*)¤t_pet_state.def->id, sizeof(VMINT), &written);
vm_file_write(file_hdl, ¤t_pet_state.mood, sizeof(VMUINT8), &written);
vm_file_write(file_hdl, ¤t_pet_state.mood_trait, sizeof(VMUINT8), &written);
vm_file_write(file_hdl, ¤t_pet_state.hunger, sizeof(VMUINT8), &written);
vm_file_write(file_hdl, ¤t_pet_state.hunger_trait, sizeof(VMUINT8), &written);
vm_file_write(file_hdl, ¤t_pet_state.age, sizeof(float), &written);
vm_file_write(file_hdl, &save_time, sizeof(VMUINT), &written);
vm_file_write(file_hdl, current_pet_state.items, PET_ITEMS_MAX * sizeof(VMINT), &written);
vm_file_write(file_hdl, ¤t_pet_state.dead, sizeof(bool), &written);
vm_file_write(file_hdl, ¤t_pet_state.glitch, sizeof(VMUINT8), &written);
vm_file_close(file_hdl);
}
#define TWENTY_MINS (60 * 20)
static void apply_time_to_game(VMUINT save_time) {
VMUINT current_time;
VMUINT time_diff;
float time_attrition;
VMINT mood_loss, hunger_increase, drop_chances, drop;
(void) vm_get_curr_utc(¤t_time); // assume the clock works
// Pet aging
time_diff = current_time - save_time;
current_pet_state.age += (float)time_diff/60/60; // pet age is in hours
time_attrition = (time_diff/2 + rand() % time_diff)/60;
// Gives about 10 hours until the pet will die
mood_loss = (VMINT)(time_attrition * (float)current_pet_state.mood_trait/1000);
hunger_increase = (VMINT)(time_attrition * (float)current_pet_state.hunger_trait/1000);
if (mood_loss < current_pet_state.mood)
current_pet_state.mood -= mood_loss;
else
current_pet_state.mood = 0;
if (hunger_increase < current_pet_state.hunger)
current_pet_state.hunger -= hunger_increase;
else
current_pet_state.hunger = 0;
if (current_pet_state.hunger < 5 && current_pet_state.mood < 5) {
current_pet_state.dead = rand() % 100 < 80; // 80% chance of death
} else if (current_pet_state.mood < 5) {
// suicide check
current_pet_state.dead = rand() % current_pet_state.mood_trait * 3 < 40;
} else if (current_pet_state.hunger < 5) {
// stave check
current_pet_state.dead = rand() % current_pet_state.hunger_trait * 2 < 50;
}
// Item drops
// Chance every 5 mins
drop_chances = time_diff/TWENTY_MINS;
for (drop = 0; drop < drop_chances; drop++) {
VMINT8 slot;
VMINT dropped_item = 0;
for (slot = 0; slot < PET_ITEMS_MAX; slot++) {
VMINT item_id = current_pet_state.def->acceptable_foods[slot];
if (dropped_item) break;
dropped_item = item_id * ((rand() % 101) < PET_ITEMS[item_id].drop_chance);
}
if (!dropped_item) {
continue;
}
for (slot = 0; slot < PET_ITEMS_MAX; slot++) {
if (current_pet_state.items[slot] == 0) {
current_pet_state.items[slot] = dropped_item;
break;
} else if (slot == PET_ITEMS_MAX-1) {
drop_chances = 0;
}
}
}
}
static void load_or_create_pet(void) {
VMINT file_hdl;
VMUINT read;
VMUINT save_time;
VMINT pet_id;
current_pet_state.fed_item = 0;
file_hdl = vm_file_open(wide_save_file_name, MODE_READ, true);
if (file_hdl < 0) {
// No pet data found.
// Create new pet state
current_pet_state.def = &PET_DB[rand() % PET_ENTRIES];
current_pet_state.age = 0;
current_pet_state.mood = PET_STAT_MAX;
current_pet_state.hunger = PET_STAT_MAX;
current_pet_state.mood_trait = RANDRANGE(1, 100);
current_pet_state.hunger_trait = RANDRANGE(1, 100);
current_pet_state.dead = false;
current_pet_state.glitch = 0;
if (RAND(100) < 5) {
current_pet_state.glitch = RAND(255) + 1;
}
memset(current_pet_state.items, 0, PET_ITEMS_MAX * sizeof(VMINT));
current_pet_state.sprite = current_pet_state.def->sprite;
return;
}
vm_file_read(file_hdl, &pet_id, sizeof(VMINT), &read);
vm_file_read(file_hdl, ¤t_pet_state.mood, sizeof(VMUINT8), &read);
vm_file_read(file_hdl, ¤t_pet_state.mood_trait, sizeof(VMUINT8), &read);
vm_file_read(file_hdl, ¤t_pet_state.hunger, sizeof(VMUINT8), &read);
vm_file_read(file_hdl, ¤t_pet_state.hunger_trait, sizeof(VMUINT8), &read);
vm_file_read(file_hdl, ¤t_pet_state.age, sizeof(float), &read);
vm_file_read(file_hdl, &save_time, sizeof(VMUINT), &read);
vm_file_read(file_hdl, current_pet_state.items, PET_ITEMS_MAX * sizeof(VMINT), &read);
vm_file_read(file_hdl, ¤t_pet_state.dead, sizeof(bool), &read);
vm_file_read(file_hdl, ¤t_pet_state.glitch, sizeof(VMUINT8), &read);
vm_file_close(file_hdl);
current_pet_state.def = &PET_DB[pet_id];
current_pet_state.sprite = current_pet_state.def->sprite;
SPAM(("LOADED PET!\n"));
SPAM(("ID: %d\n", pet_id));
SPAM(("MOOD: %d\n", (int)current_pet_state.mood));
SPAM(("HUNGER: %d\n", (int)current_pet_state.hunger));
SPAM(("SAVE TIME: %u\n", save_time));
SPAM(("ITEMS: [%d, %d, %d, %d]\n", current_pet_state.items[0], current_pet_state.items[1],
current_pet_state.items[2], current_pet_state.items[3]));
if (!current_pet_state.dead)
apply_time_to_game(save_time);
if (current_pet_state.dead) {
current_pet_state.mood = current_pet_state.hunger = 0;
update_pet_frame(0); // make sure the frame changes to the dead frame
}
}
static void
feed_pet(void) {
if (!current_pet_state.fed_item) return;
current_pet_state.hunger += PET_ITEMS[current_pet_state.fed_item].nutrition;
if (current_pet_state.hunger > PET_STAT_MAX)
current_pet_state.hunger = PET_STAT_MAX;
current_pet_state.fed_item = 0;
}
static void
play_with_pet(void) {
current_pet_state.mood += rand() % (current_pet_state.mood_trait/10 +1) +1;
if (current_pet_state.mood > PET_STAT_MAX)
current_pet_state.mood = PET_STAT_MAX;
}
static void
glitch_pet_sprite(VMUINT8* res_data) {
// glitch_pet_resource(res_data, current_pet_state.glitch);
}
//////////// TIMERS
/*
#define MAX_TIMERS 10
enum timer_type {FG_TIMER, BG_TIMER};
static struct timer {
VM_TIMERPROC_T func;
VMINT delay;
VMINT tid;
};
static struct timers {
struct timer foreground[MAX_TIMERS];
struct timer background[MAX_TIMERS];
} game_timers;
static struct timer
game_timer(VM_TIMERPROC_T func, VMINT delay) {
struct timer timer;
timer.func = func;
timer.delay = delay;
timer.tid = -1;
return timer;
}
static bool
create_timer(struct timer timer, enum timer_type type, VMINT slot) {
struct timer* slots = type == BG_TIMER ? game_timers.background : game_timers.foreground;
if (slots[slot].func != NULL)
return false;
slots[slot] = timer;
slots[slot].tid = type == FG_TIMER ? vm_create_timer(timer.delay, timer.func) : -1;
if (slots[slot].tid < 0) {
slots[slot].func = NULL;
return false;
}
return true;
}
static void
pause_timers(enum timer_type type) {
int t;
struct timer* slots = type == BG_TIMER ? game_timers.background : game_timers.foreground;
for (t = 0; t < MAX_TIMERS; t++) {
if (slots[t].func && slots[t].tid > 0) {
vm_delete_timer(slots[t].tid);
slots[t].tid = -1;
}
}
}
static void
resume_timers(enum timer_type type) {
int t;
struct timer* slots = type == BG_TIMER ? game_timers.background : game_timers.foreground;
for (t = 0; t < MAX_TIMERS; t++) {
if (slots[t].func && slots[t].tid == -1) {
slots[t].tid = vm_create_timer(slots[t].delay, slots[t].func);
}
}
}
*/
//////////// NUKLEAR SETUP
static void
game_sys_event_handler(VMINT message, VMINT param);
/* the (old) compiler seems much better at producing a small binary with static methods */
#define NK_PRIVATE
#define NK_IMPLEMENTATION
#define NK_BUTTON_TRIGGER_ON_RELEASE
#include "nuklear_mre.h"
static nk_size zero = 0;
static struct nk_user_font watch_font;
static struct nk_color bg_color = {20, 20, 20, 255};
static struct nk_color btn_color_main = {218, 59, 89, 255};
static void
nk_button_transparent(struct nk_context* ctx) {
ctx->style.button.normal = nk_style_item_color(nk_rgba(0,0,0,0));
ctx->style.button.hover = nk_style_item_color(nk_rgba(0,0,0,0));
ctx->style.button.active = nk_style_item_color(nk_rgba(0,0,0,0));
ctx->style.button.border_color = nk_rgba(0,0,0,0);
}
static void
nk_button_default(struct nk_context* ctx) {
ctx->style.button.normal = nk_style_item_color(nk_default_color_style[NK_COLOR_BUTTON]);
ctx->style.button.hover = nk_style_item_color(nk_default_color_style[NK_COLOR_BUTTON_HOVER]);
ctx->style.button.active = nk_style_item_color(nk_default_color_style[NK_COLOR_BUTTON_ACTIVE]);
ctx->style.button.border_color = nk_default_color_style[NK_COLOR_BORDER];
}
/* everything is defined within headers since nk must be static
or the ancient arm compiler will keep a load of bloat */
#define VIEWS_SETUP
#include "views.h"
float calc_watch_font_width(nk_handle _, float h, const char* str, int len) {
VMWCHAR wide_str[MRE_STR_SIZE_MAX];
vm_gb2312_to_ucs2(wide_str, MRE_STR_SIZE_MAX, (VMSTR)str);
wide_str[len] = '\0';
return (float) vm_graphic_get_string_width(wide_str);
}
static void
game_sys_event_handler(VMINT message, VMINT param) {
if (message == VM_MSG_QUIT) {
save_pet_data();
}
}
//void hack_watch_update() {
//}
void vm_main(void) {
VMUINT time;
vm_gb2312_to_ucs2(wide_save_file_name, MRE_FILE_NAME_SIZE, (VMSTR)SAVE_FILE_NAME);
if(!vm_get_curr_utc(&time)) {
vm_log_fatal("Time is broken");
}
srand(time);
init_sprites();
init_pets();
init_views();
vm_res_init();
//memset(game_timers, 0, sizeof (struct timers));
load_or_create_pet();
//create_timer(game_timer(update_pet_frame, 200), FG_TIMER, 0);
vm_create_timer(200, update_pet_frame);
watch_font.height = (float) vm_graphic_get_character_height();
watch_font.width = calc_watch_font_width;
nk_mre_init(&watch_font, vm_graphic_get_screen_width(), vm_graphic_get_screen_height());
// All the window padding does not make sense on a tiny watch
mre.ctx.style.window.spacing = nk_vec2(0,0);
mre.ctx.style.window.scaler_size = nk_vec2(0,0);
mre.ctx.style.window.footer_padding = nk_vec2(0,0);
mre.ctx.style.window.padding = nk_vec2(0,0);
mre.ctx.style.window.header.label_padding = nk_vec2(0,0);
mre.ctx.style.window.header.padding = nk_vec2(0,0);
// The the background colours
mre.ctx.style.window.fixed_background.data.color = bg_color;
mre.ctx.style.progress.normal.data.color = bg_color;
// Goto the main view of your pet
nk_mre_set_view(&mre.ctx,&view_watch_due_main);
// ((void (*) ()) 0x102ED558) (14821, hack_watch_update);
}