vtm/quelldatein/aufbau/partie.ts

362 lines
9.2 KiB
TypeScript
Raw Normal View History

2017-11-09 14:06:35 +01:00
/*
* 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 export function partie_License as published by
2017-11-09 14:06:35 +01:00
* 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 export function partie_License for more details.
2017-11-09 14:06:35 +01:00
*
* You should have received a copy of the GNU General export function partie_License
2017-11-09 14:06:35 +01:00
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
module mod_vtm_aufbau
{
/**
* @author kcf <vidofnir@folksprak.org>
*/
export type typ_partie =
2017-11-09 14:06:35 +01:00
{
welt : typ_welt;
figur : schnittstelle_fehlermonade<typ_figur>;
2018-03-26 00:41:10 +02:00
aufgabe : typ_aufgabe;
testindex : schnittstelle_fehlermonade<int>;
modus : typ_modus;
lauscher : {[ereignis : string] : Array<(angaben ?: any)=>void>};
}
;
/**
* @author kcf <vidofnir@folksprak.org>
*/
export function partie_erstellen
(
2018-03-26 00:41:10 +02:00
aufgabe : 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": [],
};
partie_welt_leeren(partie, false);
partie_zuruecksetzen(partie, false);
return partie;
}
/**
* @author kcf <vidofnir@folksprak.org>
*/
export function partie_lauschen(partie : typ_partie, ereignis : string, prozedur : (angaben ?: any)=>void) : void
{
if (ereignis in partie.lauscher)
2017-11-09 14:06:35 +01:00
{
partie.lauscher[ereignis].push(prozedur);
2017-11-09 21:57:35 +01:00
}
else
2017-11-09 21:57:35 +01:00
{
let meldung : string = "kein Ereignis mit diesem Name";
throw (new Error(meldung));
2017-11-09 21:57:35 +01:00
}
}
2017-11-09 21:57:35 +01:00
/**
* @author kcf <vidofnir@folksprak.org>
*/
function partie_benachrichtigen(partie : typ_partie, ereignis : string, angaben : any = {}) : void
{
if (ereignis in partie.lauscher)
2017-11-09 21:57:35 +01:00
{
partie.lauscher[ereignis].forEach
(
prozedur =>
{
prozedur(angaben);
}
)
;
2017-11-09 21:57:35 +01:00
}
else
2018-03-20 13:30:00 +01:00
{
let meldung : string = "kein Ereignis mit diesem Name";
throw (new Error(meldung));
2018-03-20 13:30:00 +01:00
}
}
/**
* @author kcf <vidofnir@folksprak.org>
*/
export function partie_zuruecksetzen(partie : typ_partie, bescheid_geben : boolean = true) : void
{
partie.figur = (new klasse_nichts<typ_figur>());
partie.testindex = (new klasse_nichts<int>());
partie.modus = modus_initial;
if (bescheid_geben)
2017-11-09 21:57:35 +01:00
{
partie_benachrichtigen(partie, "aenderung_figur", {});
partie_benachrichtigen(partie, "aenderung_modus", {});
2017-11-09 14:06:35 +01:00
}
else
2017-11-10 00:05:15 +01:00
{
// nichts tun
2017-11-10 00:05:15 +01:00
}
}
/**
* @author kcf <vidofnir@folksprak.org>
*/
2018-03-26 00:41:10 +02:00
export function partie_aufgabe_lesen(partie : typ_partie) : typ_aufgabe
{
return partie.aufgabe;
}
/**
* @author kcf <vidofnir@folksprak.org>
*/
2018-03-26 00:41:10 +02:00
export function partie_aufgabe_setzen(partie : typ_partie, aufgabe : typ_aufgabe) : void
{
partie.aufgabe = aufgabe;
// partie.welt_leeren();
partie_benachrichtigen(partie, "aenderung_aufgabe", {});
partie_zuruecksetzen(partie);
}
/**
* @author kcf <vidofnir@folksprak.org>
*/
export function partie_welt_lesen(partie : typ_partie) : typ_welt
{
return partie.welt;
}
/**
* @author kcf <vidofnir@folksprak.org>
*/
export function partie_welt_setzen(partie : typ_partie, welt : mod_vtm_aufbau.typ_welt, bescheid_geben : boolean = true) : void
{
partie.welt = welt;
if (bescheid_geben)
2017-11-09 14:06:35 +01:00
{
partie_benachrichtigen(partie, "aenderung_welt", {});
2017-11-09 14:06:35 +01:00
}
else
{
// nichts tun
}
}
/**
* @author kcf <vidofnir@folksprak.org>
*/
export function partie_welt_leeren(partie : typ_partie, bescheid_geben : boolean = true) : void
{
partie.welt = welt_blanko();
if (bescheid_geben)
{
partie_benachrichtigen(partie, "aenderung_welt", {});
}
else
2017-11-09 14:06:35 +01:00
{
// nichts tun
2017-11-09 14:06:35 +01:00
}
}
/**
* @author kcf <vidofnir@folksprak.org>
*/
export function partie_figur_lesen(partie : typ_partie) : schnittstelle_fehlermonade<typ_figur>
{
return partie.figur;
}
/**
* @author kcf <vidofnir@folksprak.org>
*/
export function partie_modus_lesen(partie : typ_partie) : typ_modus
{
return partie.modus;
}
/**
* @author kcf <vidofnir@folksprak.org>
*/
export function partie_welt_feld_wechseln(partie : typ_partie, stelle : typ_stelle, umgekehrt : boolean = false) : void
{
if (! (partie.modus === mod_vtm_aufbau.modus_initial))
2017-11-09 14:06:35 +01:00
{
let meldung : string = "gesperrt";
2017-11-09 14:06:35 +01:00
}
else
2017-11-09 21:57:35 +01:00
{
welt_feld_wechseln(partie.welt, stelle, umgekehrt);
partie_benachrichtigen
(
partie,
"aenderung_welt",
{
"art": "feld_wechseln",
"angaben":
{
"stelle": stelle,
"umgekehrt": umgekehrt,
"feld": welt_feld_holen(partie.welt, stelle),
}
}
)
;
2017-11-09 21:57:35 +01:00
}
}
/**
* @author kcf <vidofnir@folksprak.org>
*/
export function partie_welt_feld_drehen(partie : typ_partie, stelle : typ_stelle, inkrement : int = +1) : void
{
if (! (partie.modus === mod_vtm_aufbau.modus_initial))
2017-11-09 21:57:35 +01:00
{
let meldung : string = "gesperrt";
2017-11-09 18:42:09 +01:00
}
else
2017-11-09 14:06:35 +01:00
{
welt_feld_drehen(partie.welt, stelle, inkrement);
partie_benachrichtigen(partie, "aenderung_welt", {});
}
}
/**
* @author kcf <vidofnir@folksprak.org>
*/
export function partie_fortfahren(partie : typ_partie) : void
{
switch (partie.modus)
{
case modus_initial:
{
partie.modus = modus_ungewiss;
partie.testindex = (new klasse_schlicht<int>(0));
partie_benachrichtigen(partie, "aenderung_modus", {});
break;
}
case modus_ungewiss:
2017-11-09 14:06:35 +01:00
{
if (! partie.figur.ist_schlicht())
2017-11-09 14:06:35 +01:00
{
2018-03-26 00:41:10 +02:00
let test : typ_test = aufgabe_tests(partie.aufgabe)[partie.testindex.lesen()];
let band : Array<typ_symbol> = mod_vtm_helfer.liste_kopieren<typ_symbol>(test_eingabe(test));
let stelle : typ_stelle = welt_erzeuger_finden(partie.welt);
partie.figur = (
new klasse_schlicht<typ_figur>
(
figur_erstellen
(
band,
stelle
)
)
);
2017-11-09 14:06:35 +01:00
}
else
2017-11-09 14:06:35 +01:00
{
let figur : typ_figur = partie.figur.lesen();
let stelle : typ_stelle = figur_stelle_lesen(figur);
let aktor_ : schnittstelle_fehlermonade<typ_aktor> = welt_feld_holen(partie.welt, stelle);
2018-03-26 00:41:10 +02:00
let aktor : typ_aktor = (aktor_.ist_schlicht() ? aktor_.lesen() : verwerfer_erstellen_aktor());
aktor_verwenden(aktor, figur);
let zustand : typ_zustand = figur_zustand_lesen(figur);
if (zustand === zustand_laufend)
2017-11-09 14:06:35 +01:00
{
// nichts tun
2017-11-09 14:06:35 +01:00
}
else if ((zustand === zustand_angenommen) || (zustand === zustand_abgelehnt))
2017-11-09 14:06:35 +01:00
{
let angenommen : boolean = (zustand === zustand_angenommen);
let ausgabe : Array<typ_symbol> = figur_band_lesen(figur);
partie.figur = (new klasse_nichts<typ_figur>());
let testindex : int = partie.testindex.lesen();
2018-03-26 00:41:10 +02:00
let tests : Array<typ_test> = aufgabe_tests(partie.aufgabe);
let test : typ_test = tests[testindex];
if (! test_pruefen(test, angenommen, ausgabe))
2017-11-09 14:06:35 +01:00
{
partie.modus = modus_fehlerhaft;
partie_benachrichtigen(partie, "aenderung_modus", {});
2017-11-09 14:06:35 +01:00
}
else
2017-11-09 14:06:35 +01:00
{
testindex += 1;
if (testindex < tests.length)
2017-11-09 14:06:35 +01:00
{
// nächsten Test auswählen
partie.testindex = (new klasse_schlicht<int>(testindex));
2017-11-09 14:06:35 +01:00
}
else
{
// auf Modus "korrekt" wechseln
partie.testindex = (new klasse_nichts<int>());
partie.modus = modus_korrekt;
partie_benachrichtigen(partie, "aenderung_modus", {});
2017-11-09 14:06:35 +01:00
}
}
}
else
{
let meldung : string = "unbehandelter Zustand";
throw (new Error(meldung));
}
2017-11-09 14:06:35 +01:00
}
partie_benachrichtigen(partie, "aenderung_figur", {});
break;
}
case modus_fehlerhaft:
{
// nichts tun
break;
}
case modus_korrekt:
{
// nichts tun
break;
}
default:
{
let meldung : string = "unbehandelter Modus";
throw (new Error(meldung));
break;
2017-11-09 14:06:35 +01:00
}
}
2017-11-09 14:06:35 +01:00
}
}