vtm/quelldatein/aufbau/partie.ts
2018-03-26 10:42:58 +02:00

372 lines
11 KiB
TypeScript

/*
* Verrückte Turing-Maschinen — A turing complete game
* Copyright (C) 2016 Christian Fraß <vidofnir@folksprak.org>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
module mod_vtm
{
export module mod_aufbau
{
export module mod_partie
{
/**
* @author kcf <vidofnir@folksprak.org>
*/
export type typ_partie =
{
welt : mod_welt.typ_welt;
figur : schnittstelle_fehlermonade<mod_figur.typ_figur>;
aufgabe : mod_vtm_aufbau.typ_aufgabe;
testindex : schnittstelle_fehlermonade<int>;
modus : mod_modus.typ_modus;
lauscher : {[ereignis : string] : Array<(angaben ?: any)=>void>};
}
;
/**
* @author kcf <vidofnir@folksprak.org>
*/
export function erstellen
(
aufgabe : mod_vtm_aufbau.typ_aufgabe
)
: typ_partie
{
let partie : typ_partie = {
"welt": undefined,
"figur": undefined,
"aufgabe": aufgabe,
"testindex": undefined,
"modus": undefined,
"lauscher": undefined,
};
partie.lauscher = {
"aenderung_aufgabe": [],
"aenderung_welt": [],
"aenderung_figur": [],
"aenderung_modus": [],
};
welt_leeren(partie, false);
zuruecksetzen(partie, false);
return partie;
}
/**
* @author kcf <vidofnir@folksprak.org>
*/
export function lauschen(partie : typ_partie, ereignis : string, prozedur : (angaben ?: any)=>void) : void
{
if (ereignis in partie.lauscher)
{
partie.lauscher[ereignis].push(prozedur);
}
else
{
let meldung : string = "kein Ereignis mit diesem Name";
throw (new Error(meldung));
}
}
/**
* @author kcf <vidofnir@folksprak.org>
*/
function benachrichtigen(partie : typ_partie, ereignis : string, angaben : any = {}) : void
{
if (ereignis in partie.lauscher)
{
partie.lauscher[ereignis].forEach
(
prozedur =>
{
prozedur(angaben);
}
)
;
}
else
{
let meldung : string = "kein Ereignis mit diesem Name";
throw (new Error(meldung));
}
}
/**
* @author kcf <vidofnir@folksprak.org>
*/
export function zuruecksetzen(partie : typ_partie, bescheid_geben : boolean = true) : void
{
partie.figur = (new klasse_nichts<mod_figur.typ_figur>());
partie.testindex = (new klasse_nichts<int>());
partie.modus = mod_modus.initial;
if (bescheid_geben)
{
benachrichtigen(partie, "aenderung_figur", {});
benachrichtigen(partie, "aenderung_modus", {});
}
else
{
// nichts tun
}
}
/**
* @author kcf <vidofnir@folksprak.org>
*/
export function aufgabe_lesen(partie : typ_partie) : mod_vtm_aufbau.typ_aufgabe
{
return partie.aufgabe;
}
/**
* @author kcf <vidofnir@folksprak.org>
*/
export function aufgabe_setzen(partie : typ_partie, aufgabe : mod_vtm_aufbau.typ_aufgabe) : void
{
partie.aufgabe = aufgabe;
// partie.welt_leeren();
benachrichtigen(partie, "aenderung_aufgabe", {});
zuruecksetzen(partie);
}
/**
* @author kcf <vidofnir@folksprak.org>
*/
export function welt_lesen(partie : typ_partie) : mod_welt.typ_welt
{
return partie.welt;
}
/**
* @author kcf <vidofnir@folksprak.org>
*/
export function welt_setzen(partie : typ_partie, welt : mod_welt.typ_welt, bescheid_geben : boolean = true) : void
{
partie.welt = welt;
if (bescheid_geben)
{
benachrichtigen(partie, "aenderung_welt", {});
}
else
{
// nichts tun
}
}
/**
* @author kcf <vidofnir@folksprak.org>
*/
export function welt_leeren(partie : typ_partie, bescheid_geben : boolean = true) : void
{
partie.welt = mod_welt.blanko();
if (bescheid_geben)
{
benachrichtigen(partie, "aenderung_welt", {});
}
else
{
// nichts tun
}
}
/**
* @author kcf <vidofnir@folksprak.org>
*/
export function figur_lesen(partie : typ_partie) : schnittstelle_fehlermonade<mod_figur.typ_figur>
{
return partie.figur;
}
/**
* @author kcf <vidofnir@folksprak.org>
*/
export function modus_lesen(partie : typ_partie) : mod_modus.typ_modus
{
return partie.modus;
}
/**
* @author kcf <vidofnir@folksprak.org>
*/
export function welt_feld_wechseln(partie : typ_partie, stelle : mod_stelle.typ_stelle, umgekehrt : boolean = false) : void
{
if (! (partie.modus === mod_modus.initial))
{
let meldung : string = "gesperrt";
}
else
{
mod_welt.feld_wechseln(partie.welt, stelle, umgekehrt);
benachrichtigen
(
partie,
"aenderung_welt",
{
"art": "feld_wechseln",
"angaben":
{
"stelle": stelle,
"umgekehrt": umgekehrt,
"feld": mod_welt.feld_holen(partie.welt, stelle),
}
}
)
;
}
}
/**
* @author kcf <vidofnir@folksprak.org>
*/
export function welt_feld_drehen(partie : typ_partie, stelle : mod_stelle.typ_stelle, inkrement : int = +1) : void
{
if (! (partie.modus === mod_modus.initial))
{
let meldung : string = "gesperrt";
}
else
{
mod_welt.feld_drehen(partie.welt, stelle, inkrement);
benachrichtigen(partie, "aenderung_welt", {});
}
}
/**
* @author kcf <vidofnir@folksprak.org>
*/
export function fortfahren(partie : typ_partie) : void
{
switch (partie.modus)
{
case mod_modus.initial:
{
partie.modus = mod_modus.ungewiss;
partie.testindex = (new klasse_schlicht<int>(0));
benachrichtigen(partie, "aenderung_modus", {});
break;
}
case mod_modus.ungewiss:
{
if (! partie.figur.ist_schlicht())
{
let test : mod_vtm_aufbau.typ_test = mod_vtm_aufbau.aufgabe_tests(partie.aufgabe)[partie.testindex.lesen()];
let band : Array<mod_symbol.typ_symbol> = mod_vtm_helfer.liste_kopieren<mod_symbol.typ_symbol>(mod_vtm_aufbau.test_eingabe(test));
let stelle : mod_stelle.typ_stelle = mod_welt.erzeuger_finden(partie.welt);
partie.figur = (
new klasse_schlicht<mod_figur.typ_figur>
(
mod_figur.erstellen
(
band,
stelle
)
)
);
}
else
{
let figur : mod_figur.typ_figur = partie.figur.lesen();
let stelle : mod_stelle.typ_stelle = mod_figur.stelle_lesen(figur);
let aktor_ : schnittstelle_fehlermonade<mod_aktor.typ_aktor> = mod_welt.feld_holen(partie.welt, stelle);
let aktor : mod_aktor.typ_aktor = (aktor_.ist_schlicht() ? aktor_.lesen() : mod_aktor_verwerfer.erstellen_aktor());
mod_aktor.verwenden(aktor, figur);
let zustand : mod_zustand.typ_zustand = mod_figur.zustand_lesen(figur);
if (zustand === mod_zustand.laufend)
{
// nichts tun
}
else if ((zustand === mod_zustand.angenommen) || (zustand === mod_zustand.abgelehnt))
{
let angenommen : boolean = (zustand === mod_zustand.angenommen);
let ausgabe : Array<mod_symbol.typ_symbol> = mod_figur.band_lesen(figur);
partie.figur = (new klasse_nichts<mod_figur.typ_figur>());
let testindex : int = partie.testindex.lesen();
let tests : Array<mod_vtm_aufbau.typ_test> = mod_vtm_aufbau.aufgabe_tests(partie.aufgabe);
let test : mod_vtm_aufbau.typ_test = tests[testindex];
if (! mod_vtm_aufbau.test_pruefen(test, angenommen, ausgabe))
{
partie.modus = mod_modus.fehlerhaft;
benachrichtigen(partie, "aenderung_modus", {});
}
else
{
testindex += 1;
if (testindex < tests.length)
{
// nächsten Test auswählen
partie.testindex = (new klasse_schlicht<int>(testindex));
}
else
{
// auf Modus "korrekt" wechseln
partie.testindex = (new klasse_nichts<int>());
partie.modus = mod_modus.korrekt;
benachrichtigen(partie, "aenderung_modus", {});
}
}
}
else
{
let meldung : string = "unbehandelter Zustand";
throw (new Error(meldung));
}
}
benachrichtigen(partie, "aenderung_figur", {});
break;
}
case mod_modus.fehlerhaft:
{
// nichts tun
break;
}
case mod_modus.korrekt:
{
// nichts tun
break;
}
default:
{
let meldung : string = "unbehandelter Modus";
throw (new Error(meldung));
break;
}
}
}
}
}
}