hermeticum/witch_spike.lua

162 lines
4.8 KiB
Lua
Raw Normal View History

2022-12-16 06:37:37 +00:00
-- This file contains some drafts on how a WITCH API could look in Lua.
-- Assumed in-scope functions:
2022-12-17 00:49:17 +00:00
-- has(data)
-- tells the server what data this object has. If this object is live (ie,
-- in world) when opened for editing this invocation will match the current
-- state of the data in the database. Anything can go in here and all keys
-- are optional.
-- allows(permissions)
-- tells the server what this object's permissions are. the permissions are
-- read, write, carry, and execute. permissions can have value of either
-- "owner" or "world". if unspecified, a permission is set to "owner".
2022-12-16 06:37:37 +00:00
-- hears(pattern, callback)
-- registers a callback called when an object in the same room as this one
-- SAYs something that matches pattern
-- sees(pattern, callback)
-- registers a callback called when an object in the same room as this one
-- EMOTEs something that matches pattern
2022-12-17 00:49:17 +00:00
-- provides(verb_pattern, callback)
-- registers a callback called when an object in the same room as this one runs "/verb_pattern"
2022-12-16 06:37:37 +00:00
-- says(message)
-- issues a SAY to the server that other objects in the same room will overhear
-- does(message)
-- issues an EMOTE to the server that other objects in the same room will see
-- room.says(message)
-- tells the server to have the room "say" something. this is useful when an
-- object wants something said in the third person.
2022-12-17 00:49:17 +00:00
-- random(min, max)
-- returns a random integer between min and max. This is provided so callers
-- do not have to worry about seeding randomness themselves.
-- create(name, code)
-- returns a new object with the given name and Lua code. owner and permissions are transferred from calling object
-- drop(object)
-- drop an object on the ground of the current room
-- tell_sender(msg)
-- issues a WHISPER to whoever triggered a callback
-- teleport_sender(room_id)
-- teleports whoever triggered a callback to the specified room
-- move_sender(direction)
-- teleports whoever triggered a callback in the specified direction
--
-- Callbacks are passed "args," a table which contains the utterance that
-- triggered the handler. this table can be accessed in a few ways:
--
-- args.get("thing")
-- returns whatever string occupied a placeholder called $thing. for example, in this provides:
--
-- provides("give $thing", function(args) end)
--
-- args.get("thing") has whatever space delimited value came after "give".
--
-- args.contains("some substring")
-- returns true if "some substring" is found when all of the args are joined
-- together in one string
--
2022-12-16 06:37:37 +00:00
-- Example 1: the nervous pasta
2022-12-17 00:49:17 +00:00
has({
2022-12-16 06:37:37 +00:00
name = "spaghetti",
description = "a plate of spaghetti covered in a fresh pomodoro sauce"
2022-12-17 00:49:17 +00:00
})
allows({
read = "world",
write = "owner"
carry = "owner",
execute = "world",
})
2022-12-16 06:37:37 +00:00
hears("*eat*", function(msg)
does("quivers nervously")
end)
sees("*slurp*", function(emote)
does("inches away from " + sender.get("name"))
end)
2022-12-17 00:49:17 +00:00
-- Example 1a: the nervous pasta written pedantically
-- This example just demonstrates the syntactic sugar of "hears" and "sees"
has({
name = "spaghetti",
description = "a plate of spaghetti covered in a fresh pomodoro sauce"
})
allows({
read = "world",
write = "owner"
carry = "owner",
execute = "world",
})
provides("say", function(args)
if args.contain("eat") then
does("quivers nervously")
end
end)
provides("emote", function(args)
if args.contain("slurp") then
does("inches away from " + sender.get("name"))
end
end)
2022-12-16 06:37:37 +00:00
-- Example 2: the slot machine
2022-12-17 00:49:17 +00:00
has({
2022-12-16 06:37:37 +00:00
name = "slot machine",
description = "a vintage 1960s slot machine from Las Vegas"
2022-12-17 00:49:17 +00:00
})
allows({
read = "world",
write = "owner"
carry = "owner",
execute = "world",
})
2022-12-16 06:37:37 +00:00
provides("pull $this", function(args)
says("KA CHANK")
2022-12-17 00:49:17 +00:00
one = random(0, 9)
two = random(0, 9)
three = random(0, 9)
2022-12-16 06:37:37 +00:00
say("you got" + string.format("%d %d %d", one, two, three))
end)
provides("whack $this", function(args)
room.says(sender.get("name") + " hits the " + get("name") + " very hard ")
say("CLATTLE KRAK CHAK CHUNK")
say("you got 7 7 7")
end)
-- Example 3: vending machine
2022-12-17 00:49:17 +00:00
has({
2022-12-16 06:37:37 +00:00
name = "vending machine",
2022-12-17 00:49:17 +00:00
description = "looks like the kind of thing you'd see in Tokyo",
money = 0
})
allows({
read = "world",
write = "owner"
carry = "owner",
execute = "world",
})
provides("give $this $money $unit", function(args)
amount = tonumber(args.get("money")) or 0
unit = args.get("unit")
if unit != "yen" then
say("i only take yen sorry")
return
end
2022-12-16 06:37:37 +00:00
2022-12-17 00:49:17 +00:00
set("money", get("money") + amount)
if get("money") > 100 then
room.says("the " + get("name") + " clatters a bit then drops a pocari sweat on the floor")
set("money", 0)
bottle = create("a bottle of pocari sweat", "")
drop(bottle)
else
say("i need more money")
end
2022-12-16 06:37:37 +00:00
end)