pt1/4
Code:
// LivinGrimoire.HC
// HolyC port of LivinGrimoire.py
// Runs on TempleOS
// OOP replaced with structs + function pointers (composition)
// Caps: MAX_SKILLS=16, MAX_SENTENCES=32, MAX_ALG_PARTS=16, NEURON_QUEUE=4
#define MAX_SKILLS 16
#define MAX_SENTENCES 32
#define MAX_ALG_PARTS 16
#define NEURON_QUEUE 4
#define DEFCON_LEVELS 5
// ─────────────────────────────────────────────
// AbsDictionaryDB
// ─────────────────────────────────────────────
class AbsDictionaryDB
{
U0 (*Save)(AbsDictionaryDB *self, U8 *key, U8 *value);
U8 *(*Load)(AbsDictionaryDB *self, U8 *key);
};
U0 AbsDictionaryDB_Save_Default(AbsDictionaryDB *self, U8 *key, U8 *value)
{
// override in subcomposition
}
U8 *AbsDictionaryDB_Load_Default(AbsDictionaryDB *self, U8 *key)
{
return "null";
}
U0 AbsDictionaryDB_Init(AbsDictionaryDB *self)
{
self->Save = &AbsDictionaryDB_Save_Default;
self->Load = &AbsDictionaryDB_Load_Default;
}
// ─────────────────────────────────────────────
// AlgPart
// ─────────────────────────────────────────────
class AlgPart
{
Bool algKillSwitch;
U8 customName[64];
U8 *(*Action)(AlgPart *self, U8 *ear, U8 *skin, U8 *eye);
Bool (*Completed)(AlgPart *self);
};
U8 *AlgPart_Action_Default(AlgPart *self, U8 *ear, U8 *skin, U8 *eye)
{
return "";
}
Bool AlgPart_Completed_Default(AlgPart *self)
{
return TRUE;
}
U0 AlgPart_Init(AlgPart *self, U8 *name)
{
self->algKillSwitch = FALSE;
StrCpy(self->customName, name);
self->Action = &AlgPart_Action_Default;
self->Completed = &AlgPart_Completed_Default;
}
U0 AlgPart_SetName(AlgPart *self, U8 *name)
{
StrCpy(self->customName, name);
}
U8 *AlgPart_MyName(AlgPart *self)
{
return self->customName;
}
// ─────────────────────────────────────────────
// APVerbatim (composes AlgPart)
// ─────────────────────────────────────────────
class APVerbatim
{
AlgPart base;
U8 *sentences[MAX_SENTENCES];
I64 count;
I64 head; // queue head index
};
U8 *APVerbatim_Action(AlgPart *base, U8 *ear, U8 *skin, U8 *eye)
{
APVerbatim *self = (APVerbatim *)base;
if (self->head >= self->count)
return "";
U8 *s = self->sentences[self->head];
self->head++;
return s;
}
Bool APVerbatim_Completed(AlgPart *base)
{
APVerbatim *self = (APVerbatim *)base;
return self->head >= self->count;
}
U0 APVerbatim_Init(APVerbatim *self)
{
AlgPart_Init(&self->base, "APVerbatim");
self->base.Action = &APVerbatim_Action;
self->base.Completed = &APVerbatim_Completed;
self->count = 0;
self->head = 0;
}
U0 APVerbatim_Add(APVerbatim *self, U8 *sentence)
{
if (self->count < MAX_SENTENCES)
{
self->sentences[self->count] = sentence;
self->count++;
}
}
// ─────────────────────────────────────────────
// Algorithm
// ─────────────────────────────────────────────
class Algorithm
{
AlgPart *parts[MAX_ALG_PARTS];
I64 size;
};
U0 Algorithm_Init(Algorithm *self)
{
self->size = 0;
I64 i;
for (i = 0; i < MAX_ALG_PARTS; i++)
self->parts[i] = NULL;
}
U0 Algorithm_AddPart(Algorithm *self, AlgPart *part)
{
if (self->size < MAX_ALG_PARTS)
{
self->parts[self->size] = part;
self->size++;
}
}
I64 Algorithm_GetSize(Algorithm *self)
{
return self->size;
}
// ─────────────────────────────────────────────
// Kokoro
// ─────────────────────────────────────────────
class Kokoro
{
U8 emot[128];
AbsDictionaryDB *grimoireMemento;
// toHeart omitted: no hash map in TempleOS stdlib;
// extend with a flat key/val array if needed
};
U0 Kokoro_Init(Kokoro *self, AbsDictionaryDB *db)
{
self->emot[0] = 0;
self->grimoireMemento = db;
}
U8 *Kokoro_GetEmot(Kokoro *self)
{
return self->emot;
}
U0 Kokoro_SetEmot(Kokoro *self, U8 *emot)
{
StrCpy(self->emot, emot);
}
// ─────────────────────────────────────────────
// Neuron
// ─────────────────────────────────────────────
class Neuron
{
// defcons[1..5], each a small queue of Algorithm pointers
Algorithm *defcons[DEFCON_LEVELS + 1][NEURON_QUEUE];
I64 defcon_count[DEFCON_LEVELS + 1];
};
U0 Neuron_Init(Neuron *self)
{