Combat
Terminal game1327 lines of .st1332 lines of generated Rust
================================ Health: 19 / 20 ===========================---- g###..#####.........Explore the Dungeon. Cursor keys to move.############ .###..#####.........####################.#.........#####.################ ..##....g.......g...####################.#.........#####.################ ..##..#########.#################....###.#####..########.################ ......#########.#################g...###.#####..########.################ ..#############.########........###.####.#####..########.################ ###############.########........###.####.#####..########.################ ###############.########........###.####.#####..########.################ ###############.########....o...###.####.#####..########.##.......####### ###############.########.###.######.####.#####..########.##.......####### ###############.#####........######.#.....####..########.##.......####### ###############.#####........######.#...@.####..########.##.......####### ###############.#####g.......######.#..o..####..########g.........####### ###############.#####........######.#.....####..########.##.......####### ###############.#####........######.#.....####..########.##.......####### ###############.#####........######.#.....##.......#####.##.......####### ###############.#####........######.###..###.......#####.################ ###############.#####........######.###..###.......#####.################ ###############.#####........######.###..###.......#####.################ ###############.########.###.######.###..###.......#####.################ ###############.########.###.######.###..###g......#####.################ ###############.########.###.######.###..###.......#####.################ ###############.########.##......##.###..###############.################ ###############.########.##.g....##.##.g..##############.################
On this page
The .st files#
intent.st
structop {
type_app {
exec = {
lang = "Rust"
}
}
lib_provider {
core = {
source = "StructOp/core"
version = "@latest"
}
Screen = {
source = "StructOp/game"
version = "@latest"
}
}
}main.st
ability "call" "NewMap" {
function = ability.function.build_map
}
ability "call" "SpawnPlayer" {
function = ability.function.spawn_player
args = [data.value.player_start.current.x, data.value.player_start.current.y]
depends_on = [ability.call.NewMap]
}
ability "call" "SpawnMonsters" {
function = ability.function.spawn_monsters
depends_on = [ability.call.SpawnPlayer]
}
ability "call" "NewCamera" {
function = ability.function.on_player_move
args = [data.value.player_start.current.x, data.value.player_start.current.y]
depends_on = [ability.call.SpawnMonsters]
}
ability "game" "Dungeon" {
title = "Dungeon Crawler"
width = const.DISPLAY_WIDTH
height = const.DISPLAY_HEIGHT
fps = 30
key = ability.value.key
on_tick = [ability.clear.Screen, ability.switch.Turn]
depends_on = [ability.call.NewCamera]
}
ability "clear" "Screen" {
}
ability "switch" "Turn" {
value = data.value.turn_state.current
cases = [
{ value = TurnState.AwaitingInput, run = [ability.call.InputSystems] },
{ value = TurnState.PlayerTurn, run = [ability.call.PlayerSystems] },
{ value = TurnState.MonsterTurn, run = [ability.call.MonsterSystems] },
]
}
ability "call" "InputSystems" {
function = ability.function.input_systems
}
ability "call" "PlayerSystems" {
function = ability.function.player_systems
}
ability "call" "MonsterSystems" {
function = ability.function.monster_systems
}vars.st
constant "SCREEN_WIDTH" {
type = Int
default = 80
}
constant "SCREEN_HEIGHT" {
type = Int
default = 50
}
constant "DISPLAY_WIDTH" {
type = Int
default = 80
}
constant "DISPLAY_HEIGHT" {
type = Int
default = 25
}
ability "value" "camera" {
type = Camera
}
data "value" "camera" {
of = ability.value.camera
}
# SCREEN_WIDTH * SCREEN_HEIGHT
constant "NUM_TILES" {
type = Int
default = 4000
}
ability "value" "tiles" {
type = Array
of = TileType
size = 4000
}
data "value" "tiles" {
of = ability.value.tiles
}
# map_builder.player_start
ability "value" "player_start" {
type = Point
}
data "value" "player_start" {
of = ability.value.player_start
}
constant "NUM_ROOMS" {
type = Int
default = 20
}
ability "value" "rooms" {
type = List
of = Rect
size = 20
}
data "value" "rooms" {
of = ability.value.rooms
}
# the sorted copy build_corridors works on
ability "value" "sorted" {
type = List
of = Rect
size = 20
}
data "value" "sorted" {
of = ability.value.sorted
}
ability "value" "key" {
type = Key
}
data "value" "key" {
of = ability.value.key
}
ability "value" "turn_state" {
type = TurnState
initial = TurnState.AwaitingInput
}
data "value" "turn_state" {
of = ability.value.turn_state
}camera.st
ability "struct" "Camera" {
fields = [
{ name = "left_x", type = Int },
{ name = "right_x", type = Int },
{ name = "top_y", type = Int },
{ name = "bottom_y", type = Int },
]
}
# Camera::new(player_position) and on_player_move(player_position): centred on the player
ability "function" "on_player_move" {
params = [{ name = "x", type = Int }, { name = "y", type = Int }]
body = [ability.set.Camera]
}
ability "set" "Camera" {
target = ability.value.camera
value = {
left_x = x - const.DISPLAY_WIDTH / 2
right_x = x + const.DISPLAY_WIDTH / 2
top_y = y - const.DISPLAY_HEIGHT / 2
bottom_y = y + const.DISPLAY_HEIGHT / 2
}
}components.st
# bracket-lib's Point is a component too
ability "component" "Point" {
fields = [{ name = "x", type = Int }, { name = "y", type = Int }]
}
# ColorPair plus glyph
ability "component" "Render" {
fields = [
{ name = "fg", type = Color },
{ name = "bg", type = Color },
{ name = "glyph", type = Char },
]
}
ability "component" "Player" {
}
ability "component" "Enemy" {
}
ability "component" "MovingRandomly" {
}
# legion's Entity: an entity's number
ability "type" "Entity" {
is = Int
}
# a message of intent: an entity with only this component
ability "component" "WantsToMove" {
fields = [{ name = "entity", type = Entity }, { name = "destination", type = Point }]
}
ability "component" "Health" {
fields = [{ name = "current", type = Int }, { name = "max", type = Int }]
}
# Name(pub String)
ability "component" "Name" {
fields = [{ name = "name", type = String }]
}
ability "component" "WantsToAttack" {
fields = [{ name = "attacker", type = Entity }, { name = "victim", type = Entity }]
}map.st
ability "enum" "TileType" {
values = [Wall, Floor]
}
ability "function" "map_idx" {
params = [{ name = "x", type = Int }, { name = "y", type = Int }]
returns = Int
return = y * const.SCREEN_WIDTH + x
}
# Map::new(): every tile a floor
ability "function" "new_map" {
locals = [{ name = "i", type = Int }]
body = [ability.for.Fill]
}
ability "for" "Fill" {
counter = i
from = 0
while = i < const.NUM_TILES
next = i + 1
body = [ability.set.Floor]
}
ability "set" "Floor" {
target = ability.value.tiles[i]
value = TileType.Floor
}
ability "function" "map_render" {
locals = [{ name = "x", type = Int }, { name = "y", type = Int }]
body = [ability.for.Rows]
}
ability "for" "Rows" {
counter = y
from = data.value.camera.current.top_y
while = y <= data.value.camera.current.bottom_y
next = y + 1
body = [ability.for.Columns]
}
ability "for" "Columns" {
counter = x
from = data.value.camera.current.left_x
while = x < data.value.camera.current.right_x
next = x + 1
body = [ability.if.Visible]
}
ability "if" "Visible" {
condition = in_bounds(x, y)
then = [ability.switch.Tile]
}
ability "switch" "Tile" {
value = data.value.tiles.current[map_idx(x, y)]
cases = [
{ value = TileType.Floor, run = [ability.draw.FloorTile] },
{ value = TileType.Wall, run = [ability.draw.WallTile] },
]
}
ability "draw" "FloorTile" {
x = x - data.value.camera.current.left_x
y = y - data.value.camera.current.top_y
glyph = "."
}
ability "draw" "WallTile" {
x = x - data.value.camera.current.left_x
y = y - data.value.camera.current.top_y
glyph = "#"
}
ability "function" "in_bounds" {
params = [{ name = "x", type = Int }, { name = "y", type = Int }]
returns = Bool
return = x >= 0 && x < const.SCREEN_WIDTH && y >= 0 && y < const.SCREEN_HEIGHT
}
ability "function" "can_enter_tile" {
params = [{ name = "x", type = Int }, { name = "y", type = Int }]
returns = Bool
return = in_bounds(x, y) && data.value.tiles.current[map_idx(x, y)] == TileType.Floor
}
# try_idx: Some(map_idx(x, y)) inside the map, None (-1) outside
ability "function" "try_idx" {
params = [{ name = "x", type = Int }, { name = "y", type = Int }]
returns = Int
locals = [{ name = "idx", type = Int }]
body = [ability.set.NoIdx, ability.if.Inside]
return = idx
}
ability "set" "NoIdx" {
target = idx
value = -1
}
ability "if" "Inside" {
condition = in_bounds(x, y)
then = [ability.set.SomeIdx]
}
ability "set" "SomeIdx" {
target = idx
value = map_idx(x, y)
}map_builder.st
# bracket-lib's Rect, with_size(x, y, w, h): x2 = x1 + w, y2 = y1 + h
ability "struct" "Rect" {
fields = [
{ name = "x1", type = Int },
{ name = "y1", type = Int },
{ name = "x2", type = Int },
{ name = "y2", type = Int },
]
}
ability "function" "intersect" {
params = [{ name = "a", type = Pointer, to = Rect }, { name = "b", type = Pointer, to = Rect }]
returns = Bool
return = a.x1 <= b.x2 && a.x2 >= b.x1 && a.y1 <= b.y2 && a.y2 >= b.y1
}
ability "function" "fill" {
params = [{ name = "tile", type = TileType }]
locals = [{ name = "i", type = Int }]
body = [ability.for.FillAll]
}
ability "for" "FillAll" {
counter = i
from = 0
while = i < const.NUM_TILES
next = i + 1
body = [ability.set.FillTile]
}
ability "set" "FillTile" {
target = ability.value.tiles[i]
value = tile
}
ability "function" "build_random_rooms" {
locals = [
{ name = "room", type = Rect },
{ name = "overlap", type = Bool },
{ name = "r", type = Int },
{ name = "x", type = Int },
{ name = "y", type = Int },
{ name = "w", type = Int },
{ name = "h", type = Int },
]
body = [ability.loop.Rooms]
}
ability "loop" "Rooms" {
condition = length(data.value.rooms.current) < const.NUM_ROOMS
body = [
ability.random.RoomX,
ability.random.RoomY,
ability.random.RoomW,
ability.random.RoomH,
ability.set.Room,
ability.set.NoOverlap,
ability.for.Overlaps,
ability.if.Free,
]
}
ability "random" "RoomX" {
target = x
from = 1
to = const.SCREEN_WIDTH - 10
}
ability "random" "RoomY" {
target = y
from = 1
to = const.SCREEN_HEIGHT - 10
}
ability "random" "RoomW" {
target = w
from = 2
to = 10
}
ability "random" "RoomH" {
target = h
from = 2
to = 10
}
ability "set" "Room" {
target = room
value = {
x1 = x
y1 = y
x2 = x + w
y2 = y + h
}
}
ability "set" "NoOverlap" {
target = overlap
value = false
}
ability "for" "Overlaps" {
counter = r
from = 0
while = r < length(data.value.rooms.current)
next = r + 1
body = [ability.if.Intersects]
}
ability "if" "Intersects" {
condition = intersect(address_of(ability.value.rooms[r]), address_of(room))
then = [ability.set.Overlap]
}
ability "set" "Overlap" {
target = overlap
value = true
}
ability "if" "Free" {
condition = !overlap
then = [ability.for.RoomRows, ability.add.Room]
}
ability "for" "RoomRows" {
counter = y
from = room.y1
while = y < room.y2
next = y + 1
body = [ability.for.RoomColumns]
}
ability "for" "RoomColumns" {
counter = x
from = room.x1
while = x < room.x2
next = x + 1
body = [ability.if.OnMap]
}
ability "if" "OnMap" {
condition = x > 0 && x < const.SCREEN_WIDTH && y > 0 && y < const.SCREEN_HEIGHT
then = [ability.set.Carve]
}
ability "set" "Carve" {
target = ability.value.tiles[map_idx(x, y)]
value = TileType.Floor
}
ability "add" "Room" {
list = ability.value.rooms
value = room
}
ability "function" "apply_vertical_tunnel" {
params = [{ name = "y1", type = Int }, { name = "y2", type = Int }, { name = "x", type = Int }]
locals = [{ name = "y", type = Int }, { name = "idx", type = Int }]
body = [ability.for.Vertical]
}
ability "for" "Vertical" {
counter = y
from = min(y1, y2)
while = y <= max(y1, y2)
next = y + 1
body = [ability.set.VerticalIdx, ability.if.VerticalSome]
}
ability "set" "VerticalIdx" {
target = idx
value = try_idx(x, y)
}
ability "if" "VerticalSome" {
condition = idx >= 0
then = [ability.set.VerticalFloor]
}
ability "set" "VerticalFloor" {
target = ability.value.tiles[idx]
value = TileType.Floor
}
ability "function" "apply_horizontal_tunnel" {
params = [{ name = "x1", type = Int }, { name = "x2", type = Int }, { name = "y", type = Int }]
locals = [{ name = "x", type = Int }, { name = "idx", type = Int }]
body = [ability.for.Horizontal]
}
ability "for" "Horizontal" {
counter = x
from = min(x1, x2)
while = x <= max(x1, x2)
next = x + 1
body = [ability.set.HorizontalIdx, ability.if.HorizontalSome]
}
ability "set" "HorizontalIdx" {
target = idx
value = try_idx(x, y)
}
ability "if" "HorizontalSome" {
condition = idx >= 0
then = [ability.set.HorizontalFloor]
}
ability "set" "HorizontalFloor" {
target = ability.value.tiles[idx]
value = TileType.Floor
}
ability "function" "build_corridors" {
locals = [
{ name = "i", type = Int },
{ name = "j", type = Int },
{ name = "room", type = Rect },
{ name = "prev", type = Point },
{ name = "new", type = Point },
{ name = "coin", type = Int },
]
body = [ability.for.Copy, ability.for.Sort, ability.for.Connect]
}
# let mut rooms = self.rooms.clone();
ability "for" "Copy" {
counter = i
from = 0
while = i < length(data.value.rooms.current)
next = i + 1
body = [ability.add.Copy]
}
ability "add" "Copy" {
list = ability.value.sorted
value = data.value.rooms.current[i]
}
# rooms.sort_by(|a, b| a.center().x.cmp(&b.center().x)), as a stable insertion sort
ability "for" "Sort" {
counter = i
from = 1
while = i < length(data.value.sorted.current)
next = i + 1
body = [ability.set.Key, ability.set.Below, ability.loop.Shift, ability.set.Insert]
}
ability "set" "Key" {
target = room
value = data.value.sorted.current[i]
}
ability "set" "Below" {
target = j
value = i - 1
}
ability "loop" "Shift" {
condition = j >= 0 && (data.value.sorted.current[j].x1 + data.value.sorted.current[j].x2) / 2 > (room.x1 + room.x2) / 2
body = [ability.set.ShiftUp, ability.set.Lower]
}
ability "set" "ShiftUp" {
target = ability.value.sorted[j + 1]
value = data.value.sorted.current[j]
}
ability "set" "Lower" {
target = j
value = j - 1
}
ability "set" "Insert" {
target = ability.value.sorted[j + 1]
value = room
}
ability "for" "Connect" {
counter = i
from = 1
while = i < length(data.value.sorted.current)
next = i + 1
body = [ability.set.Prev, ability.set.New, ability.random.Coin, ability.if.Coin]
}
ability "set" "Prev" {
target = prev
value = {
x = (data.value.sorted.current[i - 1].x1 + data.value.sorted.current[i - 1].x2) / 2
y = (data.value.sorted.current[i - 1].y1 + data.value.sorted.current[i - 1].y2) / 2
}
}
ability "set" "New" {
target = new
value = {
x = (data.value.sorted.current[i].x1 + data.value.sorted.current[i].x2) / 2
y = (data.value.sorted.current[i].y1 + data.value.sorted.current[i].y2) / 2
}
}
ability "random" "Coin" {
target = coin
from = 0
to = 2
}
ability "if" "Coin" {
condition = coin == 1
then = [ability.call.HorizontalFirst, ability.call.VerticalSecond]
else = [ability.call.VerticalFirst, ability.call.HorizontalSecond]
}
ability "call" "HorizontalFirst" {
function = ability.function.apply_horizontal_tunnel
args = [prev.x, new.x, prev.y]
}
ability "call" "VerticalSecond" {
function = ability.function.apply_vertical_tunnel
args = [prev.y, new.y, new.x]
}
ability "call" "VerticalFirst" {
function = ability.function.apply_vertical_tunnel
args = [prev.y, new.y, prev.x]
}
ability "call" "HorizontalSecond" {
function = ability.function.apply_horizontal_tunnel
args = [prev.x, new.x, new.y]
}
# MapBuilder::new(): the map, the rooms, and the player's start in the first room
ability "function" "build_map" {
body = [ability.call.FillWalls, ability.call.Rooms, ability.call.Corridors, ability.set.Start]
}
ability "call" "FillWalls" {
function = ability.function.fill
args = [TileType.Wall]
}
ability "call" "Rooms" {
function = ability.function.build_random_rooms
}
ability "call" "Corridors" {
function = ability.function.build_corridors
}
ability "set" "Start" {
target = ability.value.player_start
value = {
x = (data.value.rooms.current[0].x1 + data.value.rooms.current[0].x2) / 2
y = (data.value.rooms.current[0].y1 + data.value.rooms.current[0].y2) / 2
}
}schedules.st
# each Schedule is a function calling its systems in order
ability "function" "input_systems" {
body = [ability.call.InputPlayerInput, ability.call.InputMapRender, ability.call.InputEntityRender, ability.call.InputHud]
}
ability "call" "InputPlayerInput" {
function = ability.function.player_input
}
ability "call" "InputMapRender" {
function = ability.function.map_render
}
ability "call" "InputEntityRender" {
function = ability.function.entity_render
}
ability "function" "player_systems" {
body = [ability.call.PlayerCombat, ability.call.PlayerMovement, ability.call.PlayerMapRender, ability.call.PlayerEntityRender, ability.call.PlayerHud, ability.call.PlayerEndTurn]
}
ability "call" "PlayerMovement" {
function = ability.function.movement
}
ability "call" "PlayerCombat" {
function = ability.function.combat
}
ability "call" "PlayerMapRender" {
function = ability.function.map_render
}
ability "call" "PlayerEntityRender" {
function = ability.function.entity_render
}
ability "call" "PlayerEndTurn" {
function = ability.function.end_turn
}
ability "function" "monster_systems" {
body = [ability.call.MonsterRandomMove, ability.call.MonsterCombat, ability.call.MonsterMovement, ability.call.MonsterMapRender, ability.call.MonsterEntityRender, ability.call.MonsterHud, ability.call.MonsterEndTurn]
}
ability "call" "MonsterRandomMove" {
function = ability.function.random_move
}
ability "call" "MonsterMovement" {
function = ability.function.movement
}
ability "call" "MonsterMapRender" {
function = ability.function.map_render
}
ability "call" "MonsterEntityRender" {
function = ability.function.entity_render
}
ability "call" "MonsterEndTurn" {
function = ability.function.end_turn
}
ability "call" "InputHud" {
function = ability.function.hud
}
ability "call" "PlayerHud" {
function = ability.function.hud
}
ability "call" "MonsterHud" {
function = ability.function.hud
}
ability "call" "MonsterCombat" {
function = ability.function.combat
}spawner.st
ability "function" "spawn_player" {
params = [{ name = "x", type = Int }, { name = "y", type = Int }]
body = [ability.spawn.Player]
}
ability "spawn" "Player" {
components = {
Player = {}
Point = {
x = x
y = y
}
Render = {
fg = Color.white
bg = Color.black
glyph = "@"
}
Health = {
current = 20
max = 20
}
}
}
ability "function" "spawn_monster" {
params = [{ name = "x", type = Int }, { name = "y", type = Int }]
locals = [{ name = "roll", type = Int }, { name = "hp", type = Int }, { name = "name", type = String }, { name = "glyph", type = Char }]
body = [ability.random.Dice, ability.if.Goblin, ability.spawn.Monster]
}
# rng.roll_dice(1, 10)
ability "random" "Dice" {
target = roll
from = 1
to = 11
}
ability "if" "Goblin" {
condition = roll <= 8
then = [ability.set.GoblinHp, ability.set.GoblinName, ability.set.GoblinGlyph]
else = [ability.set.OrcHp, ability.set.OrcName, ability.set.OrcGlyph]
}
ability "set" "GoblinHp" {
target = hp
value = 1
}
ability "set" "GoblinName" {
target = name
value = "Goblin"
}
ability "set" "GoblinGlyph" {
target = glyph
value = "g"
}
ability "set" "OrcHp" {
target = hp
value = 2
}
ability "set" "OrcName" {
target = name
value = "Orc"
}
ability "set" "OrcGlyph" {
target = glyph
value = "o"
}
ability "spawn" "Monster" {
components = {
Enemy = {}
Point = {
x = x
y = y
}
Render = {
fg = Color.white
bg = Color.black
glyph = glyph
}
MovingRandomly = {}
Health = {
current = hp
max = hp
}
Name = {
name = name
}
}
}
# map_builder.rooms.iter().skip(1).map(|r| r.center()).for_each(|pos| spawn_monster(...))
ability "function" "spawn_monsters" {
locals = [{ name = "i", type = Int }]
body = [ability.for.Rooms]
}
ability "for" "Rooms" {
counter = i
from = 1
while = i < length(data.value.rooms.current)
next = i + 1
body = [ability.call.Monster]
}
ability "call" "Monster" {
function = ability.function.spawn_monster
args = [(data.value.rooms.current[i].x1 + data.value.rooms.current[i].x2) / 2, (data.value.rooms.current[i].y1 + data.value.rooms.current[i].y2) / 2]
}systems.st
ability "function" "player_input" {
locals = [{ name = "delta", type = Point }, { name = "destination", type = Point }, { name = "player", type = Entity }, { name = "enemy", type = Entity }, { name = "hit_something", type = Bool }]
body = [ability.switch.Move, ability.if.Moving]
}
ability "switch" "Move" {
value = data.value.key.current
cases = [
{ value = Key.left, run = [ability.set.Left] },
{ value = Key.right, run = [ability.set.Right] },
{ value = Key.up, run = [ability.set.Up] },
{ value = Key.down, run = [ability.set.Down] },
]
default = [ability.set.Zero]
}
ability "set" "Left" {
target = delta
value = {
x = -1
y = 0
}
}
ability "set" "Right" {
target = delta
value = {
x = 1
y = 0
}
}
ability "set" "Up" {
target = delta
value = {
x = 0
y = -1
}
}
ability "set" "Down" {
target = delta
value = {
x = 0
y = 1
}
}
ability "set" "Zero" {
target = delta
value = {
x = 0
y = 0
}
}
ability "if" "Moving" {
condition = data.value.key.current != Key.none
then = [ability.query.Players, ability.if.Delta, ability.set.PlayerTurn]
}
ability "query" "Players" {
with = [Point, Player]
bind = {
pos = Point
}
entity = player
body = [ability.set.Destination]
}
ability "set" "Destination" {
target = destination
value = {
x = pos.x + delta.x
y = pos.y + delta.y
}
}
ability "if" "Delta" {
condition = delta.x != 0 || delta.y != 0
then = [ability.set.NoHit, ability.query.Targets, ability.if.NoHit]
}
ability "set" "NoHit" {
target = hit_something
value = false
}
ability "query" "Targets" {
with = [Point, Enemy]
bind = {
pos = Point
}
entity = enemy
body = [ability.if.Target]
}
ability "if" "Target" {
condition = pos.x == destination.x && pos.y == destination.y
then = [ability.set.Hit, ability.spawn.PlayerAttack]
}
ability "set" "Hit" {
target = hit_something
value = true
}
ability "spawn" "PlayerAttack" {
components = {
WantsToAttack = {
attacker = player
victim = enemy
}
}
}
ability "if" "NoHit" {
condition = !hit_something
then = [ability.spawn.PlayerMove]
}
ability "spawn" "PlayerMove" {
components = {
WantsToMove = {
entity = player
destination = destination
}
}
}
ability "set" "PlayerTurn" {
target = ability.value.turn_state
value = TurnState.PlayerTurn
}
ability "function" "combat" {
locals = [{ name = "message", type = Entity }]
body = [ability.query.Attacks]
}
ability "query" "Attacks" {
with = [WantsToAttack]
bind = {
attack = WantsToAttack
}
entity = message
body = [ability.query.Victim, ability.despawn.Attack]
}
ability "query" "Victim" {
with = [Health]
only = attack.victim
bind = {
health = Health
}
body = [ability.print.Before, ability.set.Damage, ability.if.Dead, ability.print.After]
}
ability "print" "Before" {
message = "Health before attack: ${health.current}"
}
ability "set" "Damage" {
target = health.current
value = health.current - 1
}
ability "if" "Dead" {
condition = health.current < 1
then = [ability.despawn.Victim]
}
ability "despawn" "Victim" {
entity = attack.victim
}
ability "print" "After" {
message = "Health after attack: ${health.current}"
}
ability "despawn" "Attack" {
entity = message
}
ability "function" "movement" {
locals = [{ name = "message", type = Entity }]
body = [ability.query.Moves]
}
ability "query" "Moves" {
with = [WantsToMove]
bind = {
want_move = WantsToMove
}
entity = message
body = [ability.if.CanMove, ability.despawn.Message]
}
ability "if" "CanMove" {
condition = can_enter_tile(want_move.destination.x, want_move.destination.y)
then = [ability.query.Mover, ability.query.MovedPlayer]
}
ability "query" "Mover" {
with = [Point]
only = want_move.entity
bind = {
pos = Point
}
body = [ability.store.Moved]
}
ability "store" "Moved" {
pointer = pos
value = want_move.destination
}
ability "query" "MovedPlayer" {
with = [Player]
only = want_move.entity
body = [ability.call.MoveCamera]
}
ability "call" "MoveCamera" {
function = ability.function.on_player_move
args = [want_move.destination.x, want_move.destination.y]
}
ability "despawn" "Message" {
entity = message
}
ability "function" "entity_render" {
body = [ability.query.Rendered]
}
ability "query" "Rendered" {
with = [Point, Render]
bind = {
pos = Point
render = Render
}
body = [ability.draw.Entity]
}
ability "draw" "Entity" {
x = pos.x - data.value.camera.current.left_x
y = pos.y - data.value.camera.current.top_y
glyph = render.glyph
color = render.fg
background = render.bg
}
ability "function" "random_move" {
locals = [{ name = "roll", type = Int }, { name = "destination", type = Point }, { name = "mover", type = Entity }, { name = "victim", type = Entity }, { name = "attacked", type = Bool }]
body = [ability.query.Movers]
}
ability "query" "Movers" {
with = [Point, MovingRandomly]
bind = {
pos = Point
}
entity = mover
body = [ability.random.Direction, ability.switch.Direction, ability.set.NotAttacked, ability.query.Positions, ability.if.NotAttacked]
}
ability "random" "Direction" {
target = roll
from = 0
to = 4
}
ability "switch" "Direction" {
value = roll
cases = [
{ value = 0, run = [ability.set.WanderLeft] },
{ value = 1, run = [ability.set.WanderRight] },
{ value = 2, run = [ability.set.WanderUp] },
]
default = [ability.set.WanderDown]
}
ability "set" "WanderLeft" {
target = destination
value = {
x = pos.x - 1
y = pos.y
}
}
ability "set" "WanderRight" {
target = destination
value = {
x = pos.x + 1
y = pos.y
}
}
ability "set" "WanderUp" {
target = destination
value = {
x = pos.x
y = pos.y - 1
}
}
ability "set" "WanderDown" {
target = destination
value = {
x = pos.x
y = pos.y + 1
}
}
ability "set" "NotAttacked" {
target = attacked
value = false
}
ability "query" "Positions" {
with = [Point, Health]
bind = {
target_pos = Point
}
entity = victim
body = [ability.if.AtDestination]
}
ability "if" "AtDestination" {
condition = target_pos.x == destination.x && target_pos.y == destination.y
then = [ability.query.VictimIsPlayer]
}
ability "query" "VictimIsPlayer" {
with = [Player]
only = victim
body = [ability.spawn.MonsterAttack, ability.set.Attacked]
}
ability "spawn" "MonsterAttack" {
components = {
WantsToAttack = {
attacker = mover
victim = victim
}
}
}
ability "set" "Attacked" {
target = attacked
value = true
}
ability "if" "NotAttacked" {
condition = !attacked
then = [ability.spawn.Wander]
}
ability "spawn" "Wander" {
components = {
WantsToMove = {
entity = mover
destination = destination
}
}
}
ability "function" "end_turn" {
body = [ability.switch.EndTurn]
}
ability "switch" "EndTurn" {
value = data.value.turn_state.current
cases = [
{ value = TurnState.AwaitingInput, run = [ability.return.Waiting] },
{ value = TurnState.PlayerTurn, run = [ability.set.MonsterTurn] },
{ value = TurnState.MonsterTurn, run = [ability.set.AwaitingInput] },
]
}
ability "return" "Waiting" {
}
ability "set" "MonsterTurn" {
target = ability.value.turn_state
value = TurnState.MonsterTurn
}
ability "set" "AwaitingInput" {
target = ability.value.turn_state
value = TurnState.AwaitingInput
}
ability "function" "hud" {
body = [ability.query.PlayerHealth]
}
ability "query" "PlayerHealth" {
with = [Health, Player]
bind = {
health = Health
}
body = [ability.text.Help, ability.bar.Health, ability.text.Health]
}
ability "text" "Help" {
y = 1
message = "Explore the Dungeon. Cursor keys to move."
}
ability "bar" "Health" {
x = 0
y = 0
width = const.DISPLAY_WIDTH
value = health.current
max = health.max
color = Color.red
}
ability "text" "Health" {
y = 0
message = " Health: ${health.current} / ${health.max} "
background = Color.red
}turn_state.st
ability "enum" "TurnState" {
values = [AwaitingInput, PlayerTurn, MonsterTurn]
}Input, plan and generated Rust#
input.txt · key script, one line per frame (142 lines)
input.txt · key script, one line per frame (142 lines)
left
left
left
left
left
left
left
left
left
left
down
down
down
down
right
down
down
down
down
left
up
left
left
left
left
up
up
up
left
left
down
down
down
down
down
down
down
down
down
down
down
left
right
down
right
left
downstructop plan
structop plan
# program.app will be created
+ program.app (.structop/build/app)
+ ability.struct.Camera
+ ability.function.on_player_move
+ ability.set.Camera
+ ability.component.Point
+ ability.component.Render
+ ability.component.Player
+ ability.component.Enemy
+ ability.component.MovingRandomly
+ ability.type.Entity
+ ability.component.WantsToMove
+ ability.component.Health
+ ability.component.Name
+ ability.component.WantsToAttack
+ ability.call.NewMap
+ ability.call.SpawnPlayer
+ ability.call.SpawnMonsters
+ ability.call.NewCamera
+ ability.game.Dungeon
+ ability.clear.Screen
+ ability.switch.Turn
+ ability.call.InputSystems
+ ability.call.PlayerSystems
+ ability.call.MonsterSystems
+ ability.enum.TileType
+ ability.function.map_idx
+ ability.function.new_map
+ ability.for.Fill
+ ability.set.Floor
+ ability.function.map_render
+ ability.for.Rows
+ ability.for.Columns
+ ability.if.Visible
+ ability.switch.Tile
+ ability.draw.FloorTile
+ ability.draw.WallTile
+ ability.function.in_bounds
+ ability.function.can_enter_tile
+ ability.function.try_idx
+ ability.set.NoIdx
+ ability.if.Inside
+ ability.set.SomeIdx
+ ability.struct.Rect
+ ability.function.intersect
+ ability.function.fill
+ ability.for.FillAll
+ ability.set.FillTile
+ ability.function.build_random_rooms
+ ability.loop.Rooms
+ ability.random.RoomX
+ ability.random.RoomY
+ ability.random.RoomW
+ ability.random.RoomH
+ ability.set.Room
+ ability.set.NoOverlap
+ ability.for.Overlaps
+ ability.if.Intersects
+ ability.set.Overlap
+ ability.if.Free
+ ability.for.RoomRows
+ ability.for.RoomColumns
+ ability.if.OnMap
+ ability.set.Carve
+ ability.add.Room
+ ability.function.apply_vertical_tunnel
+ ability.for.Vertical
+ ability.set.VerticalIdx
+ ability.if.VerticalSome
+ ability.set.VerticalFloor
+ ability.function.apply_horizontal_tunnel
+ ability.for.Horizontal
+ ability.set.HorizontalIdx
+ ability.if.HorizontalSome
+ ability.set.HorizontalFloor
+ ability.function.build_corridors
+ ability.for.Copy
+ ability.add.Copy
+ ability.for.Sort
+ ability.set.Key
+ ability.set.Below
+ ability.loop.Shift
+ ability.set.ShiftUp
+ ability.set.Lower
+ ability.set.Insert
+ ability.for.Connect
+ ability.set.Prev
+ ability.set.New
+ ability.random.Coin
+ ability.if.Coin
+ ability.call.HorizontalFirst
+ ability.call.VerticalSecond
+ ability.call.VerticalFirst
+ ability.call.HorizontalSecond
+ ability.function.build_map
+ ability.call.FillWalls
+ ability.call.Rooms
+ ability.call.Corridors
+ ability.set.Start
+ ability.function.input_systems
+ ability.call.InputPlayerInput
+ ability.call.InputMapRender
+ ability.call.InputEntityRender
+ ability.function.player_systems
+ ability.call.PlayerCombat
+ ability.call.PlayerMovement
+ ability.call.PlayerMapRender
+ ability.call.PlayerEntityRender
+ ability.call.PlayerHud
+ ability.function.monster_systems
+ ability.call.MonsterRandomMove
+ ability.call.MonsterCombat
+ ability.call.MonsterMovement
+ ability.call.MonsterMapRender
+ ability.call.MonsterEntityRender
+ ability.call.InputHud
+ ability.call.PlayerEndTurn
+ ability.call.MonsterHud
+ ability.call.MonsterEndTurn
+ ability.function.spawn_player
+ ability.spawn.Player
+ ability.function.spawn_monster
+ ability.random.Dice
+ ability.if.Goblin
+ ability.set.GoblinHp
+ ability.set.GoblinName
+ ability.set.GoblinGlyph
+ ability.set.OrcHp
+ ability.set.OrcName
+ ability.set.OrcGlyph
+ ability.spawn.Monster
+ ability.function.spawn_monsters
+ ability.for.Rooms
+ ability.call.Monster
+ ability.function.player_input
+ ability.switch.Move
+ ability.set.Left
+ ability.set.Right
+ ability.set.Up
+ ability.set.Down
+ ability.set.Zero
+ ability.if.Moving
+ ability.query.Players
+ ability.set.Destination
+ ability.if.Delta
+ ability.set.NoHit
+ ability.query.Targets
+ ability.if.Target
+ ability.set.Hit
+ ability.spawn.PlayerAttack
+ ability.if.NoHit
+ ability.spawn.PlayerMove
+ ability.set.PlayerTurn
+ ability.function.combat
+ ability.query.Attacks
+ ability.query.Victim
+ ability.print.Before
+ ability.set.Damage
+ ability.if.Dead
+ ability.despawn.Victim
+ ability.print.After
+ ability.despawn.Attack
+ ability.function.movement
+ ability.query.Moves
+ ability.if.CanMove
+ ability.query.Mover
+ ability.store.Moved
+ ability.query.MovedPlayer
+ ability.call.MoveCamera
+ ability.despawn.Message
+ ability.function.entity_render
+ ability.query.Rendered
+ ability.draw.Entity
+ ability.function.random_move
+ ability.query.Movers
+ ability.random.Direction
+ ability.switch.Direction
+ ability.set.WanderLeft
+ ability.set.WanderRight
+ ability.set.WanderUp
+ ability.set.WanderDown
+ ability.set.NotAttacked
+ ability.query.Positions
+ ability.if.AtDestination
+ ability.query.VictimIsPlayer
+ ability.spawn.MonsterAttack
+ ability.set.Attacked
+ ability.if.NotAttacked
+ ability.spawn.Wander
+ ability.function.end_turn
+ ability.switch.EndTurn
+ ability.return.Waiting
+ ability.set.MonsterTurn
+ ability.set.AwaitingInput
+ ability.function.hud
+ ability.query.PlayerHealth
+ ability.text.Help
+ ability.bar.Health
+ ability.text.Health
+ ability.enum.TurnState
+ ability.value.camera
+ ability.value.tiles
+ ability.value.player_start
+ ability.value.rooms
+ ability.value.sorted
+ ability.value.key
+ ability.value.turn_state
Plan: 1 to add, 0 to change, 0 to destroy.Generated main.rs
To inspect this generated file, select Rust in intent.st, initialize the project and export its source.
Export locally
structop init
structop apply -source -no-runTry it
cd rust-combat # a folder holding the .st files above
structop init
structop plan
structop apply