230 lines
3.9 KiB
Lua
230 lines
3.9 KiB
Lua
require "iuplua"
|
|
|
|
-- Hagen game engine
|
|
-- (c) Severák 2019
|
|
|
|
-- nedoporučuju nikomu číst, protože zárodek vzniknul během jednoho odpoledne
|
|
|
|
-- globals
|
|
|
|
paramsNo = 0
|
|
params = {}
|
|
currVerb = nil
|
|
|
|
verb_ord = {}
|
|
room_ord = {}
|
|
item_ord = {}
|
|
|
|
menu2codes = {}
|
|
|
|
-- constructors
|
|
|
|
function verb(def)
|
|
def.is_verb = true
|
|
def.params = def.params or {}
|
|
verb_ord[#verb_ord+1] = def
|
|
return def
|
|
end
|
|
|
|
function room(def)
|
|
def.is_room = true
|
|
return def
|
|
end
|
|
|
|
function item(def)
|
|
def.is_item = true
|
|
item_ord[#item_ord+1] = def
|
|
return def
|
|
end
|
|
|
|
-- global functions
|
|
|
|
function cls()
|
|
text.value = ""
|
|
end
|
|
|
|
function p(par)
|
|
if text.value=="" then
|
|
text.value = par
|
|
else
|
|
-- TODO - nedavat CRLF poprve
|
|
text.value = text.value .. "\n" .. par
|
|
end
|
|
text.scrollto = text.linecount .. ","
|
|
end
|
|
|
|
function here()
|
|
return game.me.w
|
|
end
|
|
|
|
function move(what, where)
|
|
game[what].w = where
|
|
end
|
|
|
|
function walk(where)
|
|
assert(game[where], "room " .. where .. " does not exist")
|
|
move("me", where)
|
|
cls()
|
|
p(game[where].n)
|
|
p(game[where].d)
|
|
for i,item in ipairs(item_ord) do
|
|
if item.d and item.w==here() then
|
|
p(item.d)
|
|
end
|
|
end
|
|
show_verbs()
|
|
end
|
|
|
|
function _has_subj_of(verb)
|
|
local parCode = verb.params[#params+1]
|
|
for i,item in ipairs(item_ord) do
|
|
if item[parCode] and (verb.global_reach or item.w==here() or item.w=="me") then
|
|
return true
|
|
end
|
|
end
|
|
return false
|
|
end
|
|
|
|
function show_verbs()
|
|
paramsNo = 0
|
|
currVerb = ""
|
|
params = {}
|
|
local here = game[game.me.w]
|
|
commands[1]=nil
|
|
menu2codes={}
|
|
for i,verb in ipairs(verb_ord) do
|
|
if verb.always or here[verb._ref] or _has_subj_of(verb) then
|
|
local idx=commands.count+1
|
|
commands[idx] = verb.n
|
|
menu2codes[idx] = verb._ref
|
|
end
|
|
end
|
|
end
|
|
|
|
function show_items()
|
|
local verb = game[currVerb]
|
|
local parCode = verb.params[#params+1]
|
|
-- print("parCode="..parCode)
|
|
commands[1]=nil
|
|
menu2codes={}
|
|
for i,item in ipairs(item_ord) do
|
|
if item[parCode] and (verb.global_reach or item.w==here() or item.w=="me") then
|
|
local idx=commands.count+1
|
|
commands[idx] = item.n
|
|
menu2codes[idx] = item._ref
|
|
end
|
|
end
|
|
end
|
|
|
|
function process_param(param)
|
|
if paramsNo == 0 then
|
|
currVerb = param
|
|
paramsNo = #game[currVerb].params
|
|
else
|
|
params[#params+1]=param
|
|
end
|
|
|
|
--[[
|
|
print("param="..param)
|
|
print("params="..table.concat(params," "))
|
|
print("currVerb="..currVerb)
|
|
print("paramsNo="..paramsNo)
|
|
print("#params="..#params)
|
|
print "---"
|
|
]]
|
|
|
|
if #params==paramsNo then
|
|
game[currVerb].act(table.unpack(params))
|
|
if game.round then
|
|
game.round()
|
|
end
|
|
show_verbs()
|
|
else
|
|
show_items()
|
|
end
|
|
|
|
end
|
|
|
|
-- game defaults
|
|
|
|
game = {
|
|
title = "Emty game",
|
|
me = item{
|
|
w="intro"
|
|
},
|
|
intro = room{
|
|
n="void",
|
|
d="YOU ARE IN A MAZE OF TWISTY LITTLE PASSAGES, ALL ALIKE."
|
|
},
|
|
vars = {}
|
|
}
|
|
|
|
-- GUI
|
|
|
|
iup.SetGlobal("UTF8MODE","YES")
|
|
|
|
text = iup.multiline{expand="YES", readonly="YES", wordwrap="YES"}
|
|
commands = iup.list{expand="YES"}
|
|
|
|
function commands:action(text, i, selected)
|
|
if selected==1 then
|
|
-- print(text, i)
|
|
process_param(menu2codes[i])
|
|
-- print(menu2codes[i])
|
|
end
|
|
end
|
|
|
|
-- TODO - ovladání pomoci klávesnice
|
|
function commands:k_any(k)
|
|
if k==iup.K_UP or k==iup.K_DOWN then
|
|
-- print "<>"
|
|
end
|
|
if k==iup.K_CR then
|
|
-- print "Enter!"
|
|
end
|
|
end
|
|
|
|
vbox= iup.hbox{text, commands; expand=1}
|
|
|
|
window = iup.dialog{vbox, title = "Hagen", gap = "10", rastersize="640x480"}
|
|
|
|
window:show()
|
|
|
|
-- iup.SetFocus(commands)
|
|
|
|
local gamefile
|
|
if arg[1] then
|
|
gamefile = arg[1]
|
|
else
|
|
local hry = {"syrecky.lua"}
|
|
local hra = iup.Alarm("Hagen engine", "vyberte hru:", "Syrečky")
|
|
if hry[hra] then
|
|
gamefile = hry[hra]
|
|
else
|
|
os.exit(1)
|
|
end
|
|
end
|
|
|
|
-- loads game
|
|
load_game, err = loadfile(gamefile, "t", setmetatable(game, {__index=_G}))
|
|
|
|
if err then
|
|
print(err)
|
|
os.exit(1)
|
|
end
|
|
|
|
load_game()
|
|
|
|
-- add refs
|
|
for ref,obj in pairs(game) do
|
|
if type(obj)=='table' and (obj.is_verb or obj.is_room or obj.is_item) then
|
|
obj._ref = ref
|
|
end
|
|
end
|
|
|
|
window.title = game.title
|
|
walk("intro")
|
|
|
|
if (iup.MainLoopLevel()==0) then
|
|
iup.MainLoop()
|
|
end |