Jump to content

[SOLVED] Need help with changing some code!


Recommended Posts

Hello, I need help getting some code to work more properly :)! So, the code I need help to get changed is all this code...

 

this code down is from modmain.lua

--------------------------------------------------------------------------

local LEAP_MAX_DIST = 10
local LEAP_MIN_DIST = 3
local LEAP_MAX_SPEED = 16
local LEAP_MIN_SPEED = LEAP_MAX_SPEED * LEAP_MIN_DIST / LEAP_MAX_DIST
local LEAP_MAX_DIST_SQ = LEAP_MAX_DIST * LEAP_MAX_DIST
local LEAP_MIN_DIST_SQ = LEAP_MIN_DIST * LEAP_MIN_DIST

local require = GLOBAL.require
local STRINGS = GLOBAL.STRINGS
local ACTIONS = GLOBAL.ACTIONS
local FRAMES = GLOBAL.FRAMES
local DEGREES = GLOBAL.DEGREES
local COLLISION = GLOBAL.COLLISION
local TheSim = GLOBAL.TheSim
local State = GLOBAL.State
local EventHandler = GLOBAL.EventHandler
local TimeEvent = GLOBAL.TimeEvent
local ActionHandler = GLOBAL.ActionHandler
local Vector3 = GLOBAL.Vector3
local PlayFootstep = GLOBAL.PlayFootstep
local SpawnPrefab = GLOBAL.SpawnPrefab

--------------------------------------------------------------------------

local leap_action = AddAction("LEAP", "Vault", function(act)
    if act.doer ~= nil and act.doer.sg ~= nil and act.doer.sg.currentstate.name == "leap_pre" then
    act.doer.sg:GoToState("leap", { pos = act.pos })
    return true
    end
end)

leap_action.priority = 10
leap_action.rmb = true
leap_action.distance = 1024

AddComponentAction("POINT", "leapstaff", function(inst, doer, pos, actions, right)
    if right and
    doer:HasTag("mosslingwhisperer") and not doer.components.sanity:IsSane() and
    doer.components.hunger.current >= 10 and GLOBAL.TheWorld.Map:IsPassableAtPoint(pos:Get()) then
    table.insert(actions, ACTIONS.LEAP)
	elseif doer.components.hunger.current <= 9.99 then
	return
    end
end)

--------------------------------------------------------------------------

AddStategraphState("wilson", State{
    name = "leap_pre",
    tags = { "doing", "busy", "canrotate" },

    onenter = function(inst)
    inst.components.locomotor:Stop()
    inst.AnimState:PlayAnimation("leap_pre")
    end,

    events =
    {
    EventHandler("animover", function(inst)
    if inst.AnimState:AnimDone() then
    if inst.bufferedaction ~= nil then
    inst:PerformBufferedAction()
    else
    inst.sg:GoToState("idle")
    end
    end
    end),
    },
})

local function ToggleOffPhysics(inst)
    inst.sg.statemem.isphysicstoggle = true
    inst.Physics:ClearCollisionMask()
    inst.Physics:CollidesWith(COLLISION.GROUND)
end

local function ToggleOnPhysics(inst)
    inst.sg.statemem.isphysicstoggle = nil
    inst.Physics:ClearCollisionMask()
    inst.Physics:CollidesWith(COLLISION.WORLD)
    inst.Physics:CollidesWith(COLLISION.OBSTACLES)
    inst.Physics:CollidesWith(COLLISION.SMALLOBSTACLES)
    inst.Physics:CollidesWith(COLLISION.CHARACTERS)
    inst.Physics:CollidesWith(COLLISION.GIANTS)
end

AddStategraphState("wilson", State{
    name = "leap",
    tags = { "doing", "busy", "canrotate", "nopredict", "nomorph" },

    onenter = function(inst, data)
    inst.components.locomotor:Stop()
    inst.AnimState:PlayAnimation("leap")

    local dist
    if data ~= nil and data.pos ~= nil then
    inst:ForceFacePoint(data.pos:Get())
    local distsq = inst:GetDistanceSqToPoint(data.pos:Get())
    if distsq <= LEAP_MIN_DIST_SQ then
    dist = LEAP_MIN_DIST
    inst.sg.statemem.speed = LEAP_MIN_SPEED
    elseif distsq >= LEAP_MAX_DIST_SQ then
    dist = LEAP_MAX_DIST
    inst.sg.statemem.speed = LEAP_MAX_SPEED
    else
    dist = math.sqrt(distsq)
    inst.sg.statemem.speed = LEAP_MAX_SPEED * dist / LEAP_MAX_DIST
    end
    else
    inst.sg.statemem.speed = LEAP_MAX_SPEED
    dist = LEAP_MAX_DIST
    end

    if inst.components.hunger ~= nil then
    inst.components.hunger:DoDelta(-dist / LEAP_MAX_DIST, true)
    end

    local x, y, z = inst.Transform:GetWorldPosition()
    local angle = inst.Transform:GetRotation() * DEGREES
    if GLOBAL.TheWorld.Map:IsPassableAtPoint(x + dist * math.cos(angle), 0, z - dist * math.sin(angle)) then
    ToggleOffPhysics(inst)
    end

    inst.Physics:SetMotorVel(inst.sg.statemem.speed * .5, 0, 0)

    local fx = SpawnPrefab("small_puff")
    if fx ~= nil then
    fx.Transform:SetScale(.3, .3, .3)
    fx.Transform:SetPosition(x, 0, z)
    end
    PlayFootstep(inst)

    if inst.components.playercontroller ~= nil then
    inst.components.playercontroller:Enable(false)
    end
    end,

    onupdate = function(inst)
    if inst.sg.statemem.isphysicstoggle then
    local x, y, z = inst.Transform:GetWorldPosition()
    local angle = inst.Transform:GetRotation() * DEGREES
    local radius = .5
    x = x + .75 * radius * math.cos(angle)
    z = z - .75 * radius * math.sin(angle)
    local ents = TheSim:FindEntities(x, 0, z, radius, { "wall" })
    for i, v in ipairs(ents) do
    if v.components.health ~= nil and v.components.health:GetPercent() > .5 then
    ToggleOnPhysics(inst)
    return
    end
    end
    end
    end,

    timeline =
    {
    TimeEvent(.5 * FRAMES, function(inst)
    inst.Physics:SetMotorVel(inst.sg.statemem.speed * .75, 0, 0)
    end),
    TimeEvent(1 * FRAMES, function(inst)
    inst.Physics:SetMotorVel(inst.sg.statemem.speed, 0, 0)
    end),
    TimeEvent(19 * FRAMES, function(inst)
    inst.Physics:SetMotorVel(inst.sg.statemem.speed * .25, 0, 0)
    inst.SoundEmitter:PlaySound("dontstarve/movement/bodyfall_dirt")

    local fx = SpawnPrefab("small_puff")
    if fx ~= nil then
    fx.Transform:SetScale(.3, .3, .3)
    local x, y, z = inst.Transform:GetWorldPosition()
    local angle = inst.Transform:GetRotation() * DEGREES
    fx.Transform:SetPosition(x + .25 * math.cos(angle), 0, z - .25 * math.sin(angle))
    end
    end),
    TimeEvent(20 * FRAMES, function(inst)
    inst.Physics:Stop()
    inst.sg:GoToState("idle", true)
    end),
    },

    onexit = function(inst)
    if inst.sg.statemem.isphysicstoggle then
    ToggleOnPhysics(inst)
    end

    if inst.components.playercontroller ~= nil then
    inst.components.playercontroller:Enable(true)
    end
    end,
})

AddStategraphState("wilson_client", State{
    name = "leap_pre",
    tags = { "doing", "busy", "canrotate" },

    onenter = function(inst)
    inst.components.locomotor:Stop()
    inst.AnimState:PlayAnimation("leap_pre")
    inst.AnimState:PushAnimation("leap_lag", false)

    inst:PerformPreviewBufferedAction()
    inst.sg:SetTimeout(2)
    end,

    onupdate = function(inst)
    if inst:HasTag("doing") then
    if inst.entity:FlattenMovementPrediction() then
    inst.sg:GoToState("idle", "noanim")
    end
    elseif inst.bufferedaction == nil then
    inst.sg:GoToState("idle")
    end
    end,

    ontimeout = function(inst)
    inst:ClearBufferedAction()
    inst.sg:GoToState("idle")
    end,
})

AddStategraphActionHandler("wilson", ActionHandler(ACTIONS.LEAP, function(inst)
    return not inst.sg:HasStateTag("busy")
    and (inst.sg:HasStateTag("moving") or inst.sg:HasStateTag("idle"))
    and "leap_pre"
    or nil
end))

AddStategraphActionHandler("wilson_client", ActionHandler(ACTIONS.LEAP, function(inst)
    return not (inst.sg:HasStateTag("busy") or inst:HasTag("busy"))
    and inst.entity:CanPredictMovement()
    and (inst.sg:HasStateTag("moving") or inst.sg:HasStateTag("idle"))
    and "leap_pre"
    or nil
end))

--------------------------------------------------------------------------

local function ReticuleTargetFn()
    local player = GLOBAL.ThePlayer
    local ground = GLOBAL.TheWorld.Map
    local x, y, z
    for r = LEAP_MAX_DIST * .6, LEAP_MAX_DIST, .25 do
    x, y, z = player.entity:LocalToWorldSpace(r, 0, 0)
    if ground:IsPassableAtPoint(x, y, z) then
    return Vector3(x, y, z)
    end
    end
    for r = LEAP_MAX_DIST * .6 - .25, LEAP_MIN_DIST, -.25 do
    x, y, z = player.entity:LocalToWorldSpace(r, 0, 0)
    if ground:IsPassableAtPoint(x, y, z) then
    return Vector3(x, y, z)
    end
    end
    return Vector3(x, y, z)
end

AddPrefabPostInit("spear", function(inst)
    inst:AddComponent("reticule")
    inst.components.reticule.targetfn = ReticuleTargetFn
    inst.components.reticule.ease = true

    if GLOBAL.TheWorld.ismastersim then
    inst:AddComponent("leapstaff")
    end
end)

this code down is from leapstaff.lua component 

local LeapStaff = Class(function(self, inst)
    self.inst = inst
    --Component exists for gathering actions
end)

return LeapStaff

So... First of all this code comes from someone else's mod called "Frog Webber" & i'm totally going to give them full credit for my character being able to "leap" & i'm sure the author of "Frog Webber" wouldn't mind me using his leap code since I think his actually a Klei dev & i'm sure if Klei allows the base game being modded i'm sure they would allow the mods they make to be used by modders too :)!!!!

So, the thing I want help with is this "leap" action code allows the player to "leap" if they're holding a walking cane but I want my character to be able to use the leap action without the need of a walking cane but I have no idea what to remove from this code to do that, I tried removing every single line of code & nothing I did made removed the "leap" cababilty from the cane, and nothing also allowed my character to use the leap action without a tool  :(! If somebody can please help I would be so glad, SO GLAD!!!! I really want my character to be able to jump around like a monkey but i'm not a code master so I have no idea what to do!

So, any help's appreciated :D!!!!

THANKS SO MUCH for reading my long post, I wish you have a super wonderful day/night, peace :D :D :D :D :D :D :D !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

Edited by SuperDavid
Link to comment
Share on other sites

6 hours ago, Joachim said:

How does it work? I don't know the mod.

From what I understand the 1st code I posted goes into the modmain of your character and it adds a new action called "leap" & the "leap" action seems to rely on the component "leapstaff" which I have no idea what "leapstaff" does... The original author has this in the "leapstaff.lua" "--Component exists for gathering actions" but I still have no idea what that means :?...

This code on the bottom seems to allow a tool you're equipped with to allow you access to the "leap" action with the press of rmb. For example if I copy paste the code on the bottom & change the "cane" to "pickaxe" if you're holding a "pickaxe" then you can use the "leap" action. I tried adding those components to my actual character but it did nothing :(.

AddPrefabPostInit("cane", function(inst)
    inst:AddComponent("reticule")
    inst.components.reticule.targetfn = ReticuleTargetFn
    inst.components.reticule.ease = true

    if GLOBAL.TheWorld.ismastersim then
        inst:AddComponent("leapstaff")
    end
end)

I also know if you remove this code on the bottom, you won't be able to use the leap action at all. If you remove the "POINT" or "leapstaff" from "AddComponentAction" it also makes  you not able to use the leap action at all.

AddComponentAction("POINT", "leapstaff", function(inst, doer, pos, actions, right)
    if right and
    doer:HasTag("mosslingwhisperer") and not doer.components.sanity:IsSane() and
    doer.components.hunger.current >= 10 and GLOBAL.TheWorld.Map:IsPassableAtPoint(pos:Get()) then
    table.insert(actions, ACTIONS.LEAP)
	elseif doer.components.hunger.current <= 9.99 then
	return
    end
end)

Sorry, but I don't know much either :(... If it helps you do you want me to upload my character mod then you can check for yourself & stuff, since I got all the leap code on my character & it works fine & you can see how it is, but that's only if you want to help me I understand if your busy & you can't :D!!! Thanks for reading/replying, have a wonderful day :D:D:D!!!!!!!!!!!!!!!!!!!

Edited by SuperDavid
Link to comment
Share on other sites

AddComponentPostInit("playeractionpicker", function(self)
	if self.inst:HasTag("mosslingwhisperer") then
		local LEAPACTION = GLOBAL.ACTIONS.LEAP
		local _GetRightClickActions = self.GetRightClickActions
		self.GetRightClickActions = function(self, position, target)
			local actions = _GetRightClickActions(self, position, target)
			if #actions == 0 then
				local iscrazy = not self.inst.replica.sanity:IsSane()
				local ishungry = self.inst.replica.hunger:GetCurrent() < 10
				local ispassable = self.map:IsPassableAtPoint(position:Get())
				if ispassable and iscrazy and not ishungry then
					actions = self:SortActionList({LEAPACTION}, position)
				end
			end
			return actions
		end
	end
end)

 

Link to comment
Share on other sites

@DarkXero I'm sorry to bother you again :( but I need your help if you can help me :)...

The code you gave me 

-- All credit for "playeractionpicker" belong to DarkXero!
AddComponentPostInit("playeractionpicker", function(self)
	if self.inst:HasTag("mosslingwhisperer") then
	local LEAPACTION = GLOBAL.ACTIONS.LEAP
	local _GetRightClickActions = self.GetRightClickActions
	self.GetRightClickActions = function(self, position, target)
	local actions = _GetRightClickActions(self, position, target)
	if #actions == 0 then
	local iscrazy = not self.inst.replica.sanity:IsSane()
	local ishungry = self.inst.replica.hunger:GetCurrent() < 10
	local ispassable = self.map:IsPassableAtPoint(position:Get())
	if ispassable and iscrazy and not ishungry then
	actions = self:SortActionList({LEAPACTION}, position)
	end
	end
	return actions
	end
	end
end)

It works fine & allows my character to "leap" without the need of a item in a world with caves turned off, but if you enter a world with caves turned on he can't leap anymore unless he holds a cane :shock: it's like the code you made for me doesn't work in a world with caves turned on :(... I'm so sorry, so sorry but could you maybe tell me what to do or what's wrong :)? I'm really embarrassed to ask of your help so much.... And really thank you so much DarkXero, god bless you :D:D!!!!!!!!! 

PS. If you need me to upload my mod just tell me then I upload it & again I wanna thank you so much because without you my mod probably would never exist :D!!

Edited by SuperDavid
added PS
Link to comment
Share on other sites

2 hours ago, SuperDavid said:

It works fine & allows my character to "leap" without the need of a item in a world with caves turned off, but if you enter a world with caves turned on he can't leap anymore unless he holds a cane :shock: it's like the code you made for me doesn't work in a world with caves turned on

Are you adding the tag "mosslingwhisperer" in the common_postinit of your character?

It needs to be there so the tag exists before the playeractionpicker is constructed.

Also this

AddComponentAction("POINT", "leapstaff", function(inst, doer, pos, actions, right)

shouldn't be needed anymore.

Link to comment
Share on other sites

My "inst:AddTag("mosslingwhisperer")" is one of the first things to be added to my character and it's in the master_postinit not common_postinit :?

Also, i'll remove the 

AddComponentAction("POINT", "leapstaff", function(inst, doer, pos, actions, right)
Edited by SuperDavid
Link to comment
Share on other sites

Tags should go in common_postinit if they are going to be a constant for your character (they exist from the beginning and forever).

This way, these tags aren't networked, they are added server side and client side, and you save resources.

If it's possible that the mosslingwhisperer tag may or may not be enabled, then use another tag that will identify your character.

If the tag will always be there for your character, then move it to common_postinit.

Link to comment
Share on other sites

24 minutes ago, DarkXero said:

 

 

Okay, I just added the mosslingwhisperer tag in my character's common_postinit & removed it from the master_postinit & now when he goes insane he can leap but if I try to become sane it disconnects me from the game without giving a reason :(...

And mosslingwhisperer tag never gets removed from my character he always has it.

Edited by SuperDavid
Link to comment
Share on other sites

I'm so sorry I just realized that it's crashing because of some bad code I had added to him, I removed that code and it's fixed now, so sorry & thanks for helping me xD...

Edited by SuperDavid
Link to comment
Share on other sites

On 8/19/2016 at 6:59 PM, DarkXero said:

Tags should go in common_postinit if they are going to be a constant for your character (they exist from the beginning and forever).

This way, these tags aren't networked, they are added server side and client side, and you save resources.

If it's possible that the mosslingwhisperer tag may or may not be enabled, then use another tag that will identify your character.

If the tag will always be there for your character, then move it to common_postinit.

I'm sorry i'm bringing this kinda old topic back up but I really need to ask, is it better to add stuff like "inst:ListenForEvent("hungerdelta", hungerdelta)" & components in the common_postinit than the master_postinit? Does it make the game less laggy or something :D?!

Link to comment
Share on other sites

56 minutes ago, SuperDavid said:

I'm sorry i'm bringing this kinda old topic back up but I really need to ask, is it better to add stuff like "inst:ListenForEvent("hungerdelta", hungerdelta)" & components in the common_postinit than the master_postinit? Does it make the game less laggy or something :D?!

No. It's a matter of networking.

common_postinit is a function that is both ran by the server and the client.

master_postinit is ran only by the server.

There's nothing that clients have to do with hungerdelta. There's no hunger component on their side. They don't need to deduct the hunger cost from leaping from the current hunger.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
  • Create New...