Jump to content

Check for structure within range and add light?


Recommended Posts

Hi,

I would like to balance the Mod "Multifunctional Carpet - Home Turf" - http://steamcommunity.com/sharedfiles/filedetails/?id=506553597
This mod can give speedbonus, proof of rain, sanity boost and insulate from freezing and overheating to flooring.The main script looks like this (I already made the change, that there is difference between wooden floor, marble floor and carpet):

Spoiler

if not GLOBAL.TheNet:GetIsServer() then
    return
end

-- Gives insulation for Winter, insulation for Summer
-- Winter insulation delays freezing, Summer insulation delays overheating
-- The closer to 0, the quicker the freezing/overheating comes
if GetModConfigData("insulation_switch") == 1 then
    AddComponentPostInit("temperature", function(self)
        local _GetInsulation = self.GetInsulation
        self.GetInsulation = function(self)
            local winterInsulation, summerInsulation = _GetInsulation(self)
            local tile, data = self.inst:GetCurrentTileType()
            if tile == GLOBAL.GROUND.CARPET then
                winterInsulation = winterInsulation + GetModConfigData("insulation_time_carpet")
                summerInsulation = summerInsulation + GetModConfigData("insulation_time_carpet")
            elseif tile == GLOBAL.GROUND.WOODFLOOR then
                winterInsulation = winterInsulation + GetModConfigData("insulation_time_wooden")
                summerInsulation = summerInsulation + GetModConfigData("insulation_time_wooden")
            elseif tile == GLOBAL.GROUND.CHECKER then
                winterInsulation = winterInsulation + GetModConfigData("insulation_time_checker")
                summerInsulation = summerInsulation + GetModConfigData("insulation_time_checker")
            end
            return math.max(0, winterInsulation), math.max(0, summerInsulation)
        end
    end)
end

-- Standing on a carpet during the night only is like wearing a top hat
if GetModConfigData("sanity_switch") == 1 then
    AddPlayerPostInit(function(inst)
        if inst.components.sanity then
            local _crfn = inst.components.sanity.custom_rate_fn
            inst.components.sanity.custom_rate_fn = function(inst)
                local ret = 0
                if _crfn then
                    ret = _crfn(inst)
                end
                --if GLOBAL.TheWorld.state.isnight then
                    local tile, data = inst:GetCurrentTileType()
                    if tile == GLOBAL.GROUND.CARPET or tile == GLOBAL.GROUND.WOODFLOOR or tile == GLOBAL.GROUND.CHECKER then
                        local carpet_delta = GLOBAL.TUNING.DAPPERNESS_MED
                        ret = ret + carpet_delta
                    end
                --end
                return ret
            end
        end
    end)
end

-- Things with moisture stay dry when standing on carpet
if GetModConfigData("rainproof_switch") == 1 then
    AddComponentPostInit("moisture", function(self)
        local _GetMoistureRate = self.GetMoistureRate
        self.GetMoistureRate = function(self)
            local tile, data = self.inst:GetCurrentTileType()
            if tile == GLOBAL.GROUND.CARPET or tile == GLOBAL.GROUND.WOODFLOOR or tile == GLOBAL.GROUND.CHECKER then
                return 0
            end
            return _GetMoistureRate(self)
        end
    end)
end

if GetModConfigData("speed_switch") == 1 then
    AddComponentPostInit("locomotor", function(self)
        if self.inst:HasTag("player") then
            local _UGSM = self.UpdateGroundSpeedMultiplier
            self.UpdateGroundSpeedMultiplier = function(self)
                _UGSM(self)
                local tile, data = self.inst:GetCurrentTileType()
                if tile == GLOBAL.GROUND.CARPET then
                    if self.carpetfast == nil then
                        self:SetExternalSpeedMultiplier(self.inst, "CarpetSpeed", GetModConfigData("speed_mult_carpet"))
                        self.carpetfast = true
                    end
                elseif tile == GLOBAL.GROUND.WOODFLOOR then
                    if self.carpetfast == nil then
                        self:SetExternalSpeedMultiplier(self.inst, "CarpetSpeed", GetModConfigData("speed_mult_wooden"))
                        self.carpetfast = true
                    end
                elseif tile == GLOBAL.GROUND.CHECKER then
                    if self.carpetfast == nil then
                        self:SetExternalSpeedMultiplier(self.inst, "CarpetSpeed", GetModConfigData("speed_mult_checker"))
                        self.carpetfast = true
                    end
                else
                    if self.carpetfast == true then
                        self:RemoveExternalSpeedMultiplier(self.inst, "CarpetSpeed")
                        self.carpetfast = nil
                    end
                end
            end
        end
    end)
end

 

But if every small flooring can give such boost, it is just overpowered.
So I thought to add several conditions, to make sure those boosts only work in a real base, e.g. a science machine within a range of X is needed and other structures.

So could you help me?:
1) How to check if specific structures are in a range of X to the flooring?
2) If all required structures are within range, the boosts should work. And as a sign to the player that the boost is active, I thought about a small light (like the one from fireflies) at the middle of each flooring tile.

My mod knowledge:
I know how to use lua in general, but I'm new to this game and the whole client server things.
So I don't know if I can add such a code to the main script, or if I need another script.
And I don't know the prefabs of all the structures. Are they listed somewhere?
And I don't know how to check the range thing in this game, nor how to add a light :)

Regards,
Serp

 

edit: added code tags.

Edited by Serpens
Link to comment
Share on other sites

If you are not able to answer these questions, is there anything that could help me solving this?

Is there a documentation about all the things I can and should use in the scripts? (The API documentation I found was deleted)
Or is there a script from the game, that does simular things?

Edited by Serpens
Link to comment
Share on other sites

I found the function

GLOBAL.GetClosestInstWithTag("lightsource", inst, radius)

with that I can check if sth is in radius and if so return it.

But unfortunately the string is a "tag".. I would prefer prefab. How to find a specific structure with this, eg the science and alchemiy machine?

Or do you know simular function with prefab?

 

edit:

ah ,a function, defined in simutil.lua uses
"local ents = TheSim:FindEntities(x, y, z, radius, musttags, canttags, mustoneoftags)"
so "ents" are all objects with that tag within the radius. So I can search for all strucutres and lopp through them to find a specific structure! :)

edit2: obviously "structure" does not mean building or simular, cause trees and mapicons are found...

and obviously a building is not an entity at all, so can't be find with that function O.ô -.-

Edited by Serpens
Link to comment
Share on other sites

Solved it finally!

I thought the prefab from the science machine would be "scienceprototyper", but it is "researchlab"... Would be great if you could tell me a source for all the prefabs, beacuse the scripts/prefabs folder is not...

I took a look at scienceprototyper.lua and at the bottom is written:
 

Quote

--Using old prefab names
return createmachine(1, "researchlab", "lvl1", TUNING.PROTOTYPER_TREES.SCIENCEMACHINE, "science"),
    createmachine(2, "researchlab2", "lvl2", TUNING.PROTOTYPER_TREES.ALCHEMYMACHINE, "alchemy"),
    MakePlacer("common/researchlab_placer", "researchlab", "researchlab", "idle" ),
    MakePlacer("common/researchlab2_placer", "researchlab2", "researchlab2", "idle")

so the entities are still working with "old" prefabs? So why at all new prefabs, if it can't be found with the new prefab ? -.-

Link to comment
Share on other sites

All prefabs are defined in files in prefabs folder. Prefab names don't necessarily match file name, for example prefabs/meats.lua defines most meats prefabs.

Prefabs' display names are defined in strings.lua. So if you want to find out the source of, for example, Garland, in strings.lua you'll find FLOWERHAT = "Garland", that is, its prefab name is flowerhat. Searching all files under prefabs folder yields nothing, because although the flowerhat prefab is defined in prefabs/hats.lua, its name is created dynamically (by concatenating "flower" and "hat").

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...