Sign in
mod

Sparring

by Potluck · version 1 · Checked for all ages

Where a world's rules let players hurt each other (a server's pvp, or Players can hurt each other on the world's Change page), a hit from another player hurts: a fist a little, a sword by how good it is, a weapon another creation made by its power and a Keen one harder, less what armor keeps off. After a hit there's a moment's grace. Where the rules don't let them, a hit only says so.

1--!strict
2-- Sparring: players hitting players. The game tells creations when one
3-- player hits another (world.on("player_hit"), world 0.28), whatever the
4-- world's rules; this creation says what a hit does. Where the rules let
5-- players hurt each other (world.rule("pvp"): a server's pvp, or the
6-- world's own Change page), it hurts as hard as what's in hand: a fist 1,
7-- a sword by SWORDS below, a weapon another creation made by its power,
8-- more for a Keen one (world.might), less what the other's armor keeps off
9-- (world.hurt takes that off by itself). After a hit a player can't be hit
10-- again for a moment, so nobody is struck down in a flurry. Shots are the
11-- Ranged creation's: they hit other players only where the rules say so.
12
13local world = require("@potluck/world@0.28")
14local tweak = require("@potluck/tweak")
15local ui = require("@potluck/ui@0.2")
16local sound = require("@potluck/sound@0.2")
17
18local knobs = tweak {
19 power = { 1, min = 0.25, max = 3, label = "How hard hits are", order = 1 },
20 grace = { 0.6, min = 0, max = 3, label = "Seconds before the same player can be hit again", order = 2 },
21}
22
23-- How hard each sword hits (a hand hits 1), as it does a creature: a
24-- remix can add its own.
25local SWORDS: { [string]: number } = {
26 ["Wooden sword"] = 2,
27 ["Stone sword"] = 3,
28 ["Copper sword"] = 3,
29 ["Iron sword"] = 4,
30 ["Moonsilver sword"] = 5,
31}
32
33local function hits(held: string?): number
34 if not held then
35 return 1
36 end
37 if SWORDS[held] then
38 return SWORDS[held]
39 end
40 local it = world.item(held)
41 return if it and it.kind == "weapon" then it.power or 1 else 1
42end
43
44-- when each player can next be hurt, and when each was last told the
45-- rules don't let it
46local safe_until: { [number]: number } = {}
47local told: { [number]: number } = {}
48
49world.on("player_hit", function(attacker, victim)
50 local now = world.time()
51 if not world.rule("pvp") then
52 if (told[attacker.id] or -math.huge) + 10 < now then
53 told[attacker.id] = now
54 ui.show(attacker, "sparring", "Players can't hurt each other in this world.", { at = "bottom" })
55 world.after(4, function()
56 ui.hide(attacker, "sparring")
57 end)
58 end
59 return
60 end
61 if (safe_until[victim.id] or 0) > now then
62 return
63 end
64 local power = hits(world.held(attacker)) * world.might(attacker) * knobs.power
65 if world.hurt(victim, power) then
66 safe_until[victim.id] = now + knobs.grace
67 sound.play("thud", { at = victim.position + vector.create(0, 1, 0), volume = 0.8 })
68 end
69end)
70
71world.on("player_left", function(p)
72 safe_until[p.id] = nil
73 told[p.id] = nil
74end)