vtm/source/model/world.ts

404 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 22:00:42 +02:00
* Copyright (C) 2016-2018 kcf <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/>.
*/
2017-11-08 11:30:34 +01:00
2025-09-23 12:13:49 +02:00
namespace mod_vtm
2017-11-08 11:30:34 +01:00
{
2025-09-23 12:13:49 +02:00
export namespace mod_model
2017-11-08 11:30:34 +01:00
{
2018-03-26 10:42:58 +02:00
2025-09-23 12:13:49 +02:00
export namespace mod_world
2018-03-26 10:42:58 +02:00
{
/**
* @author kcf <vidofnir@folksprak.org>
*/
2018-03-28 13:59:29 +02:00
export type type_world =
2018-03-26 10:42:58 +02:00
{
2018-03-28 13:59:29 +02:00
tiles : lib_hashmap.type_hashmap<mod_spot.type_spot, mod_actuator.type_actuator>;
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 create
2018-03-26 10:42:58 +02:00
(
2018-03-28 13:59:29 +02:00
tiles : lib_hashmap.type_hashmap<mod_spot.type_spot, mod_actuator.type_actuator> = lib_hashmap.create<mod_spot.type_spot, mod_actuator.type_actuator>(mod_spot.hash)
2018-03-26 10:42:58 +02:00
)
2018-03-28 13:59:29 +02:00
: type_world
2018-03-26 10:42:58 +02:00
{
return {
2018-03-28 13:59:29 +02:00
"tiles": tiles,
2018-03-26 10:42:58 +02:00
};
}
/**
* @author kcf <vidofnir@folksprak.org>
*/
2018-03-28 13:59:29 +02:00
export function tiles_read
2018-03-28 11:19:46 +02:00
(
2018-03-28 13:59:29 +02:00
world : type_world
2018-03-28 11:19:46 +02:00
)
2018-03-28 13:59:29 +02:00
: Array<{spot : mod_spot.type_spot; actuator : mod_actuator.type_actuator;}>
2018-03-26 10:42:58 +02:00
{
2018-03-28 13:59:29 +02:00
let tiles : Array<{spot : mod_spot.type_spot; actuator : mod_actuator.type_actuator;}> = [];
2020-05-03 12:25:05 +02:00
lib_hashmap.iterate
2018-03-26 10:42:58 +02:00
(
2018-03-28 13:59:29 +02:00
world.tiles,
2020-05-03 12:25:05 +02:00
(spot, actuator) => {tiles.push({"spot": spot, "actuator": actuator});}
2018-03-26 10:42:58 +02:00
)
;
2018-03-28 13:59:29 +02:00
return tiles;
2018-03-26 10:42:58 +02:00
}
/**
* @author kcf <vidofnir@folksprak.org>
*/
2018-03-28 13:59:29 +02:00
export function tile_get
2018-03-28 11:19:46 +02:00
(
2018-03-28 13:59:29 +02:00
world : type_world,
spot : mod_spot.type_spot
2018-03-28 11:19:46 +02:00
)
2018-03-28 13:59:29 +02:00
: lib_errormonade.type_errormonade<mod_actuator.type_actuator>
2018-03-26 10:42:58 +02:00
{
2018-03-28 13:59:29 +02:00
return lib_hashmap.get(world.tiles, spot);
2018-03-26 10:42:58 +02:00
}
/**
* @author kcf <vidofnir@folksprak.org>
*/
2018-03-28 13:59:29 +02:00
export function tile_set
2018-03-28 11:19:46 +02:00
(
2018-03-28 13:59:29 +02:00
world : type_world,
spot : mod_spot.type_spot,
actuator : mod_actuator.type_actuator
2018-03-28 11:19:46 +02:00
)
: void
2018-03-26 10:42:58 +02:00
{
2018-03-28 13:59:29 +02:00
lib_hashmap.set(world.tiles, spot, actuator);
2018-03-26 10:42:58 +02:00
}
/**
* @author kcf <vidofnir@folksprak.org>
*/
2018-03-29 22:00:42 +02:00
export function tile_change
2018-03-28 11:19:46 +02:00
(
2018-03-28 13:59:29 +02:00
world : type_world,
spot : mod_spot.type_spot,
2018-03-29 22:00:42 +02:00
inverted : boolean = false
2018-03-28 11:19:46 +02:00
)
: void
2018-03-26 10:42:58 +02:00
{
2020-05-03 12:25:05 +02:00
const extended : boolean = true;
const liste : Array<{pruefer : (actuator : mod_actuator.type_actuator)=>boolean; erspotr : ()=>mod_actuator.type_actuator;}> = (
2018-03-26 10:42:58 +02:00
[]
.concat
(
[
{
2018-03-29 01:13:39 +02:00
"pruefer": (actuator) => (actuator.kind === "conveyer"),
"erspotr": () => mod_actuator.mod_conveyer.create_extended(0),
2018-03-26 10:42:58 +02:00
},
]
)
.concat
(
2018-03-29 22:00:42 +02:00
lib_list.sequence(extended ? 4 : 2).map
2018-03-26 10:42:58 +02:00
(
symbol => (
{
2018-03-28 13:59:29 +02:00
"pruefer": (actuator) =>
2018-03-26 10:42:58 +02:00
{
2018-03-29 01:13:39 +02:00
if (actuator.kind === "writer")
2018-03-26 10:42:58 +02:00
{
2018-03-29 01:13:39 +02:00
return (mod_actuator.mod_writer.symbol_read(actuator.data) === symbol);
2018-03-26 10:42:58 +02:00
}
else
{
return false;
}
}
,
2018-03-29 01:13:39 +02:00
"erspotr": () => mod_actuator.mod_writer.create_extended(0, symbol)
}
2018-03-26 10:42:58 +02:00
)
)
)
.concat
(
2018-03-29 22:00:42 +02:00
lib_list.sequence(extended ? 2 : 1).map
2018-03-26 10:42:58 +02:00
(
index =>
{
2018-03-28 13:59:29 +02:00
let symbol_links : mod_symbol.type_symbol = (2*index+0);
let symbol_rechts : mod_symbol.type_symbol = (2*index+1);
2018-03-26 10:42:58 +02:00
return (
{
2018-03-28 13:59:29 +02:00
"pruefer": (actuator) =>
2018-03-26 10:42:58 +02:00
{
2018-03-29 01:13:39 +02:00
if (actuator.kind === "reader")
2018-03-26 10:42:58 +02:00
{
return (
2018-03-29 01:13:39 +02:00
(mod_actuator.mod_reader.symbol_links_read(actuator.data) === symbol_links)
2018-03-26 10:42:58 +02:00
&&
2018-03-29 01:13:39 +02:00
(mod_actuator.mod_reader.symbol_rechts_read(actuator.data) === symbol_rechts)
2018-03-26 10:42:58 +02:00
);
}
else
{
return false;
}
}
,
2018-03-29 01:13:39 +02:00
"erspotr": () => mod_actuator.mod_reader.create_extended(0, symbol_links, symbol_rechts)
2018-03-26 10:42:58 +02:00
}
);
}
2018-03-26 10:42:58 +02:00
)
)
.concat
(
[
2017-11-08 19:47:56 +01:00
{
2018-03-29 01:13:39 +02:00
"pruefer": (actuator) => (actuator.kind === "rejector"),
"erspotr": () => mod_actuator.mod_rejector.create_extended(),
2018-03-26 10:42:58 +02:00
},
]
)
);
2018-03-28 13:59:29 +02:00
let index_alt : lib_errormonade.type_errormonade<int>;
2020-05-03 12:25:05 +02:00
const actuator_alt_ : lib_errormonade.type_errormonade<mod_actuator.type_actuator> = lib_hashmap.get(world.tiles, spot);
2018-03-28 13:59:29 +02:00
if (lib_errormonade.filled<mod_actuator.type_actuator>(actuator_alt_))
2018-03-26 10:42:58 +02:00
{
2020-05-03 12:25:05 +02:00
const actuator_alt : mod_actuator.type_actuator = lib_errormonade.read(actuator_alt_);
const gefunden : boolean = (
liste
.some
2018-03-26 10:42:58 +02:00
(
2018-03-30 01:06:13 +02:00
(entry, index) =>
2017-11-08 19:47:56 +01:00
{
2018-03-30 01:06:13 +02:00
if (entry.pruefer(actuator_alt))
2017-11-08 19:47:56 +01:00
{
2018-03-28 13:59:29 +02:00
index_alt = (lib_errormonade.create_just<int>(index));
2018-03-26 10:42:58 +02:00
return true;
2017-11-08 19:47:56 +01:00
}
else
{
return false;
}
}
2018-03-26 10:42:58 +02:00
)
);
if (! gefunden)
{
2018-03-28 13:59:29 +02:00
index_alt = (lib_errormonade.create_nothing<int>());
}
else
2017-11-08 18:41:56 +01:00
{
2018-03-28 13:59:29 +02:00
// nothing tun
2017-11-08 18:41:56 +01:00
}
}
2018-03-26 10:42:58 +02:00
else
{
2020-05-03 12:25:05 +02:00
const message : string = "kein Aktor gesetzt";
2018-03-28 13:59:29 +02:00
// console.warn(message);
index_alt = (lib_errormonade.create_just<int>(0));
2018-03-26 10:42:58 +02:00
}
2018-03-28 13:59:29 +02:00
if (lib_errormonade.filled<int>(index_alt))
2018-03-26 10:42:58 +02:00
{
2020-05-03 12:25:05 +02:00
const index_neu : int = lib_math.mod(lib_errormonade.read<int>(index_alt) + (inverted ? -1 : +1), liste.length);
const actuator_neu : mod_actuator.type_actuator = liste[index_neu].erspotr();
2018-03-28 13:59:29 +02:00
tile_set(world, spot, actuator_neu);
2018-03-26 10:42:58 +02:00
}
else
{
2020-05-03 12:25:05 +02:00
const message : string = ("Aktor nicht gefunden");
2018-03-28 13:59:29 +02:00
// throw (new Error(message));
console.warn(message);
2018-03-26 10:42:58 +02:00
}
2017-11-08 18:41:56 +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 tile_rotate
2018-03-28 11:19:46 +02:00
(
2018-03-28 13:59:29 +02:00
world : type_world,
spot : mod_spot.type_spot,
2018-03-29 22:00:42 +02:00
increment : int = +1
2018-03-28 11:19:46 +02:00
)
: void
{
2018-03-28 13:59:29 +02:00
let actuator_ : lib_errormonade.type_errormonade<mod_actuator.type_actuator> = lib_hashmap.get(world.tiles, spot);
if (lib_errormonade.filled<mod_actuator.type_actuator>(actuator_))
{
2018-03-29 22:00:42 +02:00
mod_actuator.rotate(lib_errormonade.read<mod_actuator.type_actuator>(actuator_), increment);
2018-03-26 10:42:58 +02:00
}
else
{
console.warn("kein Aktor gesetzt");
}
}
/**
* @author kcf <vidofnir@folksprak.org>
* @throws {Error}
*/
2018-03-29 01:13:39 +02:00
export function generator_finden
2018-03-28 11:19:46 +02:00
(
2018-03-28 13:59:29 +02:00
world : type_world
2018-03-28 11:19:46 +02:00
)
2018-03-28 13:59:29 +02:00
: mod_spot.type_spot
2018-03-26 10:42:58 +02:00
{
2018-03-28 13:59:29 +02:00
let spot : lib_errormonade.type_errormonade<mod_spot.type_spot> = (lib_errormonade.create_nothing<mod_spot.type_spot>());
lib_hashmap.iterate
2018-03-26 10:42:58 +02:00
(
2018-03-28 13:59:29 +02:00
world.tiles,
(spot_, actuator) =>
{
2018-03-29 01:13:39 +02:00
if (actuator.kind === "generator")
2018-03-26 10:42:58 +02:00
{
2018-03-28 13:59:29 +02:00
if (lib_errormonade.filled<mod_spot.type_spot>(spot))
2018-03-26 10:42:58 +02:00
{
2020-05-03 12:25:05 +02:00
const message : string = "mehrere Erzeuger gefunden";
2018-03-28 13:59:29 +02:00
throw (new Error(message));
2018-03-26 10:42:58 +02:00
}
else
{
2018-03-28 13:59:29 +02:00
spot = (lib_errormonade.create_just<mod_spot.type_spot>(spot_));
2018-03-26 10:42:58 +02:00
}
}
}
2018-03-26 10:42:58 +02:00
)
;
2018-03-28 13:59:29 +02:00
if (lib_errormonade.filled<mod_spot.type_spot>(spot))
2018-03-26 10:42:58 +02:00
{
2018-03-28 13:59:29 +02:00
return lib_errormonade.read<mod_spot.type_spot>(spot);
2018-03-26 10:42:58 +02:00
}
else
{
2020-05-03 12:25:05 +02:00
const message : string = "kein Erzeuger gefunden";
2018-03-28 13:59:29 +02:00
throw (new Error(message));
2018-03-26 10:42:58 +02:00
}
}
/**
* @author kcf <vidofnir@folksprak.org>
*/
2018-03-28 11:19:46 +02:00
export function blanko
(
groesse : int = 3
)
2018-03-28 13:59:29 +02:00
: type_world
2018-03-26 10:42:58 +02:00
{
2018-03-28 13:59:29 +02:00
let world : type_world = create();
2018-03-26 10:42:58 +02:00
for (let u : int = -groesse; u <= +groesse; u += 1)
{
for (let v : int = -groesse; v <= +groesse; v += 1)
{
2018-03-26 10:42:58 +02:00
if (Math.abs(u-v) <= groesse)
{
2020-05-03 12:25:05 +02:00
const spot : mod_spot.type_spot = {"u": u, "v": v};
2018-03-28 13:59:29 +02:00
let actuator : mod_actuator.type_actuator;
2018-03-26 10:42:58 +02:00
if ((u === -groesse) && (v === 0))
{
2018-03-29 01:13:39 +02:00
actuator = mod_actuator.mod_generator.create_extended(0);
2018-03-26 10:42:58 +02:00
}
else if ((u === +groesse) && (v === 0))
{
2018-03-29 01:13:39 +02:00
actuator = mod_actuator.mod_acceptor.create_extended();
2018-03-26 10:42:58 +02:00
}
else
{
2018-03-29 01:13:39 +02:00
actuator = mod_actuator.mod_rejector.create_extended();
2018-03-26 10:42:58 +02:00
}
2018-03-28 13:59:29 +02:00
lib_hashmap.set(world.tiles, spot, actuator);
2018-03-26 10:42:58 +02:00
}
}
2018-03-26 10:42:58 +02:00
}
2018-03-28 13:59:29 +02:00
return world;
2018-03-26 10:42:58 +02:00
}
/**
* @author kcf <vidofnir@folksprak.org>
*/
2018-03-28 13:59:29 +02:00
export function export_
2018-03-28 11:19:46 +02:00
(
2018-03-28 13:59:29 +02:00
world : type_world
2018-03-28 11:19:46 +02:00
)
: any
2018-03-26 10:42:58 +02:00
{
2018-03-28 13:59:29 +02:00
let raw : any = {};
raw["tiles"] = {};
lib_hashmap.iterate
2018-03-26 10:42:58 +02:00
(
2018-03-28 13:59:29 +02:00
world.tiles,
(spot, actuator) =>
{
2020-05-03 12:25:05 +02:00
const spot_ : string = mod_spot.export_(spot);
const actuator_ : any = mod_actuator.export_(actuator);
2018-03-28 13:59:29 +02:00
raw["tiles"][spot_] = actuator_;
}
2018-03-26 10:42:58 +02:00
)
;
2018-03-28 13:59:29 +02:00
return raw;
}
2018-03-26 10:42:58 +02:00
/**
* @author kcf <vidofnir@folksprak.org>
*/
2018-03-28 13:59:29 +02:00
export function import_
2018-03-28 11:19:46 +02:00
(
2018-03-28 13:59:29 +02:00
raw : any
2018-03-28 11:19:46 +02:00
)
2018-03-28 13:59:29 +02:00
: type_world
{
2018-03-28 13:59:29 +02:00
let tiles : lib_hashmap.type_hashmap<mod_spot.type_spot, mod_actuator.type_actuator> = (lib_hashmap.create<mod_spot.type_spot, mod_actuator.type_actuator>(mod_spot.hash));
for (let spot_ in raw["tiles"])
2018-03-26 10:42:58 +02:00
{
2020-05-03 12:25:05 +02:00
const spot : mod_spot.type_spot = mod_spot.import_(spot_);
const actuator_ : mod_actuator.type_actuator = raw["tiles"][spot_];
const actuator : mod_actuator.type_actuator = mod_actuator.import_(actuator_);
2018-03-28 13:59:29 +02:00
lib_hashmap.set(tiles, spot, actuator);
2018-03-26 10:42:58 +02:00
}
return (
2018-03-28 13:59:29 +02:00
create
2018-03-26 10:42:58 +02:00
(
2018-03-28 13:59:29 +02:00
tiles
2018-03-26 10:42:58 +02:00
)
);
}
2018-03-26 10:42:58 +02:00
}
2018-03-26 10:42:58 +02:00
2017-11-08 11:30:34 +01:00
}
}