#!sdl_chomik

#
# A fantasy game by Pawel Biernacki
# version 0.0.2
# written in SDL_Chomik
# requires sdl_chomik 0.2.2
# Vantaa 2026

let sdl chomik widgets prefix=value integer 0;

include "sdl_chomik_widgets.sdl_chomik"
include "fantasy_load_additional_graphics.sdl_chomik"
include "fantasy_game_skeleton.sdl_chomik"
include "fantasy_create_widgets.sdl_chomik"
include "fantasy_perceived_informations.sdl_chomik"

let learn interesting informations for the person (PERSON:integer)=value code
{
    let my person id = value integer [(PERSON:integer)];
};


let learn interesting informations=value code
{
    # given the perceived informations - find the informations that are not obviously known
    <learn interesting informations for the person (PERSON:person_id)>;
};

let learn obvious informations for the person (PERSON:integer)=value code
{
    let my person id = value integer [(PERSON:integer)];
};

let learn obvious informations=value code
{
    # given the perceived informations - find the informations that are obviously known
    # (for the wizards many things are obvious)
    <learn obvious informations for the person (PERSON:person_id)>;
};

# everyone chooses randomly a decision - i.e. the index of the possible action he wants to do
let make decision for the person (PERSON:integer)=value code
{
    let my person id = value integer [(PERSON:integer)];

    let the read from stream source stream index = <my decision making stream index>;
    <read from stream "integer">;

    <modulo "integer" <the read from stream result "integer"> <my amount of possible actions the person <my person id> can perform>>; # we know the amount of actions is greater than 0
    <add "integer" <the modulo result "integer"> 1>;
    let my decision of the person <my person id> = <the add result "integer">;

    <print "the person" <my person id> "makes decision" <my possible action <my decision of the person <my person id>> the person <my person id> can perform>>;
};

let make decisions=value code
{
    <make decision for the person (PERSON:person_id)>;
};


let perform action pick up the key=value code
{
    <print "I pick up the key" <my possible action <my possible action index> for the person <my person id> the key>>;

    let my key id = <my possible action <my possible action index> for the person <my person id> the key>;

    let my pocket item location = value item_location in persons <my person id> pocket;
    let my location of the key <my key id> = <my pocket item location>;
};

let perform action give the key to someone else=value code
{
    <print "I give the key" <my possible action <my possible action index> for the person <my person id> the key> "to someone else" <my possible action <my possible action index> for the person <my person id> the target person>>;

    let my key id = <my possible action <my possible action index> for the person <my person id> the key>;
    let my person 2 id = <my possible action <my possible action index> for the person <my person id> the target person>;

    let my new pocket item location = value item_location in persons <my person 2 id> pocket;
    let my location of the key <my key id> = <my new pocket location>;
};

let perform action attack someone else=value code
{
    <print "I attack someone else" <my possible action <my possible action index> for the person <my person id> the target person>>;

    let my person 2 id = <my possible action <my possible action index> for the person <my person id> the target person>;
};

let perform action go to place=value code
{
    <print "I go to a place" <my possible action <my possible action index> for the person <my person id> the target place>>;
    let my target place of the person <my person id> = <my possible action <my possible action index> for the person <my person id> the target place>;
    let my place of the person <my person id> = <my possible action <my possible action index> for the person <my person id> the target place>;
};

let perform action wait=value code
{
    <print "I wait">;
};

let perform decision for the person (PERSON:integer)=value code
{
    let my person id = value integer [(PERSON:integer)];

    <print "the person" <my person id> "performs decision" <my possible action <my decision of the person <my person id>> the person <my person id> can perform>>;

    let my possible action index = <my decision of the person <my person id>>;

    <my how to do the action <my decision of the person <my person id>> the person <my person id> can perform>;
};

let perform all decisions=value code
{
    <print "performing all decisions">;
    <perform decision for the person (PERSON:person_id)>;
    <print "all decisions have been performed">;
};


include "fantasy_choose_graphical_sprites_location.sdl_chomik"


<assign the perception>;
let amount of key sprites in place (PLACE:place_id) = value integer 0;
let initial amount of person sprites in place (PLACE:place_id) = value integer 0;

<choose graphical sprites initial location>;
<learn interesting informations>; # interesting informations imply the questions he might ask
<learn obvious informations>; # if something is obvious and someone asks you about it you tend to answer
let my target place of the person (PERSON:person_id)=<my place of the person (PERSON:person_id)>;
<make decisions>;


#<please print all perceived informations of the person 1>;
#<please print all possible actions of the person 1>;


type game_mode = { intro, game, game animation, credits };
expand(1);
let my game mode = value game_mode intro;


let please show the screen on game mode intro = value code
{
    <show image <my background tile image index> 0 0>;
    <show text <<sdl chomik widgets prefix> the font regular index>
        "A long, long time ago, in the mighty Commonwealth there were four dwarves." 64 32>;
    <show text <<sdl chomik widgets prefix> the font regular index>
        "Their names were Gotrek, Gerrudir, Gimli and Gregor." 64 64>;
    <show text <<sdl chomik widgets prefix> the font regular index>
        "The Commonwealth was a vast country, with five major towns." 64 96>;
    <show text <<sdl chomik widgets prefix> the font regular index>
        "Each dwarf has a class and various features." 64 128>;
    <show text <<sdl chomik widgets prefix> the font regular index>
        "There are also some keys here and there, which can open the towns." 64 160>;
    <show text <<sdl chomik widgets prefix> the font regular index>
        "Any dwarf can attack another one, provided they are in the same town." 64 192>;

    <show text <<sdl chomik widgets prefix> the font regular index>
        "Press Enter to play." 64 256>;

    <show image <my chomik image index> 0 0>;
};

let please show the screen on game mode game = value code
{
    <show image <my game background tile image index> 0 0>;
    <<sdl chomik widgets prefix> please show all widgets>;
    <show image <my chomik image index> 0 0>;
};

let copy the terminal location to the initial location for all persons (PERSON:integer)=value code
{
    let my person id = value integer [(PERSON:integer)];
    let my person widget index = <my widget for the person <my person id>>;
    let <sdl chomik widgets prefix> the widget initial relative position x of the widget <my person widget index> = <<sdl chomik widgets prefix> the widget terminal relative position x of the widget <my person widget index>>;
    let <sdl chomik widgets prefix> the widget initial relative position y of the widget <my person widget index> = <<sdl chomik widgets prefix> the widget terminal relative position y of the widget <my person widget index>>;

    let my place of the person <my person id> = <my target place of the person <my person id>>;
};


let my game animation progress = value float 0.0;

let switch to the game on lower = value code {};
let switch to the game on equal = value code
{
    let my game mode = value game_mode game;
    <copy the terminal location to the initial location for all persons (PERSON:person_id)>;
    <print "animation terminated">;
};
let switch to the game on greater = value code
{
    let my game mode = value game_mode game;
    <copy the terminal location to the initial location for all persons (PERSON:person_id)>;
    <print "animation terminated">;

};

let please show the screen on game mode game animation = value code
{
    <show image <my game background tile image index> 0 0>;
    #<print "my game animation progress=" <my game animation progress>>;

    <<sdl chomik widgets prefix> please set the widget animation progress <my game animation progress>>;

    <<sdl chomik widgets prefix> please show all widgets>;
    <show image <my chomik image index> 0 0>;

    <add "float" <my game animation progress> 0.04>;
    let my game animation progress = <the add result "float">;

    <compare "float" <my game animation progress> 1.0>;
    <switch to the game on <the compare result>>;
};

let please show the screen on game mode credits = value code
{
    <show image <my game background tile image index> 0 0>;
    <show text <<sdl chomik widgets prefix> the font regular index>
        "A fantasy game by Pawel Biernacki." 64 32>;
    <show image <my chomik image index> 0 0>;
};

let please run the key return handler on game mode intro = value code
{
    let my game mode = value game_mode game;
};

let please run the key return handler on game mode game = value code
{
    <perform all decisions>;
    let terminal amount of person sprites in place (PLACE:place_id) = value integer 0;
    let amount of key sprites in place (PLACE:place_id) = value integer 0;
    <choose graphical sprites terminal location>;
    <assign the perception>;
    <learn interesting informations>; # interesting informations imply the questions he might ask
    <learn obvious informations>; # if something is obvious and someone asks you about it you tend to answer
    let my target place of the person (PERSON:person_id)=<my place of the person (PERSON:person_id)>;
    <make decisions>;

    let my game animation progress = value float 0.0;
    let my game mode = value game_mode game animation;
};

let please run the key return handler on game mode game animation = value code
{
    <print "animation progress" <my game animation progress>>;
};

let please run the key escape handler on game mode intro = value code {};

let please run the key escape handler on game mode game = value code
{
    let my game mode = value game_mode credits;
};

let please run the key escape handler on game mode game animation = value code
{
    let my game mode = value game_mode credits;
};

let please run the key escape handler on game mode credits = value code
{
    <sdl quit>;
};

let on mouse motion = value code
{
    <<sdl chomik widgets prefix> please handle mouse motion on <<sdl chomik widgets prefix> the widget mode>>;
};

let on mouse button left=value code
{
    <<sdl chomik widgets prefix> please handle mouse left click on <<sdl chomik widgets prefix> the widget mode>>;
};

let on key return = value code
{
    <please run the key return handler on game mode <my game mode>>;
};

let on key escape = value code
{
    <please run the key escape handler on game mode <my game mode>>;
};

let sdl loop body = value code
{
    <please show the screen on game mode <my game mode>>;
};

<sdl loop>;

