Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Backport tool charge helpers #391

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 6 additions & 7 deletions .github/workflows/luacheck.yml
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
on: [push, pull_request]
name: luacheck
on: [push, pull_request]
jobs:
lint:
luacheck:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@master
- name: lint
uses: Roang-zero1/factorio-mod-luacheck@master
with:
luacheckrc_url: ""
- name: Checkout
uses: actions/checkout@master
- name: Luacheck
uses: lunarmodules/luacheck@master
12 changes: 6 additions & 6 deletions .github/workflows/mineunit.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,21 @@ name: mineunit
on: [push, pull_request]

jobs:
build:
mineunit:

runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v2
with:
submodules: true
- uses: actions/checkout@v3

- id: mineunit
uses: mt-mods/mineunit-actions@master
with:
working-directory: ./technic
mineunit-args: --engine-version 5.4.1
badge-label: Test coverage

- uses: RubbaBoy/BYOB@v1.2.0
- uses: RubbaBoy/BYOB@v1.3.0
if: success() && github.event_name == 'push' && github.ref == 'refs/heads/master'
with:
NAME: "${{ steps.mineunit.outputs.badge-name }}"
Expand All @@ -40,6 +39,7 @@ jobs:
--------------------------------------------------------------
${{ steps.mineunit.outputs.mineunit-report }}
```

### Raw test runner output for geeks:
```
${{ steps.mineunit.outputs.mineunit-stdout }}
Expand All @@ -53,7 +53,7 @@ jobs:
comment: |
<details><summary><i>Mineunit failed regression tests, click for details</i></summary>

### Regression test log for technic:
### Regression test log for Technic:
```
${{ steps.mineunit.outputs.mineunit-stdout }}
```
Expand Down
7 changes: 7 additions & 0 deletions technic/doc/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,13 @@ Helper functions
* Some configuration function
* `technic.tube_inject_item(pos, start_pos, velocity, item)`
* Same as `pipeworks.tube_inject_item`
* `technic.get_charge(itemstack)` (alias `technic.get_RE_charge`)
* Returns current charge level of tool.
* `technic.set_charge(itemstack, charge)` (alias `technic.set_RE_charge`)
* Sets tool charge level.
* `technic.use_charge(itemstack, charge)` (alias `technic.use_RE_charge`)
* Attempt to use charge and return `true`/`false` indicating success.
* Always succeeds without checking charge level if creative is enabled.

Registration functions
----------------------
Expand Down
39 changes: 39 additions & 0 deletions technic/helpers.lua
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,45 @@ function technic.refill_RE_charge(stack)
return stack
end

function technic.set_RE_charge(stack, charge)
local max_charge = technic.power_tools[stack:get_name()]
if max_charge then
technic.set_RE_wear(stack, charge, max_charge)
local meta = minetest.deserialize(stack:get_metadata()) or {}
meta.charge = math.min(math.max(0, charge), max_charge)
stack:set_metadata(minetest.serialize(meta))
end
end

technic.set_charge = technic.set_RE_charge

function technic.get_RE_charge(stack)
local max_charge = technic.power_tools[stack:get_name()]
if max_charge then
local meta = minetest.deserialize(stack:get_metadata()) or {}
return meta.charge or 0, max_charge
end
return 0, 0
end

technic.get_charge = technic.get_RE_charge

function technic.use_RE_charge(stack, amount)
if technic.creative_mode or amount <= 0 then
-- Do not check charge in creative mode or when trying to use zero amount
return true
end
local charge = technic.get_RE_charge(stack)
if charge < amount then
-- Not enough energy available
return false
end
technic.set_RE_charge(stack, charge - amount)
-- Charge used successfully
return true
end

technic.use_charge = technic.use_RE_charge

-- If the node is loaded, returns it. If it isn't loaded, load it and return nil.
function technic.get_or_load_node(pos)
Expand Down
57 changes: 30 additions & 27 deletions technic/machines/register/battery_box.lua
Original file line number Diff line number Diff line change
Expand Up @@ -200,59 +200,62 @@ function technic.register_battery_box(data)
if inventory:is_empty(listname) then
return
end
-- Get itemstack and check if it is registered tool
local toolstack = inventory:get_stack(listname, 1)
local toolname = toolstack:get_name()
if technic.power_tools[toolname] == nil then
return
end
-- Load and check tool metadata
local toolmeta = minetest.deserialize(toolstack:get_metadata()) or {}
if not toolmeta.charge then
toolmeta.charge = 0
local tooldef = toolstack:get_definition()
if not tooldef then
return
end
return toolstack, toolmeta, technic.power_tools[toolname]
return toolstack, tooldef
end

local function charge_tools(meta, batt_charge, charge_step)
-- Get tool metadata
if batt_charge <= 0 then
return batt_charge, false
end
-- Get tool stack and definition
local inv = meta:get_inventory()
local toolstack, toolmeta, max_charge = get_tool(inv, "src")
local toolstack, tooldef = get_tool(inv, "src")
if not toolstack then return batt_charge, false end
-- Do the charging
if toolmeta.charge >= max_charge then
-- Do the charging, get and check
local get_charge = tooldef.technic_get_charge or technic.get_charge
local tool_charge, max_charge = get_charge(toolstack)
if tool_charge >= max_charge then
return batt_charge, true
elseif batt_charge <= 0 then
return batt_charge, false
end
-- Do the charging, set charge and replace stack
local set_charge = tooldef.technic_set_charge or technic.set_charge
charge_step = math.min(charge_step, batt_charge)
charge_step = math.min(charge_step, max_charge - toolmeta.charge)
toolmeta.charge = toolmeta.charge + charge_step
technic.set_RE_wear(toolstack, toolmeta.charge, max_charge)
toolmeta.charge = toolmeta.charge
toolstack:set_metadata(minetest.serialize(toolmeta))
charge_step = math.min(charge_step, max_charge - tool_charge)
tool_charge = tool_charge + charge_step
set_charge(toolstack, tool_charge)
inv:set_stack("src", 1, toolstack)
return batt_charge - charge_step, (toolmeta.charge == max_charge)
return batt_charge - charge_step, (tool_charge == max_charge)
end

local function discharge_tools(meta, batt_charge, charge_step, batt_max_charge)
-- Get tool metadata
if batt_charge >= batt_max_charge then
return batt_charge, false
end
-- Get tool stack and definition
local inv = meta:get_inventory()
local toolstack, toolmeta, max_charge = get_tool(inv, "dst")
local toolstack, tooldef = get_tool(inv, "dst")
if not toolstack then return batt_charge, false end
-- Do the discharging
local tool_charge = toolmeta.charge
-- Do the discharging, get and check
local get_charge = tooldef.technic_get_charge or technic.get_charge
local tool_charge = get_charge(toolstack)
if tool_charge <= 0 then
return batt_charge, true
elseif batt_charge >= batt_max_charge then
return batt_charge, false
end
-- Do the discharging, set charge and replace stack
local set_charge = tooldef.technic_set_charge or technic.set_charge
charge_step = math.min(charge_step, batt_max_charge - batt_charge)
charge_step = math.min(charge_step, tool_charge)
tool_charge = tool_charge - charge_step
technic.set_RE_wear(toolstack, tool_charge, max_charge)
toolmeta.charge = tool_charge
toolstack:set_metadata(minetest.serialize(toolmeta))
set_charge(toolstack, tool_charge)
inv:set_stack("dst", 1, toolstack)
return batt_charge + charge_step, (tool_charge == 0)
end
Expand Down
32 changes: 32 additions & 0 deletions technic/spec/fixtures/default.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
mineunit:set_modpath("default", "spec/fixtures")

local function register_node(name, groups, additional_definition)
local definition = {
description = name.." description",
tiles = { "default_"..name },
groups = groups,
}
for k,v in pairs(additional_definition or {}) do definition[k] = v end
minetest.register_node(":default:"..name, definition)
end

local function register_item(name)
minetest.register_craftitem(":default:"..name, {
description = name.." description",
})
end

-- Register some basic nodes for cutting, grinding, digging, registering recipes etc.
register_node("stone", {cracky = 3, stone = 1}, {is_ground_content = true, drop = "default:cobble"})
register_node("cobble", {cracky=3, stone = 2})
register_node("sand", {snappy=2, choppy=2, oddly_breakable_by_hand=2})
register_node("wood", {tree=1, choppy=2, oddly_breakable_by_hand=2})
register_node("dirt", {crumbly = 3, soil = 1})
register_node("sandstone", {crumbly = 1, cracky = 3})
register_node("steelblock", {cracky = 1, level = 2})
register_node("furnace", {cracky=2})
register_node("furnace_active", {cracky=2, not_in_creative_inventory=1}, {drop = "default:furnace"})

register_item("steel_ingot")

screwdriver = {}
36 changes: 36 additions & 0 deletions technic/spec/fixtures/digilines.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
-- Simple digilines mod fixture that logs sent messages, works with some simple digiline mods

mineunit:set_modpath("digilines", "spec/fixtures")

digilines = {
_msg_log = {},
receptor_send = function(pos, rules, channel, msg)
table.insert(digilines._msg_log, {
pos = pos,
rules = rules,
channel = channel,
msg = msg,
})
end,
rules = {
default = {
{x=0, y=0, z=-1},
{x=1, y=0, z=0},
{x=-1, y=0, z=0},
{x=0, y=0, z=1},
{x=1, y=1, z=0},
{x=1, y=-1, z=0},
{x=-1, y=1, z=0},
{x=-1, y=-1, z=0},
{x=0, y=1, z=1},
{x=0, y=-1, z=1},
{x=0, y=1, z=-1},
{x=0, y=-1, z=-1}
}
}
}

digilines = setmetatable(digilines, {
__call = function(self,...) return self end,
__index = function(...) return function(...)end end,
})
27 changes: 27 additions & 0 deletions technic/spec/fixtures/mesecons.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
mineunit:set_modpath("mesecons", "spec/fixtures")
mineunit:set_modpath("mesecons_mvps", "spec/fixtures")

mesecon = {
state = {},
rules = {
default = {
{x = 0, y = 0, z = -1},
{x = 1, y = 0, z = 0},
{x = -1, y = 0, z = 0},
{x = 0, y = 0, z = 1},
{x = 1, y = 1, z = 0},
{x = 1, y = -1, z = 0},
{x = -1, y = 1, z = 0},
{x = -1, y = -1, z = 0},
{x = 0, y = 1, z = 1},
{x = 0, y = -1, z = 1},
{x = 0, y = 1, z = -1},
{x = 0, y = -1, z = -1},
}
}
}

mesecon = setmetatable(mesecon, {
__call = function(self,...) return self end,
__index = function(...) return function(...)end end,
})
Empty file.
27 changes: 27 additions & 0 deletions technic/spec/fixtures/technic.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
enable_cans = true
enable_chainsaw = true
enable_flashlight = true
enable_mining_drill = true
enable_mining_laser = true
enable_multimeter = true
enable_prospector = true
enable_sonic_screwdriver = true
enable_tree_tap = true
enable_vacuum = true

multimeter_remote_start_ttl = 300

enable_wind_mill = true
enable_nuclear_reactor_digiline_selfdestruct = true
quarry_max_depth = 100
quarry_time_limit = 5000

switch_off_delay_seconds = 1800
network_overload_reset_time = 20
admin_priv = basic_privs
enable_corium_griefing = true
enable_radiation_protection = true
enable_radiation_throttling = true
enable_entity_radiation_damage = true
enable_longterm_radiation_damage = true
max_lag_reduction_multiplier = 0.99
22 changes: 22 additions & 0 deletions technic/spec/fixtures/technic.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
-- Use this fixture when loading full Technic mod.
-- Loads all required modules and fixtures for technic

-- Load modules required by tests
mineunit("core")
mineunit("player")
mineunit("protection")
mineunit("common/after")
mineunit("server")
mineunit("voxelmanip")
if mineunit:config("engine_version") ~= "mineunit" then
mineunit("game/voxelarea")
end

-- Load fixtures required by tests
fixture("default")
fixture("mesecons")
fixture("digilines")
fixture("pipeworks")

-- Load technic_worldgen
fixture("technic_worldgen")
7 changes: 7 additions & 0 deletions technic/spec/fixtures/technic_worldgen.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
-- Load technic_worldgen mod

mineunit:set_modpath("technic_worldgen", "../technic_worldgen")

mineunit:set_current_modname("technic_worldgen")
sourcefile("../technic_worldgen/init")
mineunit:restore_current_modname()
11 changes: 11 additions & 0 deletions technic/spec/mineunit.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
deprecated = "ignore"
time_step = 100
exclude = {
-- Integration tests are not part of mod code
"integration_test",
-- These are commented out, not part of mod without manually editing code
"machines/MV/lighting",
"machines/MV/power_radiator",
-- Exclude everything outside of technic directory, some are tested but we do not want stats for these
"^../"
}
Loading
Loading