Compare commits

...

3 Commits

Author SHA1 Message Date
3faab5e206 basic brain 2026-03-30 16:31:19 +02:00
d2435eee7f auto resolve existance 2026-03-30 16:10:31 +02:00
5858e2808d use interaction resolve matching 2026-03-27 20:03:50 +01:00
12 changed files with 321 additions and 162 deletions

View File

@@ -1,4 +1,4 @@
<div class="bg-red-100 w-full max-w-[1024px] aspect-square">
<div class="text-lg text-primary-700">{{ chibi().state }}</div>
<div class="text-lg text-primary-700">{{ chibi().currentStateName }}</div>
<canvas class="w-full h-full" #canvas></canvas>
</div>

View File

@@ -1,7 +1,14 @@
import { Chibi, ChibiId } from "../../types/chibi/chibi";
import { Chibi, ChibiId, ChibiStatBar } from "../../types/chibi/chibi";
import { EChibiName } from "../../types/chibi/chibi-name";
import { EChibiStateName } from "../../types/chibi/chibi-state-name";
export const statBar = (current: number, max: number): ChibiStatBar => {
return {
current,
max
}
}
export const MAX_STAT_VALUE: number = 100000;
export const CHIBIS: Chibi[] = [
@@ -12,16 +19,35 @@ export const CHIBIS: Chibi[] = [
spritePath: '',
experience: 0,
unlocked: true,
happyness: MAX_STAT_VALUE,
health: MAX_STAT_VALUE,
hunger: MAX_STAT_VALUE,
boredom: MAX_STAT_VALUE,
energy: MAX_STAT_VALUE,
happyness: statBar(MAX_STAT_VALUE, MAX_STAT_VALUE),
health: statBar(MAX_STAT_VALUE, MAX_STAT_VALUE),
hunger: statBar(MAX_STAT_VALUE, MAX_STAT_VALUE),
boredom: statBar(MAX_STAT_VALUE, MAX_STAT_VALUE),
energy: statBar(MAX_STAT_VALUE, MAX_STAT_VALUE),
attentionSpan: 1000,
recoveryRate: 1000,
constitution: 1000,
power: 1000,
maintenanceCals: 0,
state: EChibiStateName.Sleeping
currentStateName: EChibiStateName.Sleeping
},
{
id: '238df3e7-3003-4471-91f5-a7ecf3151e18' as ChibiId,
name: EChibiName.Caethya,
iconPath: '',
spritePath: '',
experience: 0,
unlocked: true,
happyness: statBar(MAX_STAT_VALUE, MAX_STAT_VALUE),
health: statBar(MAX_STAT_VALUE, MAX_STAT_VALUE),
hunger: statBar(MAX_STAT_VALUE, MAX_STAT_VALUE),
boredom: statBar(MAX_STAT_VALUE, MAX_STAT_VALUE),
energy: statBar(MAX_STAT_VALUE, MAX_STAT_VALUE),
attentionSpan: 1000,
recoveryRate: 1000,
constitution: 1000,
power: 1000,
maintenanceCals: 0,
currentStateName: EChibiStateName.Idle
},
];

View File

@@ -0,0 +1,91 @@
import { Chibi } from "../../types/chibi/chibi";
import { EChibiInteraction } from "../../types/chibi/chibi-interaction";
import { ChibiState, STATES } from "../../types/chibi/chibi-state";
import { EChibiStateName } from "../../types/chibi/chibi-state-name";
import { RandomService } from "../random.service";
import { IBrain } from "./brain";
export type InteractionMap = { [key in EChibiStateName]: InteractionRecord | undefined }
export type ChibiInteraction<T = null> = {
name: EChibiInteraction;
payload: T;
}
export type InteractionResolutionFunction = (interaction: ChibiInteraction<any>) => ChibiState;
export type InteractionRecord = Partial<Record<EChibiInteraction, InteractionResolutionFunction>>;
export type ResolutionFunction = () => ChibiState | undefined;
export type ResolutionMap = { [key in EChibiStateName]: ResolutionFunction | undefined }
export abstract class BasicBrain implements IBrain {
protected randomService!: RandomService;
protected chibi!: Chibi;
protected readonly defaultState: EChibiStateName = EChibiStateName.Sleeping;
protected hasBeenAwakeFor: number = 0;
protected timeSinceLastInteraction: number = 0;
protected timeInCurrentState: number = 0;
protected abstract interactionMap: InteractionMap;
protected abstract resolutionMap: ResolutionMap;
get state(): ChibiState {
return STATES[this.chibi.currentStateName];
}
init(chibi: Chibi, randomService: RandomService): void {
this.randomService = randomService;
this.chibi = chibi;
}
exist(): void {
this.timeInCurrentState++;
const resolution = this.matchResolution(this.resolutionMap, STATES[this.chibi.currentStateName]);
if (resolution) {
const newState = resolution();
if (newState && newState != STATES[this.chibi.currentStateName]) {
this.changeState(newState);
}
}
}
resolveInteraction(interaction: ChibiInteraction): void {
this.timeSinceLastInteraction = 0;
const resolution = this.matchInteraction(this.interactionMap, interaction.name, STATES[this.chibi.currentStateName]);
console.log('resolution', resolution);
console.log(`${this.chibi.name} should ${interaction.name} with payload: ${interaction.payload}`);
if (resolution) {
this.changeState(resolution(interaction));
}
}
matchInteraction(map: InteractionMap, interaction: EChibiInteraction, state: ChibiState): InteractionResolutionFunction | undefined {
const directMatch = map?.[state.name]?.[interaction];
if (directMatch) {
return directMatch;
}
if (state.parent) {
return this.matchInteraction(map, interaction, state.parent);
}
return undefined;
}
matchResolution(map: ResolutionMap, state: ChibiState): ResolutionFunction | undefined {
const directMatch = map?.[state.name];
if (directMatch) {
return directMatch;
}
if (state.parent) {
return this.matchResolution(map, state.parent);
}
return undefined;
}
protected changeState(state: ChibiState): void {
this.chibi.currentStateName = state.name;
this.timeInCurrentState = 0;
}
};

View File

@@ -1,15 +1,20 @@
import { EChibiName } from "../../types/chibi/chibi-name";
import { IBrain } from "./brain";
import { Aperio } from "./brains/aperio.brain";
import { Caethya } from "./brains/caethya.brain";
import { Haetor } from "./brains/haetor.brain";
const APERIO = new Aperio();
const CAETHYA = new Caethya();
const HAETOR = new Haetor();
export const BRAIN_MAP: Record<EChibiName, IBrain> = {
[EChibiName.Aperio]: APERIO,
[EChibiName.Basra]: APERIO,
[EChibiName.Caethya]: APERIO,
[EChibiName.Caethya]: CAETHYA,
[EChibiName.Eos]: APERIO,
[EChibiName.Evina]: APERIO,
[EChibiName.Haetor]: HAETOR,
[EChibiName.Sol]: APERIO,
[EChibiName.Sybil]: APERIO,
[EChibiName.Vinera]: APERIO

View File

@@ -1,6 +1,6 @@
import { Chibi } from "../../types/chibi/chibi";
import { RandomService } from "../random.service";
import { ChibiInteraction } from "./brains/aperio.brain";
import { ChibiInteraction } from "./basic.brain";
export interface IBrain {

View File

@@ -1,18 +1,9 @@
import { Chibi } from "../../../types/chibi/chibi";
import { EChibiInteraction } from "../../../types/chibi/chibi-interaction";
import { ChibiState, isState, STATE_AWAKE, STATE_EATING, STATE_GRUMBLING, STATE_IDLE, STATE_SLEEPING, STATES } from "../../../types/chibi/chibi-state";
import { ChibiState, STATE_AWAKE, STATE_EATING, STATE_GRUMBLING, STATE_IDLE, STATE_SLEEPING } from "../../../types/chibi/chibi-state";
import { EChibiStateName } from "../../../types/chibi/chibi-state-name";
import { Food } from "../../../types/food";
import { RandomOutcome, RandomService } from "../../random.service";
import { IBrain } from "../brain";
export type ChibiInteraction<T = null> = {
name: EChibiInteraction;
payload: T;
}
type ResolutionFunction = (interaction: ChibiInteraction<any>) => ChibiState;
type InteractionRecord = Partial<Record<EChibiInteraction, ResolutionFunction>>;
import { RandomOutcome, } from "../../random.service";
import { BasicBrain, ChibiInteraction, InteractionMap, ResolutionMap } from "../basic.brain";
const MAX_TIME_AWAKE: number = 10;
@@ -22,22 +13,12 @@ const SADNESS_FROM_NEGLECT: number = 50;
const MAX_DURATION_GRUMBLE: number = 1;
const MAX_DURATION_EATING: number = 1;
export class Aperio implements IBrain {
export class Aperio extends BasicBrain {
private randomService!: RandomService;
private chibi!: Chibi;
private readonly defaultState: EChibiStateName = EChibiStateName.Sleeping;
private previousState: EChibiStateName = this.defaultState;
private hasBeenAwakeFor: number = 0;
private timeSinceLastInteraction: number = 0;
private timeInCurrentState: number = 0;
private lastConsumedFoods: string[] = [];
private preferredFoods: string[] = [];
private state: ChibiState = STATES[this.defaultState];
private interactionMap: { [key in EChibiStateName]: InteractionRecord | undefined } = {
protected override interactionMap: InteractionMap = {
[EChibiStateName.Sleeping]: {
[EChibiInteraction.WakeUp]: (interaction: ChibiInteraction): ChibiState => this.wakeUp(interaction),
},
@@ -56,102 +37,77 @@ export class Aperio implements IBrain {
[EChibiStateName.Depressed]: undefined,
[EChibiStateName.Tired]: undefined
}
init(chibi: Chibi, randomService: RandomService): void {
this.randomService = randomService;
this.chibi = chibi;
this.state = STATES[chibi.state];
}
exist(): void {
this.timeInCurrentState++;
this.resolveAwake();
this.resolveSleeping();
this.resolveGrumbling();
this.resolveEating();
}
resolveInteraction(interaction: ChibiInteraction): void {
this.timeSinceLastInteraction = 0;
const specificMatch: ResolutionFunction | undefined = this.interactionMap[this.state.name]?.[interaction.name];
const awakeMatch: ResolutionFunction | undefined = this.interactionMap[EChibiStateName.Awake]?.[interaction.name];
const asleepMatch: ResolutionFunction | undefined = this.interactionMap[EChibiStateName.Sleeping]?.[interaction.name];
const resolution: ResolutionFunction | undefined = specificMatch || (this.isAwake() ? awakeMatch : asleepMatch);
console.log(`specific match: ${specificMatch}`);
console.log(`${this.chibi.name} should ${interaction.name} with payload: ${interaction.payload}`);
if (resolution) {
this.changeState(resolution(interaction));
}
}
isAwake(): boolean {
return isState(this.state, EChibiStateName.Awake);
protected override resolutionMap: ResolutionMap = {
[EChibiStateName.Sleeping]: (): ChibiState | undefined => this.resolveSleeping(),
[EChibiStateName.Awake]: (): ChibiState | undefined => this.resolveAwake(),
[EChibiStateName.Snoring]: undefined,
[EChibiStateName.Nightmare]: undefined,
[EChibiStateName.Grumbling]: (): ChibiState | undefined => this.resolveGrumbling(),
[EChibiStateName.Bored]: undefined,
[EChibiStateName.Idle]: undefined,
[EChibiStateName.Exited]: undefined,
[EChibiStateName.Fighting]: undefined,
[EChibiStateName.Eating]: (): ChibiState | undefined => this.resolveEating(),
[EChibiStateName.Hungry]: undefined,
[EChibiStateName.Depressed]: undefined,
[EChibiStateName.Tired]: undefined
}
// resolvers
// functions which auitomatically resolve based on the existence cycle
// functions which automatically resolve based on the existence cycle
resolveAwake(): void {
if (this.isAwake()) {
console.log(`${this.chibi.name} is awake.`);
this.timeSinceLastInteraction++;
this.hasBeenAwakeFor++;
resolveAwake(): ChibiState | undefined {
console.log(`${this.chibi.name} is awake.`);
this.timeSinceLastInteraction++;
this.hasBeenAwakeFor++;
if (this.timeInCurrentState % 10 === 0) {
this.saddenedByNeglect(this.timeSinceLastInteraction, SADNESS_FROM_NEGLECT);
}
if (this.hasBeenAwakeFor > MAX_TIME_AWAKE) {
this.changeState(STATE_SLEEPING);
}
if (this.timeInCurrentState % 10 === 0) {
this.saddenedByNeglect(this.timeSinceLastInteraction, SADNESS_FROM_NEGLECT);
}
if (this.hasBeenAwakeFor > MAX_TIME_AWAKE) {
return STATE_SLEEPING;
}
return undefined;
}
increaseHappyness(increase: number): void {
resolveSleeping(): ChibiState | undefined {
console.log(`${this.chibi.name} is sleeping.`);
this.hasBeenAwakeFor = 0;
this.increaseHappyness(HAPPINESS_FROM_SLEEP);
const awakeChance = this.randomService.obtainRandom(2048);
if (awakeChance.matchesChance(1)) {
return STATE_AWAKE;
}
return undefined;
}
resolveGrumbling(): ChibiState | undefined {
console.log(`${this.chibi.name} is grumbling. ${this.timeInCurrentState}`);
if (this.timeInCurrentState > MAX_DURATION_GRUMBLE) {
return STATE_SLEEPING;
}
return undefined;
}
resolveEating(): ChibiState | undefined {
console.log(`${this.chibi.name} is eating. ${this.timeInCurrentState}`);
if (this.timeInCurrentState > MAX_DURATION_EATING) {
return STATE_IDLE;
}
return undefined;
}
private increaseHappyness(increase: number): void {
console.log(`${this.chibi.name} happyness increases.`);
this.chibi.happyness = this.chibi.happyness + increase;
this.chibi.happyness.current = this.chibi.happyness.current + increase;
}
saddenedByNeglect(neglectTime: number, sadness: number): void {
private saddenedByNeglect(neglectTime: number, sadness: number): void {
console.log(`${this.chibi.name} happyness decreases.`);
this.chibi.happyness = this.chibi.happyness - (neglectTime * sadness);
}
resolveSleeping(): void {
if (isState(this.state, EChibiStateName.Sleeping)) {
console.log(`${this.chibi.name} is sleeping.`);
this.hasBeenAwakeFor = 0;
this.increaseHappyness(HAPPINESS_FROM_SLEEP);
const awakeChance = this.randomService.obtainRandom(2048);
if (awakeChance.matchesChance(1)) {
this.changeState(STATE_AWAKE);
}
}
}
resolveGrumbling(): void {
if (isState(this.state, EChibiStateName.Grumbling)) {
console.log(`${this.chibi.name} is grumbling.`);
if (this.timeInCurrentState > MAX_DURATION_GRUMBLE) {
this.changeState(STATE_SLEEPING);
}
}
}
resolveEating(): void {
if (isState(this.state, EChibiStateName.Eating)) {
console.log(`${this.chibi.name} is eating.`);
if (this.timeInCurrentState > MAX_DURATION_EATING) {
this.changeState(STATES[this.previousState]);
}
}
}
private changeState(state: ChibiState): void {
this.previousState = this.state.name;
this.state = state;
this.chibi.state = this.state.name;
this.timeInCurrentState = 0;
this.chibi.happyness.current = this.chibi.happyness.current - (neglectTime * sadness);
}
/*

View File

@@ -0,0 +1,36 @@
import { EChibiStateName } from "../../../types/chibi/chibi-state-name";
import { BasicBrain, InteractionMap, ResolutionMap } from "../basic.brain";
export class Caethya extends BasicBrain {
protected override interactionMap: InteractionMap = {
[EChibiStateName.Sleeping]: undefined,
[EChibiStateName.Awake]: undefined,
[EChibiStateName.Snoring]: undefined,
[EChibiStateName.Nightmare]: undefined,
[EChibiStateName.Grumbling]: undefined,
[EChibiStateName.Bored]: undefined,
[EChibiStateName.Idle]: undefined,
[EChibiStateName.Exited]: undefined,
[EChibiStateName.Fighting]: undefined,
[EChibiStateName.Eating]: undefined,
[EChibiStateName.Hungry]: undefined,
[EChibiStateName.Depressed]: undefined,
[EChibiStateName.Tired]: undefined
};
protected override resolutionMap: ResolutionMap = {
[EChibiStateName.Sleeping]: undefined,
[EChibiStateName.Snoring]: undefined,
[EChibiStateName.Nightmare]: undefined,
[EChibiStateName.Grumbling]: undefined,
[EChibiStateName.Awake]: undefined,
[EChibiStateName.Bored]: undefined,
[EChibiStateName.Idle]: undefined,
[EChibiStateName.Exited]: undefined,
[EChibiStateName.Fighting]: undefined,
[EChibiStateName.Eating]: undefined,
[EChibiStateName.Hungry]: undefined,
[EChibiStateName.Depressed]: undefined,
[EChibiStateName.Tired]: undefined
};
}

View File

@@ -0,0 +1,36 @@
import { EChibiStateName } from "../../../types/chibi/chibi-state-name";
import { BasicBrain, InteractionMap, ResolutionMap } from "../basic.brain";
export class Haetor extends BasicBrain {
protected override interactionMap: InteractionMap = {
[EChibiStateName.Sleeping]: undefined,
[EChibiStateName.Awake]: undefined,
[EChibiStateName.Snoring]: undefined,
[EChibiStateName.Nightmare]: undefined,
[EChibiStateName.Grumbling]: undefined,
[EChibiStateName.Bored]: undefined,
[EChibiStateName.Idle]: undefined,
[EChibiStateName.Exited]: undefined,
[EChibiStateName.Fighting]: undefined,
[EChibiStateName.Eating]: undefined,
[EChibiStateName.Hungry]: undefined,
[EChibiStateName.Depressed]: undefined,
[EChibiStateName.Tired]: undefined
};
protected override resolutionMap: ResolutionMap = {
[EChibiStateName.Sleeping]: undefined,
[EChibiStateName.Snoring]: undefined,
[EChibiStateName.Nightmare]: undefined,
[EChibiStateName.Grumbling]: undefined,
[EChibiStateName.Awake]: undefined,
[EChibiStateName.Bored]: undefined,
[EChibiStateName.Idle]: undefined,
[EChibiStateName.Exited]: undefined,
[EChibiStateName.Fighting]: undefined,
[EChibiStateName.Eating]: undefined,
[EChibiStateName.Hungry]: undefined,
[EChibiStateName.Depressed]: undefined,
[EChibiStateName.Tired]: undefined
};
}

View File

@@ -14,43 +14,45 @@
}
</div>
<div class="h-1/3 min-h-20 bg-primary-300 relative">
<ff-food-pantry
[isVisible]="shownMenu == EInteractionMenuState.Pantry"
(onClose)="closeMenu()"
(onFoodChosen)="interact(EChibiInteraction.Feed, $event)"
></ff-food-pantry>
@if (chibi()) {
<ff-food-pantry
[isVisible]="shownMenu == EInteractionMenuState.Pantry"
(onClose)="closeMenu()"
(onFoodChosen)="interact(EChibiInteraction.Feed, $event)"
></ff-food-pantry>
<ff-inventory
[isVisible]="shownMenu == EInteractionMenuState.Inventory"
(onClose)="closeMenu()"
></ff-inventory>
<ff-inventory
[isVisible]="shownMenu == EInteractionMenuState.Inventory"
(onClose)="closeMenu()"
></ff-inventory>
<ff-visitor-list
[isVisible]="shownMenu == EInteractionMenuState.VisitorList"
(onClose)="closeMenu()"
[host]="chibi()"
[visitors]="[]"
></ff-visitor-list>
<ff-visitor-list
[isVisible]="shownMenu == EInteractionMenuState.VisitorList"
(onClose)="closeMenu()"
[host]="chibi()"
[visitors]="[]"
></ff-visitor-list>
<div class="flex flex-row gap-5 py-4 container mx-auto">
<ff-button (click)="openInventory()">{{
lang.game.actions.giveItem
}}</ff-button>
<ff-button (click)="openFoodPantry()">{{
lang.game.actions.feed
}}</ff-button>
<ff-button (click)="openVisitorList()">{{
lang.game.actions.inviteVisitor
}}</ff-button>
@if (isAwake()) {
<ff-button (click)="interact(EChibiInteraction.WakeUp)">{{
lang.game.actions.wakeUp
<div class="flex flex-row gap-5 py-4 container mx-auto">
<ff-button (click)="openInventory()">{{
lang.game.actions.giveItem
}}</ff-button>
} @else {
<ff-button (click)="interact(EChibiInteraction.PutToSleep)">{{
lang.game.actions.putToSleep
<ff-button (click)="openFoodPantry()">{{
lang.game.actions.feed
}}</ff-button>
}
</div>
<ff-button (click)="openVisitorList()">{{
lang.game.actions.inviteVisitor
}}</ff-button>
@if (isAwake()) {
<ff-button (click)="interact(EChibiInteraction.PutToSleep)">{{
lang.game.actions.putToSleep
}}</ff-button>
} @else {
<ff-button (click)="interact(EChibiInteraction.WakeUp)">{{
lang.game.actions.wakeUp
}}</ff-button>
}
</div>
}
</div>
</div>

View File

@@ -12,12 +12,12 @@ import { IBrain } from '../../logic/chibi-behaviour/brain';
import { BRAIN_MAP } from '../../logic/chibi-behaviour/brain-map';
import { TranslateableComponent } from '../../components/translateable.component';
import { Food } from '../../types/food';
import { interval, Subscribable, Subscription } from 'rxjs';
import { interval, Subscription } from 'rxjs';
import { RandomService } from '../../logic/random.service';
import { ChibiInteraction } from '../../logic/chibi-behaviour/brains/aperio.brain';
import { isState, STATES } from '../../types/chibi/chibi-state';
import { EChibiStateName } from '../../types/chibi/chibi-state-name';
import { VisitorListComponent } from "../../components/interaction-menus/visitor-list/visitor-list.component";
import { ChibiInteraction } from '../../logic/chibi-behaviour/basic.brain';
enum EInteractionMenuState {
Neutral,
@@ -70,7 +70,7 @@ export class InteractionsComponent extends TranslateableComponent implements OnD
}
public isAwake(): boolean {
return isState(STATES[this.chibi().state], EChibiStateName.Awake);
return isState(STATES[this.chibi().currentStateName], EChibiStateName.Awake);
}
public interact(interaction: EChibiInteraction, item?: Food): void {
@@ -78,6 +78,7 @@ export class InteractionsComponent extends TranslateableComponent implements OnD
this.brain.resolveInteraction({ name: interaction, payload: item } as ChibiInteraction<Food>);
}
public openFoodPantry(): void {
this.interactionMenuState.set(EInteractionMenuState.Pantry);
}

View File

@@ -4,6 +4,7 @@ export enum EChibiName {
Caethya = 'Caethya',
Eos = 'Eos',
Evina = 'Evina',
Haetor = 'Haetor',
Sol = 'Sol',
Sybil = 'Sybil',
Vinera = 'Vinera'

View File

@@ -8,7 +8,7 @@ export type ChibiPreview = {
id: ChibiId;
name: EChibiName;
iconPath: string;
happyness: number;
happyness: ChibiStatBar;
level: number;
};
@@ -19,15 +19,15 @@ export type Chibi = {
spritePath: string;
experience: number;
unlocked: boolean;
state: EChibiStateName;
currentStateName: EChibiStateName;
} & ChibiWellnessStats & ChibiTraits;
export type ChibiWellnessStats = {
happyness: number;
health: number;
hunger: number; //feed them
boredom: number; //do activities to alleviate boredom
energy: number; //let them rest
happyness: ChibiStatBar;
health: ChibiStatBar;
hunger: ChibiStatBar; //feed them
boredom: ChibiStatBar; //do activities to alleviate boredom
energy: ChibiStatBar; //let them rest
};
export type ChibiTraits = {
@@ -36,4 +36,9 @@ export type ChibiTraits = {
constitution: number; //how resistant they are to damage
power: number; //how much damage they deal
maintenanceCals: number; //how quickly they get hungry
};
};
export type ChibiStatBar = {
current: number;
max: number;
}