vtm/quelldatein/aufbau/partie.ts

372 lines
11 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
2018-03-26 10:42:58 +02:00
* it under the terms of the GNU General Public 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
2018-03-26 10:42:58 +02:00
* GNU General Public License for more details.
2017-11-09 14:06:35 +01:00
*
2018-03-26 10:42:58 +02:00
* You should have received a copy of the GNU General Public License
2017-11-09 14:06:35 +01:00
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
2018-03-26 10:42:58 +02:00
module mod_vtm
2017-11-09 14:06:35 +01:00
{
2018-03-26 10:42:58 +02:00
export module mod_aufbau
2017-11-09 14:06:35 +01:00
{
2018-03-26 10:42:58 +02:00
export module mod_partie
2017-11-09 14:06:35 +01:00
{
2018-03-26 10:42:58 +02:00
/**
* @author kcf <vidofnir@folksprak.org>
*/
export type typ_partie =
{
welt : mod_welt.typ_welt;
figur : schnittstelle_fehlermonade<mod_figur.typ_figur>;
2018-03-26 14:22:20 +02:00
aufgabe : mod_aufgabe.typ_aufgabe;
2018-03-26 10:42:58 +02:00
testindex : schnittstelle_fehlermonade<int>;
modus : mod_modus.typ_modus;
lauscher : {[ereignis : string] : Array<(angaben ?: any)=>void>};
}
;
2017-11-09 21:57:35 +01:00
2018-03-26 10:42:58 +02:00
/**
* @author kcf <vidofnir@folksprak.org>
*/
export function erstellen
(
2018-03-26 14:22:20 +02:00
aufgabe : mod_aufgabe.typ_aufgabe
2018-03-26 10:42:58 +02:00
)
: 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)
{
2018-03-26 10:42:58 +02:00
partie.lauscher[ereignis].push(prozedur);
}
2018-03-26 10:42:58 +02:00
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>
*/
2018-03-26 14:22:20 +02:00
export function aufgabe_lesen(partie : typ_partie) : mod_aufgabe.typ_aufgabe
2018-03-26 10:42:58 +02:00
{
return partie.aufgabe;
}
/**
* @author kcf <vidofnir@folksprak.org>
*/
2018-03-26 14:22:20 +02:00
export function aufgabe_setzen(partie : typ_partie, aufgabe : mod_aufgabe.typ_aufgabe) : void
2018-03-26 10:42:58 +02:00
{
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
{
2018-03-26 10:42:58 +02:00
partie.welt = mod_welt.blanko();
if (bescheid_geben)
{
benachrichtigen(partie, "aenderung_welt", {});
}
else
{
// nichts tun
}
}
2018-03-26 10:42:58 +02:00
/**
* @author kcf <vidofnir@folksprak.org>
*/
export function figur_lesen(partie : typ_partie) : schnittstelle_fehlermonade<mod_figur.typ_figur>
2017-11-09 14:06:35 +01:00
{
2018-03-26 10:42:58 +02:00
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))
2017-11-09 14:06:35 +01:00
{
2018-03-26 10:42:58 +02:00
let meldung : string = "gesperrt";
2017-11-09 14:06:35 +01:00
}
else
2017-11-09 14:06:35 +01:00
{
2018-03-26 10:42:58 +02:00
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:
2017-11-09 14:06:35 +01:00
{
2018-03-26 10:42:58 +02:00
partie.modus = mod_modus.ungewiss;
partie.testindex = (new klasse_schlicht<int>(0));
benachrichtigen(partie, "aenderung_modus", {});
break;
2017-11-09 14:06:35 +01:00
}
2018-03-26 10:42:58 +02:00
case mod_modus.ungewiss:
2017-11-09 14:06:35 +01:00
{
2018-03-26 10:42:58 +02:00
if (! partie.figur.ist_schlicht())
2017-11-09 14:06:35 +01:00
{
2018-03-26 14:22:20 +02:00
let test : mod_test.typ_test = mod_aufgabe.tests(partie.aufgabe)[partie.testindex.lesen()];
2018-03-26 22:14:10 +02:00
let band : Array<mod_symbol.typ_symbol> = mod_vtm.mod_helfer.liste_kopieren<mod_symbol.typ_symbol>(mod_test.eingabe(test));
2018-03-26 10:42:58 +02:00
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
)
)
);
2017-11-09 14:06:35 +01:00
}
else
2017-11-09 14:06:35 +01:00
{
2018-03-26 10:42:58 +02:00
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))
2017-11-09 14:06:35 +01:00
{
2018-03-26 10:42:58 +02:00
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();
2018-03-26 14:22:20 +02:00
let tests : Array<mod_test.typ_test> = mod_aufgabe.tests(partie.aufgabe);
let test : mod_test.typ_test = tests[testindex];
if (! mod_test.pruefen(test, angenommen, ausgabe))
2018-03-26 10:42:58 +02:00
{
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", {});
}
}
2017-11-09 14:06:35 +01:00
}
else
{
2018-03-26 10:42:58 +02:00
let meldung : string = "unbehandelter Zustand";
throw (new Error(meldung));
2017-11-09 14:06:35 +01:00
}
}
2018-03-26 10:42:58 +02:00
benachrichtigen(partie, "aenderung_figur", {});
break;
}
case mod_modus.fehlerhaft:
{
// nichts tun
break;
2017-11-09 14:06:35 +01:00
}
2018-03-26 10:42:58 +02:00
case mod_modus.korrekt:
{
2018-03-26 10:42:58 +02:00
// nichts tun
break;
}
default:
{
let meldung : string = "unbehandelter Modus";
throw (new Error(meldung));
2018-03-26 10:42:58 +02:00
break;
}
2017-11-09 14:06:35 +01:00
}
2018-03-26 10:42:58 +02:00
2017-11-09 14:06:35 +01:00
}
2018-03-26 10:42:58 +02:00
2017-11-09 14:06:35 +01:00
}
2018-03-26 10:42:58 +02:00
2017-11-09 14:06:35 +01:00
}
}