medical/init.lua
2025-01-12 15:17:56 +05:00

298 lines
9.3 KiB
Lua

--[[
Copyright 2025 Andrey Stepanov
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
]]--
local S = core.get_translator("physiology")
local ui = unified_inventory
local _contexts = {}
local function get_context(name)
local context = _contexts[name] or {}
_contexts[name] = context
return context
end
core.register_on_leaveplayer(
function(player)
_contexts[player:get_player_name()] = nil
end
)
local pfp = {
padding = 0.1,
group_btn_w = 0.7,
group_btn_step = 0.9,
section_bg = "#22222266",
body_scale = 0.8,
}
local function draw_group_btn(button, active, x)
local suffix = nil
if active == true then
suffix = "active"
else
suffix = "inactive"
end
local formspec = {
"image_button[",
x, ",0;",
pfp.group_btn_w, ",", pfp.group_btn_w, ";",
button.name, "_", suffix, ".png;",
button.name, ";",
";",
"false;",
"false;",
"]",
"tooltip[",
button.name, ";",
button.label,
"]"
}
return table.concat(formspec)
end
local function group_default_formspec(perplayer_formspec)
local fp = perplayer_formspec
local form_width = (fp.formw - fp.form_header_x * 2) / 3
local formspec = {
"box[",
"0,0;",
form_width - pfp.padding, ",3.2;",
pfp.section_bg,
"]"
}
return table.concat(formspec)
end
local group_buttons = {
{
name = "triage_card",
label = S("Triage Card"),
get_formspec = group_default_formspec,
},
{
name = "examine_patient",
label = S("Examine patient"),
get_formspec = group_default_formspec,
},
{
name = "bandage_fracture",
label = S("Bandage & Fracture"),
get_formspec = group_default_formspec,
},
{
name = "medication",
label = S("Medication"),
get_formspec = group_default_formspec,
},
{
name = "advanced_treatment",
label = S("Advanced treatment"),
get_formspec = group_default_formspec,
},
}
local function draw_groups(active_group, perplayer_formspec)
local formspec = {}
for i, button in ipairs(group_buttons) do
local is_active = false
if active_group == button.name then
is_active = true
end
table.insert(formspec, draw_group_btn(button, is_active, pfp.padding+(i-1)*pfp.group_btn_step))
end
local container = {
"container[",
"0,", pfp.group_btn_step,
"]"
}
table.insert(
formspec,
table.concat(container)
)
table.insert(
formspec,
group_default_formspec(perplayer_formspec)
)
table.insert(
formspec,
"container_end[]"
)
return table.concat(formspec)
end
ui.register_page(
"health",
{
get_formspec = function(player, perplayer_formspec)
local active_group = get_context(player:get_player_name()).active_group or group_buttons[1].name
local fp = perplayer_formspec
local form_width = fp.formw - fp.form_header_x * 2
local section_width = form_width / 3
local body_center = section_width * 1.5
local leg_w = pfp.body_scale * 0.8
local leg_offset = pfp.body_scale * 0.02
local formspec = {
perplayer_formspec.standard_inv_bg,
"container[",
fp.form_header_x, ",", fp.form_header_y, ";",
"]",
"box[",
"0,0;",
form_width, ",", 0.4, ";",
"orange",
"]",
"label[",
pfp.padding, ",0.2;",
player:get_player_name(),
"]",
"container[0,0.65]",
"label[",
pfp.padding, ",0;",
S("EXAMINE & TREATMENT"),
"]",
"label[",
section_width + pfp.padding, ",0;",
S("STATUS"),
"]",
"label[",
section_width * 2 + pfp.padding, ",0;",
S("OVERVIEW"),
"]",
"box[",
"0,0.14;",
form_width, ",0.02;",
"darkgray",
"]",
"container[0,0.35]",
draw_groups(
active_group,
perplayer_formspec
),
"image_button[",
body_center - pfp.body_scale/2, ",0;",
pfp.body_scale, ",", pfp.body_scale, ";",
"head.png;head;;",
"false;false;",
"]",
"tooltip[",
"head;",
S("Head"),
"]",
"image_button[",
body_center - pfp.body_scale/2, ",", pfp.body_scale, ";",
pfp.body_scale, ",", pfp.body_scale*2, ";",
"body.png;body;;",
"false;false;",
"]",
"tooltip[",
"body;",
S("Body"),
"]",
"image_button[",
body_center - pfp.body_scale*1.5, ",", pfp.body_scale, ";",
pfp.body_scale, ",", pfp.body_scale*1.8, ";",
"right_hand.png;right_hand;;",
"false;false;",
"]",
"tooltip[",
"right_hand;",
S("Right Hand"),
"]",
"image_button[",
body_center + pfp.body_scale/2, ",", pfp.body_scale, ";",
pfp.body_scale, ",", pfp.body_scale*1.8, ";",
"left_hand.png;left_hand;;",
"false;false;",
"]",
"tooltip[",
"left_hand;",
S("Left Hand"),
"]",
"image_button[",
body_center - leg_w - leg_offset, ",", pfp.body_scale*3, ";",
leg_w, ",", pfp.body_scale*1.8, ";",
"right_leg.png;right_leg;;",
"false;false;",
"]",
"tooltip[",
"right_leg;",
S("Right Leg"),
"]",
"image_button[",
body_center + leg_offset, ",", pfp.body_scale*3, ";",
leg_w, ",", pfp.body_scale*1.8, ";",
"left_leg.png;left_leg;;",
"false;false;",
"]",
"tooltip[",
"left_leg;",
S("Left Leg"),
"]",
"box[",
section_width * 2, ",0;",
section_width, ",9;",
pfp.section_bg,
"]",
"container_end[]",
"container_end[]",
"container_end[]"
}
return {
formspec = table.concat(formspec),
draw_inventory = true,
draw_item_list = false
}
end
}
)
ui.register_button(
"health",
{
type = "image",
image = "redcross.png",
tooltip = S("Health")
}
)
core.register_on_player_receive_fields(
function(player, formname, fields)
if formname ~= "" then
return
end
for _, btn in ipairs(group_buttons) do
if fields[btn.name] then
get_context(player:get_player_name()).active_group = btn.name
ui.set_inventory_formspec(player, "health")
return
end
end
end
)