Digimon World 3 decomp
The European version, SLES_039.36
Loading...
Searching...
No Matches
task.h
Go to the documentation of this file.
1#ifndef DW3_TASK_H
2#define DW3_TASK_H
3
4/* The task system and its registry (task/) */
5
6#include "common.h"
7#include <sys/types.h>
8#include <libgte.h>
9#include <libgpu.h>
10
11struct Task;
12
13/* The task registry's methods (TASK_REGISTRY.funcs) */
14typedef struct TaskFuncs {
15 /* 0x00 */ void (*clear)(void);
16 /* 0x04 */ void (*add)(struct Task *task);
17 /* 0x08 */ void (*remove)(struct Task *task);
18 /* 0x0C */ void *(*find)(s32 id, s32 key1, s32 key2);
19 /* 0x10 */ void *(*findNext)(void);
20 /* 0x14 */ void (*runChildren)(struct Task *task);
21 /* 0x18 */ struct Task *(*run)(struct Task *task);
22 /* 0x1C */ void (*kill)(struct Task *task);
23} TaskFuncs;
24
25/*
26 * Tasks: every game object is a task, a heap block that starts with this
27 * header and is updated once per frame (createTask). A task owns an array of
28 * child tasks (windows, cursors...), which run right after it, and it gets
29 * that array as the second argument of its update. The state machine is
30 * driven through the header: TASK_INIT runs once (usually just nextState),
31 * TASK_RUN is the normal state, and TASK_KILL frees the task (and kills its
32 * children) after its last update.
33 */
34#define TASK_INIT 0
35#define TASK_RUN 1
36#define TASK_DONE 2
37#define TASK_KILL 3
38
39#define TASK_HEADER(Type) \
40 /* 0x00 */ s32 id; /* registered tasks only: findTask matches these */ \
41 /* 0x04 */ s32 key1; \
42 /* 0x08 */ s32 key2; \
43 /* 0x0C */ s32 state; \
44 /* 0x10 */ s32 substate; \
45 /* 0x14 */ s32 step; \
46 /* 0x18 */ s32 counter; \
47 /* 0x1C */ s32 paused; /* in TASK_RUN: skip the update and children */ \
48 /* 0x20 */ s32 childCount; \
49 /* 0x24 */ void *children; \
50 /* 0x28 */ void (*setState)(struct Type *task, s32 state); \
51 /* 0x2C */ void (*setSubstate)(struct Type *task, s32 substate); \
52 /* 0x30 */ void (*setStep)(struct Type *task, s32 step); \
53 /* 0x34 */ void (*setCounter)(struct Type *task, s32 counter); \
54 /* 0x38 */ void (*nextState)(struct Type *task); \
55 /* 0x3C */ void (*nextSubstate)(struct Type *task); \
56 /* 0x40 */ void (*nextStep)(struct Type *task); \
57 /* 0x44 */ void (*tickCounter)(struct Type *task); \
58 /* 0x48 */ void (*update)(); /* (task, children) */ \
59 /* 0x4C */ void (*destroy)(struct Type *task)
60
61/* Any task, seen through its header */
62typedef struct Task {
64} Task;
65
66/* The tasks the registry holds at most */
67#define TASK_REGISTRY_SIZE 100
68
69/* Tasks created with an id, so that other code can find them */
70typedef struct TaskRegistry {
71 /* 0x000 */ Task *tasks[TASK_REGISTRY_SIZE]; /* NULL for a free entry */
72 /* 0x190 */ s32 findId; /* -1 matches anything */
73 /* 0x194 */ s32 findKey1;
74 /* 0x198 */ s32 findKey2;
75 /* 0x19C */ s32 findNext;
76 /* 0x1A0 */ TaskFuncs funcs;
78
79void *createTask(void (*update)(), s32 size, s32 childrenSize);
80void taskSetState(Task *task, s32 state);
81void taskSetSubstate(Task *task, s32 substate);
82void taskSetStep(Task *task, s32 step);
83void taskSetCounter(Task *task, s32 counter);
84void taskNextState(Task *task);
85void taskNextSubstate(Task *task);
86void taskNextStep(Task *task);
87void taskTickCounter(Task *task);
88void destroyTask(Task *task);
89void *createTaskWithId(void (*update)(), s32 size, s32 childrenSize, s32 id);
90struct Task *executeTask(struct Task *task);
91void runChildTasks(Task *task);
92Task *runTask(Task *task);
93void killTask(Task *task);
94void clearTaskRegistry(void);
95void registerTask(Task *task);
96void unregisterTask(Task *task);
97void *findNextTask(void);
98void *findTask(s32 id, s32 key1, s32 key2);
99
101
102#endif /* DW3_TASK_H */
signed int s32
Definition common.h:11
void clearTaskRegistry(void)
Forgets every registered task.
Definition registry.c:4
void taskNextStep(Task *task)
Task method: goes to the next step, with its counter at 0.
Definition task.c:48
Task * runTask(Task *task)
Runs a frame of a task and its children; returns it, or NULL once it is gone.
Definition registry.c:107
void * createTask(void(*update)(), s32 size, s32 childrenSize)
createTaskWithId for a task nothing needs to find
Definition task.c:105
#define TASK_REGISTRY_SIZE
The tasks the registry holds at most.
Definition task.h:67
void taskTickCounter(Task *task)
Task method: counts one more.
Definition task.c:54
void * createTaskWithId(void(*update)(), s32 size, s32 childrenSize, s32 id)
Allocates a zeroed task of size bytes (header included) with room for childrenSize / 4 child tasks.
Definition task.c:80
void unregisterTask(Task *task)
Removes a task from the registry.
Definition registry.c:26
void registerTask(Task *task)
Adds a task to the first free entry of the registry (ignored when it is full)
Definition registry.c:13
void * findNextTask(void)
The next registered task that matches findTask's id and keys (-1 matches anything),...
Definition registry.c:39
void destroyTask(Task *task)
Default destructor: kills the children, then frees the task.
Definition task.c:59
TaskRegistry TASK_REGISTRY
Definition registry.c:122
void taskSetCounter(Task *task, s32 counter)
Task method: sets the counter.
Definition task.c:28
void taskSetState(Task *task, s32 state)
Task method: goes to state state, from substate 0.
Definition task.c:7
void taskSetStep(Task *task, s32 step)
Task method: goes to step step, with its counter at 0.
Definition task.c:22
struct Task * executeTask(struct Task *task)
One frame of a task and its children; returns NULL once the task is gone.
Definition registry.c:70
#define TASK_HEADER(Type)
Definition task.h:39
void taskSetSubstate(Task *task, s32 substate)
Task method: goes to substate substate, from step 0.
Definition task.c:15
void taskNextState(Task *task)
Task method: goes to the next state, from substate 0.
Definition task.c:33
void runChildTasks(Task *task)
Runs a frame of each child of a task, dropping the ones that end.
Definition registry.c:94
void killTask(Task *task)
Ends a task now: its last update and destroy run at once.
Definition registry.c:115
void * findTask(s32 id, s32 key1, s32 key2)
The first registered task that matches (-1 matches anything), or NULL.
Definition registry.c:56
void taskNextSubstate(Task *task)
Task method: goes to the next substate, from step 0.
Definition task.c:41
The task registry's methods (TASK_REGISTRY.funcs)
Definition task.h:14
void(* remove)(struct Task *task)
Definition task.h:17
void(* runChildren)(struct Task *task)
Definition task.h:20
void(* clear)(void)
Definition task.h:15
void(* kill)(struct Task *task)
Definition task.h:22
void(* add)(struct Task *task)
Definition task.h:16
Tasks created with an id, so that other code can find them.
Definition task.h:70
s32 findKey2
Definition task.h:74
Task * tasks[100]
NULL for a free entry.
Definition task.h:71
TaskFuncs funcs
Definition task.h:76
s32 findId
-1 matches anything
Definition task.h:72
s32 findKey1
Definition task.h:73
s32 findNext
Definition task.h:75
Any task, seen through its header.
Definition task.h:62
s32 key1
Definition task.h:63
s32 key2
Definition task.h:63