brewing update

master
runs 2020-08-04 03:54:57 +02:00
parent c9c6a98360
commit 0571cb2810
23 changed files with 289 additions and 147 deletions

View File

@ -2,6 +2,9 @@
Create incredible potions for Minetest.
**This mod requires Player Physics mod as hard dependency.**
## API
### brewing.register_potion_craft
@ -23,7 +26,13 @@ Create incredible potions for Minetest.
- Level: Intensity of the effect (1 to 3)
- Recipe: The 3 ingredients to craft the potion.
- Recipe: The 3 ingredients to craft the potion. The recipe is SHAPED (the order matters).
## Duration Time of the Effects
- For Jump & Speed effects= 60s (lvl1), 30s (lvl2) and 15s (lvl3).
- For Health & Air effects= 15s (lvl1), 3s (lvl2) and 60s (lvl3).
- Note: "One Use Health" and "One Use Air" obviously have no time.
## Recipe Book

View File

@ -1,7 +1,7 @@
local S, modname = ...
-- Function to register the potions
brewing.register_potion = function(sname, name, fname, time, def)
brewing.register_potion = function(sname, name, fname, def)
local tps = {"add", "sub"}
for t=1, #tps do
for i=1, #def.types do
@ -29,21 +29,17 @@ brewing.register_potion = function(sname, name, fname, time, def)
if t == 2 then
flags.inv = true
end
for name, val in pairs(brewing.effects[def.effect](sname, name, fname, time, sdata, flags)) do
item_def[name] = val
for key, val in pairs(brewing.effects[def.effect](sname, name, fname, sdata, flags)) do
item_def[key] = val
end
for name, val in pairs(sdata.set) do
item_def[name] = val
for key, val in pairs(sdata.set) do
item_def[key] = val
end
for name, val in pairs(sdata.effects) do
item_def.potions[name] = val
item_def["time"] = sdata.time
for key, val in pairs(sdata.effects) do
item_def.potions[key] = val
end
minetest.register_node(fname.."_"..tps[t]..sdata.type, item_def)
--potions.register_liquid(i..tps[t]..sname, name.." ("..tps[t].." "..i..")", item_def.on_use)
if minetest.get_modpath("throwing")~=nil then
brewing.register_arrow(fname.."_"..tps[t]..sdata.type, i..tps[t]..sname, name.." ("..tps[t].." "..i..")", item_def.on_use,
item_def.description, item_def.inventory_image)
end
end
end
end
@ -70,20 +66,15 @@ brewing.get_craft_result = function(ingredients)
--firstly in the 2 rows
for i= 1, 3, 1 do
match = false
for j = 1, 3, 1 do
--minetest.chat_send_player("singleplayer", "item="..potion_craft["recipe"][i])
--minetest.chat_send_player("singleplayer", "item2=".. ingredients[j])
if (potion_craft["recipe"][i] == ingredients[j]) or (potion_craft["recipe"][i] == '') then
match = true
break
end
if (potion_craft["recipe"][i] == ingredients[i]) or (potion_craft["recipe"][i] == '') then
match = true
end
if not match then --if an ingredient does not match
break
end
end
if match then --if coincidence with a potion_craft
output = modname ..":" .. potion_craft["effect"] .. "_".. potion_craft["type"] .. potion_craft["level"]
output = modname ..":" .. potion_craft["effect"] .. "_".. potion_craft["type"] .. math.abs(potion_craft["level"])
break
end
end

View File

@ -38,15 +38,15 @@ local function create_recipe_book_form()
"button_exit[3.5,6.6;1,1;btn_exit;"..S("Close").."]"
--Create the cells
local cells = ""
local potion_name = ""
local potion_names = {}
local potion_idxs = ""
local potion_idx = 0
local potion_times = ""
local ing1_idxs = ""
local ing2_idxs = ""
local ing3_idxs = ""
for index, potion_craft in ipairs(brewing.craft_list) do
if potion_craft["effect"] == "jumping" then
local potion_name
if potion_craft["effect"] == "jump" then
potion_name = "potions_jump.png"
else
potion_name = "potions_"..potion_craft["effect"]..".png"
@ -59,6 +59,7 @@ local function create_recipe_book_form()
potion_exists = nil
end
end
local potion_idx
if potion_exists then
potion_idx = potion_exists
else
@ -88,7 +89,15 @@ local function create_recipe_book_form()
else
effect_type = "-"
end
cells = cells .. potion_idx .. ","..S(uppercase(potion_craft["effect"])) .. ",".. S("lvl").. " ".. effect_type .. potion_craft["level"]..','..index..','..index..','..index
local potion_name = "brewing:"..potion_craft["effect"].."_"..potion_craft["type"]..math.abs(potion_craft["level"])
--minetest.chat_send_all(potion_name)
local potion_time= minetest.registered_items[potion_name].time
if potion_time == nil then
potion_time = "-"
else
potion_time = potion_time.."s"
end
cells = cells .. potion_idx .. ","..S(uppercase(potion_craft["effect"])) .. ",".. S("lvl").. " ".. effect_type .. potion_craft["level"]..','..index..','..index..','..index..','..potion_time
if index > 1 then
ing1_idxs = ing1_idxs .. ','
ing2_idxs = ing2_idxs .. ','
@ -105,9 +114,10 @@ local function create_recipe_book_form()
potion_idxs = potion_idxs .. tostring(idx).."="..value
end
--minetest.chat_send_all(potion_idxs)
--local def = minetest.registered_items[potion_name]
recipe_book_formspec =
recipe_book_formspec ..
"tablecolumns[image,"..potion_idxs..";text;text;image,"..ing1_idxs..";image,"..ing2_idxs..";image,"..ing3_idxs.."]"..
"tablecolumns[image,"..potion_idxs..";text;text;image,"..ing1_idxs..";image,"..ing2_idxs..";image,"..ing3_idxs..";text]"..
"table[0.375,0.375;7.2,6;table_potions;"..cells..";0]"
return recipe_book_formspec
end
@ -199,10 +209,11 @@ end
local function try_to_make_potion(pos, player)
local meta = minetest.get_meta(pos)
local inv = meta:get_inventory()
local ing1, ing2, ing3, ignitor, water, flask, dst
local ing1, ing2, ing3, ignitor, water, flask
local ing1_name, ing2_name, ing3_name, ignitor_name, water_name, flask_name
local flask_count
local brewed
local infotext
local update = true
while update do
update = false
@ -219,16 +230,15 @@ local function try_to_make_potion(pos, player)
flask= inv:get_stack("flask", 1)
flask_name = flask:get_name()
flask_count = flask:get_count()
dst= inv:get_stack("dst", 1)
--The list: {ingredient_list_name, ingredient_stack, ingredient_name, how_much_decrements_when_crafted}
local ing_list = {{"ing1", ing1, ing1_name, 1}, {"ing2", ing2, ing2_name, 1}, {"ing3", ing3, ing3_name, 1}, {"ignitor", ignitor, ignitor_name, 1}, {"flask", flask, flask_name, brewing.settings.filled_flasks}}
local is_valid_water= is_valid_water(water_name)
local valid_water= is_valid_water(water_name)
--minetest.chat_send_player("singleplayer", brewing.settings.ignitor_name)
if ignitor_name== brewing.settings.ignitor_name and is_valid_water and flask_name== brewing.settings.flask_name and flask_count >= brewing.settings.filled_flasks then
if ignitor_name== brewing.settings.ignitor_name and valid_water and flask_name== brewing.settings.flask_name and flask_count >= brewing.settings.filled_flasks then
--brewed, afterbrewed = minetest.get_craft_result({method = "normal", width =3, items = {ingplus1, ingplus2, ingplus3, ingminus1, ingminus2, ingminus3, ignitor, water, flask}})
brewed = brewing.get_craft_result({ing1_name, ing2_name, ing3_name})
if brewed ~= nil then
@ -252,7 +262,7 @@ local function try_to_make_potion(pos, player)
end
end
end
local infotext = ""
infotext = ""
end
--
-- Set meta values

View File

@ -4,12 +4,12 @@ local S = ...
brewing.effects = {}
brewing.effects.phys_override = function(sname, name, fname, time, sdata, flags)
brewing.effects.phys_override = function(effect_name, description_name, potion_name, sdata, flags)
local def = {
on_use = function(itemstack, user, pointed_thing)
brewing.make_sound("player", user, "brewing_magic_sound")
--brewing.magic_aura(user, user:get_pos(), "player", "default")
brewing.grant(time, user:get_player_name(), fname.."_"..flags.type..sdata.type, name, flags)
brewing.grant(user, effect_name, potion_name.."_"..flags.type..sdata.type, description_name, sdata.time or 0, flags)
itemstack:take_item()
return itemstack
end,
@ -24,7 +24,7 @@ brewing.effects.phys_override = function(sname, name, fname, time, sdata, flags)
return def
end
brewing.effects.fixhp = function(sname, name, fname, time, sdata, flags)
brewing.effects.fixhp = function(sname, name, fname, sdata, flags)
local def = {
on_use = function(itemstack, user, pointed_thing)
brewing.make_sound("player", user, "brewing_magic_sound")
@ -52,7 +52,7 @@ brewing.effects.fixhp = function(sname, name, fname, time, sdata, flags)
return def
end
brewing.effects.air = function(sname, name, fname, time, sdata, flags)
brewing.effects.air = function(sname, name, fname, sdata, flags)
local def = {
on_use = function(itemstack, user, pointed_thing)
brewing.make_sound("player", user, "brewing_magic_sound")
@ -82,38 +82,6 @@ brewing.effects.air = function(sname, name, fname, time, sdata, flags)
return def
end
brewing.effects.blowup = function(sname, name, fname, time, sdata, flags)
local def = {
on_use = function(itemstack, user, pointed_thing)
brewing.make_sound("player", user, "brewing_magic_sound")
--brewing.magic_aura(user, user:get_pos(), "player", "default")
brewing.grant(time, user:get_player_name(), fname.."_"..flags.type..sdata.type, name, flags)
itemstack:take_item()
return itemstack
end,
potions = {
speed = 0,
jump = 0,
gravity = 0,
tnt = 0,
},
}
def.mobs = {
on_near = function(itemstack, user, pointed_thing)
local str = user:get_luaentity().brewing.exploding
if flags.inv==true then
str = math.max(0, str - sdata.power)
else
str = math.min(str + sdata.power, 250)
end
user:get_luaentity().brewing.exploding = str
itemstack:take_item()
return itemstack
end,
}
return def
end
brewing.effects.set_invisibility = function(player) -- hide player and name tag
local prop = {
visual_size = {x = 0, y = 0},
@ -134,8 +102,9 @@ brewing.effects.set_visibility = function(player) -- show player and tag
player:set_properties(prop)
end
brewing.grant = function(time, playername, potion_name, type, flags)
brewing.grant = function(player, effect_name, potion_name, description_name, time, flags)
local rootdef = minetest.registered_items[potion_name]
--minetest.chat_send_all(potion_name)
if rootdef == nil then
return
end
@ -152,27 +121,14 @@ brewing.grant = function(time, playername, potion_name, type, flags)
def.jump = 0 - def.jump
def.tnt = 0 - def.tnt
end
brewing.addPrefs(playername, def.speed, def.jump, def.gravity, def.tnt)
brewing.refresh(playername)
minetest.chat_send_player(playername, S("You are under the effects of the").." "..type.." "..S("potion."))
local player_name = player:get_player_name()
playerphysics.add_physics_factor(player, effect_name, potion_name, def[effect_name])
minetest.chat_send_player(player_name, S("You are under the effects of the").." "..description_name.." "..S("potion."))
--minetest.chat_send_all("time="..tostring(time))
minetest.after(time, function()
brewing.addPrefs(playername, 0-def.speed, 0-def.jump, 0-def.gravity, 0-def.tnt)
brewing.refresh(playername)
minetest.chat_send_player(playername, S("The effects of the").." "..type.." "..S("potion have worn off."))
if minetest.get_player_by_name(player_name)~=nil then
playerphysics.remove_physics_factor(player, effect_name, potion_name)
minetest.chat_send_player(player_name, S("The effects of the").." "..description_name.." "..S("potion have worn off."))
end
end)
end
brewing.addPrefs = function(playername, speed, jump, gravity, tnt)
local prefs = brewing.players[playername]
prefs.speed = prefs.speed + speed
prefs.jump = prefs.jump + jump
prefs.gravity = prefs.gravity + gravity
prefs.tnt = prefs.tnt + tnt
end
brewing.refresh = function(playername)
if minetest.get_player_by_name(playername)~=nil then
local prefs = brewing.players[playername]
minetest.get_player_by_name(playername):set_physics_override(prefs.speed, prefs.jump, prefs.gravity)
end
end

View File

@ -11,13 +11,13 @@ brewing.craft_list = {} --for potion crafts
brewing.effects = {} --for the player effects
brewing.players = {} --for the players
assert(loadfile(modpath.. "/settings.lua"))(S, modpath)
assert(loadfile(modpath.. "/settings.lua"))(modpath)
assert(loadfile(modpath.. "/api.lua"))(S, modname)
assert(loadfile(modpath.. "/effects.lua"))(S)
assert(loadfile(modpath.. "/potions.lua"))(S)
assert(loadfile(modpath.. "/potion_crafts.lua"))()
assert(loadfile(modpath.. "/cauldron.lua"))(S)
assert(loadfile(modpath.. "/nodes.lua"))(S)
assert(loadfile(modpath.. "/player.lua"))(S)
assert(loadfile(modpath.. "/player.lua"))()
assert(loadfile(modpath.. "/sound.lua"))()
assert(loadfile(modpath.. "/mushroom.lua"))(S)

View File

@ -6,7 +6,7 @@ Cortinarius Violaceus=Cortinarius Violaceus
Gliophorus Viridis=Gliophorus Viridis
Green Hygrocybe=Green Hygrocybe
Health=Salud
Jumping=Salto
Jump=Salto
Leaiana Mycena=Leaiana Mycena
lvl=nivel
Magic Cauldron= Caldero mágico

View File

@ -1,4 +1,4 @@
name = brewing
description = Magic Brewing System
depends = default, flowers
depends = playerphysics, default, flowers
optional_depends =

View File

@ -1,5 +1,7 @@
local S = ...
local mg_name = minetest.get_mapgen_setting("mg_name")
-- Orange Mycena
if mg_name ~= "v6" and mg_name ~= "singlenode" then

View File

@ -37,8 +37,8 @@ minetest.register_ore({
clust_scarcity = 10*10*10,
clust_num_ores = 5,
clust_size = 5,
y_min = -512,
y_max = -256,
y_min = -512,
})
-- Magic Gem

View File

@ -1,25 +1,3 @@
local S = ...
minetest.register_on_joinplayer(function(player)
brewing.players[player:get_player_name()] = {
antigravity = 1,
jump = 1,
gravity = 1,
tnt = 0,
air = 0,
}
end)
minetest.register_on_joinplayer(function(player)
brewing.players[player:get_player_name()] = {
speed = 1,
jump = 1,
gravity = 1,
tnt = 0,
air = 0,
}
end)
minetest.register_chatcommand("effect", {
params = "none",
description = "get effect info",

View File

@ -7,17 +7,17 @@ brewing.register_potion_craft({
recipe = {'brewing:cortinarius_violaceus', 'flowers:mushroom_red', 'brewing:gliophorus_viridis'}
})
--Jumping
--Jump
brewing.register_potion_craft({
effect= "jumping",
effect= "jump",
type= "add",
level= 1,
recipe = {'flowers:mushroom_brown', 'flowers:mushroom_red', 'brewing:gliophorus_viridis'}
})
brewing.register_potion_craft({
effect= "jumping",
effect= "jump",
type= "add",
level= 2,
recipe = {'brewing:orange_mycena', 'brewing:cortinarius_violaceus', 'brewing:gliophorus_viridis'}
@ -45,3 +45,12 @@ brewing.register_potion_craft({
level= -3,
recipe = {'flowers:mushroom_red', 'flowers:mushroom_red', 'flowers:mushroom_red'}
})
--Speed
brewing.register_potion_craft({
effect= "speed",
type= "add",
level= 2,
recipe = {'brewing:pluteus_chrysophaeus', 'brewing:green_hygrocybe', 'brewing:green_hygrocybe'}
})

View File

@ -1,6 +1,6 @@
local S = ...
brewing.register_potion("speed", S("Speed"), "brewing:speed", 300, {
brewing.register_potion("speed", S("Speed"), "brewing:speed", {
effect = "phys_override",
types = {
{
@ -9,6 +9,7 @@ brewing.register_potion("speed", S("Speed"), "brewing:speed", 300, {
effects = {
speed = 1,
},
time = 60,
},
{
type = 2,
@ -16,6 +17,7 @@ brewing.register_potion("speed", S("Speed"), "brewing:speed", 300, {
effects = {
speed = 2,
},
time = 30,
},
{
type = 3,
@ -23,11 +25,12 @@ brewing.register_potion("speed", S("Speed"), "brewing:speed", 300, {
effects = {
speed = 3,
},
time = 15,
},
}
})
brewing.register_potion("antigrav", S("Anti-Gravity"), "brewing:antigravity", 300, {
brewing.register_potion("antigrav", S("Anti-Gravity"), "brewing:antigravity", {
effect = "phys_override",
types = {
{
@ -36,6 +39,7 @@ brewing.register_potion("antigrav", S("Anti-Gravity"), "brewing:antigravity", 30
effects = {
gravity = -0.1,
},
time = 60,
},
{
type = 2,
@ -43,6 +47,7 @@ brewing.register_potion("antigrav", S("Anti-Gravity"), "brewing:antigravity", 30
effects = {
gravity = -0.2,
},
time = 30,
},
{
type = 3,
@ -50,11 +55,12 @@ brewing.register_potion("antigrav", S("Anti-Gravity"), "brewing:antigravity", 30
effects = {
gravity = -0.3,
},
time = 15,
},
}
})
brewing.register_potion("jump", S("Jumping"), "brewing:jumping", 300, {
brewing.register_potion("jump", S("Jump"), "brewing:jump", {
effect = "phys_override",
types = {
{
@ -63,6 +69,7 @@ brewing.register_potion("jump", S("Jumping"), "brewing:jumping", 300, {
effects = {
jump = 0.5,
},
time = 60,
},
{
type = 2,
@ -70,6 +77,7 @@ brewing.register_potion("jump", S("Jumping"), "brewing:jumping", 300, {
effects = {
jump = 1,
},
time = 30,
},
{
type = 3,
@ -77,11 +85,12 @@ brewing.register_potion("jump", S("Jumping"), "brewing:jumping", 300, {
effects = {
jump = 1.5,
},
time = 15,
},
}
})
brewing.register_potion("ouhealth", S("One Use Health"), "brewing:ouhealth", 300, {
brewing.register_potion("ouhealth", S("One Use Health"), "brewing:ouhealth", {
effect = "fixhp",
types = {
{
@ -108,26 +117,26 @@ brewing.register_potion("ouhealth", S("One Use Health"), "brewing:ouhealth", 300
}
})
brewing.register_potion("health", S("Health"), "brewing:health", 300, {
brewing.register_potion("health", S("Health"), "brewing:health", {
effect = "fixhp",
types = {
{
type = 1,
time = 60,
time = 15,
set = {},
effects = {
},
},
{
type = 2,
time = 120,
time = 30,
set = {},
effects = {
},
},
{
type = 3,
time = 180,
time = 60,
set = {},
effects = {
},
@ -135,7 +144,7 @@ brewing.register_potion("health", S("Health"), "brewing:health", 300, {
}
})
brewing.register_potion("ouair", S("One Use Air"), "brewing:ouair", 300, {
brewing.register_potion("ouair", S("One Use Air"), "brewing:ouair", {
effect = "air",
types = {
{
@ -162,26 +171,26 @@ brewing.register_potion("ouair", S("One Use Air"), "brewing:ouair", 300, {
}
})
brewing.register_potion("air", S("Air"), "brewing:air", 300, {
brewing.register_potion("air", S("Air"), "brewing:air", {
effect = "air",
types = {
{
type = 1,
time = 60,
time = 15,
set = {},
effects = {
},
},
{
type = 2,
time = 120,
time = 30,
set = {},
effects = {
},
},
{
type = 3,
time = 180,
time = 60,
set = {},
effects = {
},

View File

@ -1,4 +1,4 @@
local S, modpath = ...
local modpath = ...
local function split(inputstr, sep)
if sep == nil then

View File

@ -1,5 +1,3 @@
farbows = {}
-- internationalization boilerplate
local S = minetest.get_translator(minetest.get_current_modname())

View File

@ -1,7 +1,4 @@
jonez = {}
--Variables
local modname = "jonez"
local S = minetest.get_translator(minetest.get_current_modname())
function firstToUpper(str)

View File

@ -94,7 +94,7 @@ function mobkit.lq_climb(self)
pos.y = pos.y + 0.5
climb_pos = pos
break
elseif not(petz.is_tree_like(node_name)) then
elseif not(petz.is_tree_like(node.name)) then
climb = false
break
end

View File

@ -0,0 +1,123 @@
# Player Physics API.
Version: 1.0.0
This mod makes it possible for multiple mods to modify player physics (speed, jumping strength, gravity) without conflict.
## Introduction
### For players
Mods and games in Minetest can set physical attributes of players, such as speed and jump strength. For example, player speed could be set to 200%. But the way this works makes it difficult for multiple mods to *modify* physical attributes without leading to conflicts, problems and hilarious bugs, like speed that changes often to nonsense values.
The Player Physics API aims to resolve this conflict by providing a “common ground” for mods to work together in this regard.
This mod does nothing on its own, you will only need to install it as dependency of other mods.
When you browse for mods that somehow mess with player physics (namely: speed, jump strength or gravity) and want to use more than one of them, check out if they support the Player Physics API. If they don't, it's very likely these mods will break as soon you activate more than one of them, for example, if two mods try to set the player speed. If you found such a “hilarious bug”, please report it to the developers of the mods (or games) and point them to the Player Physics API.
Of course, not all mods need the Player Physics API. Mods that don't touch player physics at all won't need this mod.
The rest of this document is directed at developers.
### For developers
The function `set_physics_override` from the Minetest Lua API allows mod authors to override physical attributes of players, such as speed or jump strength.
This function works fine as long there is only one mod that sets a particular physical attribute at a time. However, as soon as at least two different mods (that do not know each other) try to change the same player physics attribute using only this function, there will be conflicts as each mod will undo the change of the other mod, as the function sets a raw value. A classic race condition occurs. This is the case because the mods fail to communicate with each other.
This mod solves the problem of conflicts. It bans the concept of “setting the raw value directly” and replaces it with the concept of factors that mods can add and remove for each attribute. The real physical player attribute will be the product of all active factors.
## Quick start
Let's say you have a mod `example` and want to double the speed of the player (i.e. multiply it by a factor of 2), but you also don't want to break other mods that might touch the speed.
Previously, you might have written something like this:
`player:set_physics_override({speed=2})`
However, your mod broke down as soon the mod `example2` came along, which wanted to increase the speed by 50%. In the real game, the player speed randomly switched from 50% and 200% which was a very annoying bug.
In your `example` mod, you can replace the code with this:
`playerphysics.add_physics_factor(player, "speed", "my_double_speed", 2)`
Where `"my_double_speed` is an unique ID for your speed factor.
Now your `example` mod is interoperable! And now, of course, the `example2` mod has to be updated in a similar fashion.
## Precondition
There is only one precondition to using this mod, but it is important:
Mods *MUST NOT* call `set_physics_override` directly for numerical values. Instead, to modify player physics, all mods that touch player physics have to use this API.
## Functions
### `playerphysics.add_physics_factor(player, attribute, id, value)`
Adds a factor for a player physic and updates the player physics immediately.
#### Parameters
* `player`: Player object
* `attribute`: Which of the physical attributes to change. Any of the numeric values of `set_physics_override` (e.g. `"speed"`, `"jump"`, `"gravity"`)
* `id`: Unique identifier for this factor. Identifiers are stored on a per-player per-attribute type basis
* `value`: The factor to add to the list of products
If a factor for the same player, attribute and `id` already existed, it will be overwritten.
### `playerphysics.remove_physics_factor(player, attribute, id)`
Removes the physics factor of the given ID and updates the player's physics.
#### Parameters
Same as in `playerphysics.add_physics_factor`, except there is no `value` argument.
## Examples
### Speed changes
Let's assume this mod is used by 3 different mods all trying to change the speed:
Potions, Exhaustion and Electrocution.
Here's what it could look like:
Potions mod:
```
playerphysics.add_physics_factor(player, "speed", "run_potion", 2)
```
Exhaustion mod:
```
playerphysics.add_physics_factor(player, "jump", "exhausted", 0.75)
```
Electrocution mod:
```
playerphysics.add_physics_factor(player, "jump", "shocked", 0.9)
```
When the 3 mods have done their change, the real player speed is simply the product of all factors, that is:
2 * 0.75 * 0.9 = 1.35
The final player speed is thus 135%.
### Speed changes, part 2
Let's take the example above.
Now if the Electrocution mod is done with shocking the player, it just needs to call:
```
playerphysics.remove_physics_factor(player, "jump", "shocked")
```
The effect is now gone, so the new player speed will be:
2 * 0.75 = 1.5
### Sleeping
To simulate sleeping by preventing all player movement, this can be done with this easy trick:
```
playerphysics.add_physics_factor(player, "speed", "sleeping", 0)
playerphysics.add_physics_factor(player, "jump", "sleeping", 0)
```
This works regardless of the other factors because 0 times anything equals 0.
## License
This mod is free software, released under the MIT License.

View File

@ -0,0 +1 @@
This mod makes it possible for multiple mods to modify player physics (speed, jumping strength, gravity) without conflict.

View File

@ -0,0 +1,43 @@
playerphysics = {}
local function calculate_attribute_product(player, attribute)
local a = minetest.deserialize(player:get_attribute("playerphysics:physics"))
local product = 1
if a == nil or a[attribute] == nil then
return product
end
local factors = a[attribute]
if type(factors) == "table" then
for _, factor in pairs(factors) do
product = product * factor
end
end
return product
end
function playerphysics.add_physics_factor(player, attribute, id, value)
local a = minetest.deserialize(player:get_attribute("playerphysics:physics"))
if a == nil then
a = { [attribute] = { [id] = value } }
elseif a[attribute] == nil then
a[attribute] = { [id] = value }
else
a[attribute][id] = value
end
player:set_attribute("playerphysics:physics", minetest.serialize(a))
local raw_value = calculate_attribute_product(player, attribute)
player:set_physics_override({[attribute] = raw_value})
end
function playerphysics.remove_physics_factor(player, attribute, id)
local a = minetest.deserialize(player:get_attribute("playerphysics:physics"))
if a == nil or a[attribute] == nil then
-- Nothing to remove
return
else
a[attribute][id] = nil
end
player:set_attribute("playerphysics:physics", minetest.serialize(a))
local raw_value = calculate_attribute_product(player, attribute)
player:set_physics_override({[attribute] = raw_value})
end

View File

@ -0,0 +1 @@
name = playerphysics

View File

@ -28,7 +28,7 @@ function rcbows.spawn_arrow(user, strength, itemstack)
return true
end
function rcbows.launch_arrow(user, user_pos, name, def, itemstack)
function rcbows.launch_arrow(user, name, def, itemstack)
if not rcbows.spawn_arrow(user, def.strength, itemstack) then --throw arrow (spawn arrow entity)
return -- something failed
end
@ -74,7 +74,7 @@ function rcbows.register_bow(name, def)
assert(type(def.strength) == "number")
assert(def.uses > 0)
local function reload_bow(itemstack, user)
local function reload_bow(itemstack, user, pointed_thing)
local inv = user:get_inventory()
local arrow, inventory_arrows, inventory_arrow, inv_arrow_name
local inv_list = inv:get_list("main")
@ -160,7 +160,7 @@ function rcbows.register_bow(name, def)
groups = {not_in_creative_inventory=1},
on_use = function(itemstack, user, pointed_thing)
return rcbows.launch_arrow(user, user_pos, name, def, itemstack)
return rcbows.launch_arrow(user, name, def, itemstack)
end,
on_secondary_use = function(itemstack, user, pointed_thing) --viewfinder

19
mods/swaz/.luacheckrc Normal file
View File

@ -0,0 +1,19 @@
unused_args = false
allow_defined_top = true
globals = {
"minetest",
"mobkit"
}
read_globals = {
string = {fields = {"split"}},
table = {fields = {"copy", "getn"}},
-- Builtin
"vector", "ItemStack",
"dump", "DIR_DELIM", "VoxelArea", "Settings",
-- MTG
"default", "sfinv", "creative",
}

View File

@ -1,7 +1,5 @@
local modname = "swaz"
local modpath = minetest.get_modpath(modname)
-- internationalization boilerplate
local S = minetest.get_translator(minetest.get_current_modname())
local mg_name = minetest.get_mapgen_setting("mg_name")
-- Register Biomes
@ -512,7 +510,6 @@ if mg_name ~= "v6" and mg_name ~= "singlenode" then
sidelen = 16,
fill_ratio = 0.008,
biomes = {"swampz"},
sidelen = 16,
noise_params = {
offset = 0.05,
scale = 0.008,
@ -534,7 +531,6 @@ if mg_name ~= "v6" and mg_name ~= "singlenode" then
sidelen = 16,
fill_ratio = 0.008,
biomes = {"swampz"},
sidelen = 16,
noise_params = {
offset = 0.005,
scale = 0.008,