vtm/source/model/game.ts

443 lines
11 KiB
TypeScript
Raw Normal View History

2017-11-09 14:06:35 +01:00
/*
* Verrückte Turing-Maschinen A turing complete game
2018-03-29 01:13:39 +02:00
* Copyright (C) 2016-2018 Christian Fraß <vidofnir@folksprak.org>
2017-11-09 14:06:35 +01:00
*
* 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-28 13:59:29 +02:00
export module mod_model
2017-11-09 14:06:35 +01:00
{
2018-03-26 10:42:58 +02:00
2018-03-29 01:13:39 +02:00
export module mod_game
2017-11-09 14:06:35 +01:00
{
2018-03-26 10:42:58 +02:00
/**
* @author kcf <vidofnir@folksprak.org>
*/
2018-03-29 01:13:39 +02:00
export type type_game =
2018-03-26 10:42:58 +02:00
{
2018-03-28 13:59:29 +02:00
world : mod_world.type_world;
token : lib_errormonade.type_errormonade<mod_token.type_token>;
task : mod_task.type_task;
testindex : lib_errormonade.type_errormonade<int>;
mode : mod_mode.type_mode;
listeners : {[event : string] : Array<(data ?: any)=>void>};
2018-03-26 10:42:58 +02:00
}
;
2017-11-09 21:57:35 +01:00
2018-03-26 10:42:58 +02:00
/**
* @author kcf <vidofnir@folksprak.org>
*/
2018-03-28 13:59:29 +02:00
export function create
(
2018-03-28 13:59:29 +02:00
task : mod_task.type_task
2018-03-26 10:42:58 +02:00
)
2018-03-29 01:13:39 +02:00
: type_game
2018-03-26 10:42:58 +02:00
{
2018-03-29 01:13:39 +02:00
let game : type_game =
2018-03-28 13:59:29 +02:00
{
"world": undefined,
"token": undefined,
"task": task,
"testindex": undefined,
"mode": undefined,
"listeners": undefined,
}
;
2018-03-29 01:13:39 +02:00
game.listeners =
2018-03-28 13:59:29 +02:00
{
2018-03-29 01:13:39 +02:00
"change_task": [],
"change_world": [],
"change_token": [],
"change_mode": [],
2018-03-28 13:59:29 +02:00
}
;
2018-03-29 01:13:39 +02:00
world_clear(game, false);
reset(game, false);
return game;
2018-03-26 10:42:58 +02:00
}
/**
* @author kcf <vidofnir@folksprak.org>
*/
2018-03-28 11:19:46 +02:00
export function lauschen
(
2018-03-29 01:13:39 +02:00
game : type_game,
2018-03-28 13:59:29 +02:00
event : string,
procedure : (data ?: any)=>void
2018-03-28 11:19:46 +02:00
)
: void
2018-03-26 10:42:58 +02:00
{
2018-03-29 01:13:39 +02:00
if (event in game.listeners)
{
2018-03-29 01:13:39 +02:00
game.listeners[event].push(procedure);
}
2018-03-26 10:42:58 +02:00
else
{
2018-03-28 13:59:29 +02:00
let message : string = "kein Ereignis mit diesem Name";
throw (new Error(message));
2018-03-26 10:42:58 +02:00
}
}
/**
* @author kcf <vidofnir@folksprak.org>
*/
2018-03-28 13:59:29 +02:00
function notify
2018-03-28 11:19:46 +02:00
(
2018-03-29 01:13:39 +02:00
game : type_game,
2018-03-28 13:59:29 +02:00
event : string,
data : any = {}
2018-03-28 11:19:46 +02:00
) : void
2018-03-26 10:42:58 +02:00
{
2018-03-29 01:13:39 +02:00
if (event in game.listeners)
2018-03-26 10:42:58 +02:00
{
2018-03-29 01:13:39 +02:00
game.listeners[event].forEach
2018-03-26 10:42:58 +02:00
(
2018-03-28 13:59:29 +02:00
procedure =>
2018-03-26 10:42:58 +02:00
{
2018-03-28 13:59:29 +02:00
procedure(data);
2018-03-26 10:42:58 +02:00
}
)
;
}
else
{
2018-03-28 13:59:29 +02:00
let message : string = "kein Ereignis mit diesem Name";
throw (new Error(message));
2018-03-26 10:42:58 +02:00
}
}
/**
* @author kcf <vidofnir@folksprak.org>
*/
2018-03-28 13:59:29 +02:00
export function reset
2018-03-28 11:19:46 +02:00
(
2018-03-29 01:13:39 +02:00
game : type_game,
2018-03-28 13:59:29 +02:00
inform : boolean = true
2018-03-28 11:19:46 +02:00
)
: void
2018-03-26 10:42:58 +02:00
{
2018-03-29 01:13:39 +02:00
game.token = (lib_errormonade.create_nothing<mod_token.type_token>());
game.testindex = (lib_errormonade.create_nothing<int>());
game.mode = mod_mode.initial;
2018-03-28 13:59:29 +02:00
if (inform)
2018-03-26 10:42:58 +02:00
{
2018-03-29 01:13:39 +02:00
notify(game, "change_token", {});
notify(game, "change_mode", {});
2018-03-26 10:42:58 +02:00
}
else
{
2018-03-28 13:59:29 +02:00
// nothing tun
2018-03-26 10:42:58 +02:00
}
}
/**
* @author kcf <vidofnir@folksprak.org>
*/
2018-03-28 13:59:29 +02:00
export function task_read
2018-03-28 11:19:46 +02:00
(
2018-03-29 01:13:39 +02:00
game : type_game
2018-03-28 11:19:46 +02:00
)
2018-03-28 13:59:29 +02:00
: mod_task.type_task
2018-03-26 10:42:58 +02:00
{
2018-03-29 01:13:39 +02:00
return game.task;
2018-03-26 10:42:58 +02:00
}
/**
* @author kcf <vidofnir@folksprak.org>
*/
2018-03-28 13:59:29 +02:00
export function task_set
2018-03-28 11:19:46 +02:00
(
2018-03-29 01:13:39 +02:00
game : type_game,
2018-03-28 13:59:29 +02:00
task : mod_task.type_task
2018-03-28 11:19:46 +02:00
) : void
2018-03-26 10:42:58 +02:00
{
2018-03-29 01:13:39 +02:00
game.task = task;
// game.world_clear();
notify(game, "change_task", {});
reset(game);
2018-03-26 10:42:58 +02:00
}
/**
* @author kcf <vidofnir@folksprak.org>
*/
2018-03-28 13:59:29 +02:00
export function world_read
2018-03-28 11:19:46 +02:00
(
2018-03-29 01:13:39 +02:00
game : type_game
2018-03-28 11:19:46 +02:00
)
2018-03-28 13:59:29 +02:00
: mod_world.type_world
2018-03-26 10:42:58 +02:00
{
2018-03-29 01:13:39 +02:00
return game.world;
2018-03-26 10:42:58 +02:00
}
/**
* @author kcf <vidofnir@folksprak.org>
*/
2018-03-28 13:59:29 +02:00
export function world_set
2018-03-28 11:19:46 +02:00
(
2018-03-29 01:13:39 +02:00
game : type_game,
2018-03-28 13:59:29 +02:00
world : mod_world.type_world,
inform : boolean = true
2018-03-28 11:19:46 +02:00
)
: void
2018-03-26 10:42:58 +02:00
{
2018-03-29 01:13:39 +02:00
game.world = world;
2018-03-28 13:59:29 +02:00
if (inform)
2018-03-26 10:42:58 +02:00
{
2018-03-29 01:13:39 +02:00
notify(game, "change_world", {});
2018-03-26 10:42:58 +02:00
}
else
{
2018-03-28 13:59:29 +02:00
// nothing tun
2018-03-26 10:42:58 +02:00
}
}
/**
* @author kcf <vidofnir@folksprak.org>
*/
2018-03-28 13:59:29 +02:00
export function world_clear
2018-03-28 11:19:46 +02:00
(
2018-03-29 01:13:39 +02:00
game : type_game,
2018-03-28 13:59:29 +02:00
inform : boolean = true
2018-03-28 11:19:46 +02:00
)
: void
{
2018-03-29 01:13:39 +02:00
game.world = mod_world.blanko();
2018-03-28 13:59:29 +02:00
if (inform)
2018-03-26 10:42:58 +02:00
{
2018-03-29 01:13:39 +02:00
notify(game, "change_world", {});
2018-03-26 10:42:58 +02:00
}
else
{
2018-03-28 13:59:29 +02:00
// nothing tun
2018-03-26 10:42:58 +02:00
}
}
2018-03-26 10:42:58 +02:00
/**
* @author kcf <vidofnir@folksprak.org>
*/
2018-03-28 13:59:29 +02:00
export function token_read
2018-03-28 11:19:46 +02:00
(
2018-03-29 01:13:39 +02:00
game : type_game
2018-03-28 11:19:46 +02:00
)
2018-03-28 13:59:29 +02:00
: lib_errormonade.type_errormonade<mod_token.type_token>
2017-11-09 14:06:35 +01:00
{
2018-03-29 01:13:39 +02:00
return game.token;
2018-03-26 10:42:58 +02:00
}
/**
* @author kcf <vidofnir@folksprak.org>
*/
2018-03-28 13:59:29 +02:00
export function mode_read
2018-03-28 11:19:46 +02:00
(
2018-03-29 01:13:39 +02:00
game : type_game
2018-03-28 11:19:46 +02:00
)
2018-03-28 13:59:29 +02:00
: mod_mode.type_mode
2018-03-26 10:42:58 +02:00
{
2018-03-29 01:13:39 +02:00
return game.mode;
2018-03-26 10:42:58 +02:00
}
/**
* @author kcf <vidofnir@folksprak.org>
*/
2018-03-28 13:59:29 +02:00
export function world_tile_wechseln
2018-03-28 11:19:46 +02:00
(
2018-03-29 01:13:39 +02:00
game : type_game,
2018-03-28 13:59:29 +02:00
spot : mod_spot.type_spot,
2018-03-28 11:19:46 +02:00
umgekehrt : boolean = false
)
: void
2018-03-26 10:42:58 +02:00
{
2018-03-29 01:13:39 +02:00
if (! (game.mode === mod_mode.initial))
2017-11-09 14:06:35 +01:00
{
2018-03-28 13:59:29 +02:00
let message : string = "gesperrt";
2017-11-09 14:06:35 +01:00
}
else
2017-11-09 14:06:35 +01:00
{
2018-03-29 01:13:39 +02:00
mod_world.tile_wechseln(game.world, spot, umgekehrt);
2018-03-28 13:59:29 +02:00
notify
2018-03-26 10:42:58 +02:00
(
2018-03-29 01:13:39 +02:00
game,
"change_world",
2018-03-26 10:42:58 +02:00
{
2018-03-28 13:59:29 +02:00
"kind": "tile_wechseln",
"data":
2018-03-26 10:42:58 +02:00
{
2018-03-28 13:59:29 +02:00
"spot": spot,
2018-03-26 10:42:58 +02:00
"umgekehrt": umgekehrt,
2018-03-29 01:13:39 +02:00
"tile": mod_world.tile_get(game.world, spot),
2018-03-26 10:42:58 +02:00
}
}
)
;
}
}
/**
* @author kcf <vidofnir@folksprak.org>
*/
2018-03-28 13:59:29 +02:00
export function world_tile_rotate
2018-03-28 11:19:46 +02:00
(
2018-03-29 01:13:39 +02:00
game : type_game,
2018-03-28 13:59:29 +02:00
spot : mod_spot.type_spot,
2018-03-28 11:19:46 +02:00
inkrement : int = +1
)
: void
2018-03-26 10:42:58 +02:00
{
2018-03-29 01:13:39 +02:00
if (! (game.mode === mod_mode.initial))
2018-03-26 10:42:58 +02:00
{
2018-03-28 13:59:29 +02:00
let message : string = "gesperrt";
2018-03-26 10:42:58 +02:00
}
else
{
2018-03-29 01:13:39 +02:00
mod_world.tile_rotate(game.world, spot, inkrement);
notify(game, "change_world", {});
2018-03-26 10:42:58 +02:00
}
}
/**
* @author kcf <vidofnir@folksprak.org>
*/
2018-03-28 13:59:29 +02:00
export function resume
2018-03-28 11:19:46 +02:00
(
2018-03-29 01:13:39 +02:00
game : type_game
2018-03-28 11:19:46 +02:00
)
: void
2018-03-26 10:42:58 +02:00
{
2018-03-29 01:13:39 +02:00
switch (game.mode)
2018-03-26 10:42:58 +02:00
{
2018-03-28 13:59:29 +02:00
case mod_mode.initial:
2017-11-09 14:06:35 +01:00
{
2018-03-29 01:13:39 +02:00
game.mode = mod_mode.uncertain;
game.testindex = (lib_errormonade.create_just<int>(0));
notify(game, "change_mode", {});
2018-03-26 10:42:58 +02:00
break;
2017-11-09 14:06:35 +01:00
}
2018-03-28 13:59:29 +02:00
case mod_mode.uncertain:
2017-11-09 14:06:35 +01:00
{
2018-03-29 01:13:39 +02:00
if (! lib_errormonade.filled<mod_token.type_token>(game.token))
2017-11-09 14:06:35 +01:00
{
2018-03-29 01:13:39 +02:00
let test : mod_test.type_test = mod_task.tests(game.task)[lib_errormonade.read<int>(game.testindex)];
2018-03-28 13:59:29 +02:00
let tape : Array<mod_symbol.type_symbol> = mod_vtm.mod_helfer.list_copy<mod_symbol.type_symbol>(mod_test.input(test));
2018-03-29 01:13:39 +02:00
let spot : mod_spot.type_spot = mod_world.generator_finden(game.world);
game.token = (
2018-03-28 13:59:29 +02:00
lib_errormonade.create_just<mod_token.type_token>
2018-03-26 10:42:58 +02:00
(
2018-03-28 13:59:29 +02:00
mod_token.create
2018-03-26 10:42:58 +02:00
(
2018-03-28 13:59:29 +02:00
tape,
spot
2018-03-26 10:42:58 +02:00
)
)
);
2017-11-09 14:06:35 +01:00
}
else
2017-11-09 14:06:35 +01:00
{
2018-03-29 01:13:39 +02:00
let token : mod_token.type_token = lib_errormonade.read<mod_token.type_token>(game.token);
2018-03-28 13:59:29 +02:00
let spot : mod_spot.type_spot = mod_token.spot_read(token);
2018-03-29 01:13:39 +02:00
let actuator_ : lib_errormonade.type_errormonade<mod_actuator.type_actuator> = mod_world.tile_get(game.world, spot);
2018-03-28 13:59:29 +02:00
let actuator : mod_actuator.type_actuator = (
lib_errormonade.filled<mod_actuator.type_actuator>(actuator_)
? lib_errormonade.read<mod_actuator.type_actuator>(actuator_)
2018-03-29 01:13:39 +02:00
: mod_actuator.mod_rejector.create_extended()
2018-03-27 17:24:31 +02:00
);
2018-03-28 13:59:29 +02:00
mod_actuator.use(actuator, token);
let state : mod_state.type_state = mod_token.state_read(token);
if (state === mod_state.running)
2018-03-26 10:42:58 +02:00
{
2018-03-28 13:59:29 +02:00
// nothing tun
2018-03-26 10:42:58 +02:00
}
2018-03-28 13:59:29 +02:00
else if ((state === mod_state.accepted) || (state === mod_state.rejected))
2017-11-09 14:06:35 +01:00
{
2018-03-28 13:59:29 +02:00
let accepted : boolean = (state === mod_state.accepted);
let ausgabe : Array<mod_symbol.type_symbol> = mod_token.tape_read(token);
2018-03-29 01:13:39 +02:00
game.token = (lib_errormonade.create_nothing<mod_token.type_token>());
let testindex : int = lib_errormonade.read<int>(game.testindex);
let tests : Array<mod_test.type_test> = mod_task.tests(game.task);
2018-03-28 13:59:29 +02:00
let test : mod_test.type_test = tests[testindex];
if (! mod_test.pruefen(test, accepted, ausgabe))
2018-03-26 10:42:58 +02:00
{
2018-03-29 01:13:39 +02:00
game.mode = mod_mode.wrong;
notify(game, "change_mode", {});
2018-03-26 10:42:58 +02:00
}
else
{
testindex += 1;
if (testindex < tests.length)
{
// nächsten Test auswählen
2018-03-29 01:13:39 +02:00
game.testindex = (lib_errormonade.create_just<int>(testindex));
2018-03-26 10:42:58 +02:00
}
else
{
2018-03-28 13:59:29 +02:00
// auf Modus "correct" wechseln
2018-03-29 01:13:39 +02:00
game.testindex = (lib_errormonade.create_nothing<int>());
game.mode = mod_mode.correct;
notify(game, "change_mode", {});
2018-03-26 10:42:58 +02:00
}
}
2017-11-09 14:06:35 +01:00
}
else
{
2018-03-28 13:59:29 +02:00
let message : string = "unbehandelter Zustand";
throw (new Error(message));
2017-11-09 14:06:35 +01:00
}
}
2018-03-29 01:13:39 +02:00
notify(game, "change_token", {});
2018-03-26 10:42:58 +02:00
break;
}
2018-03-28 13:59:29 +02:00
case mod_mode.wrong:
2018-03-26 10:42:58 +02:00
{
2018-03-28 13:59:29 +02:00
// nothing tun
2018-03-26 10:42:58 +02:00
break;
2017-11-09 14:06:35 +01:00
}
2018-03-28 13:59:29 +02:00
case mod_mode.correct:
{
2018-03-28 13:59:29 +02:00
// nothing tun
2018-03-26 10:42:58 +02:00
break;
}
default:
{
2018-03-28 13:59:29 +02:00
let message : string = "unbehandelter Modus";
throw (new Error(message));
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
}
}