As part of my concept testing for an Australian outback themed project I needed a script to apply a "shade" value of trees, specifically an accumulative effect (given outback trees are found in clumps as averse to "forests".)
I ended up with a script that allows a tree to be added in the IDE, "tree shade" script applied, "health" value set and simply copy pasted at will and at random with no need to keep track of the count.
For coders, they may be interested in how the array is auto counted and populated.
See comments for use - specifically performance issues if you get stupid about it. I tested it with over 500 (low poly) trees and didn't even notice it was running so I know it works when used within it's intention boundaries. See comments for more information.
-- CODE BY !nullptr (aka SisterMatic) - please credit - not fussed - just don't call it your code :)
-- SPECIAL NOTE: This script was NOT designed for a massive forest type map where shade is provided by every tree.
-- It was designed for my Australian outback project where trees are often in clumps along water courses.
-- The onus is on you to manage and contain "active shade" trees sensibly.
-- BENEFITS
-- A script that auto detects "shade" trees added to a map as long as the script is assigned. (See usage for best way)
-- Shade benefits per tree increase as you get closer.
-- Tree group shade is accounted for. Each tree in a group casts it's own shade and is accumulative.
-- ie: Tree 1 might give 100% of it's shade and Tree 2 might also be giving 50% of it's shade etc..
-- USAGE
-- Add a tree to your map. Assign tree_shade.lua, static = no and IMPORTANT, set health to the shade value of the tree.
-- eg: a BIG tree might offer 10 shade, a small tree might offer 3.
-- Copy and paste, even paint your trees in. Script will do the count etc. for you.
-- Access "shade" as a global variable. (or change script to return shade)
-- KNOWN ISSUES
-- Nil that I could find except performance on high tree count.
-- PERFORMANCE NOTES:
-- This was tested with over 500 "shade" trees and 1000+ non-shade (script not-assigned on map edges etc.)
-- Performance hit was neglible AS LONG AS a timer is set to at least 1 second intervals.
-- Does NOT take into account which "side" of a tree the sun is on. That would cripple you. Being "under" a tree is more than adequate.
-- Array population occurs only once. Step through occurs every call but is no issue *with* interval.
-- Don't use animated trees - although I haven't tried, no point really and is just pushing your luck.
-- EDITS
-- Safe to make changes to the area of shade but it's pretty accurate imho. It was designed around "Aussie" trees
-- Increase timer at poll_timer to reduce overhead but shade update will be slower.
-- In this example I have added my "hour" timing to stop calculations if it's dark. For obvious reasons :)
-- You can adjust the timer value to increase interval and have either constant or periodical shade updates.
shade = 0 -- access this var for total shade value
local tree_array = {}
local start_time_tree=math.ceil(g_Time/1000)
local tupdate = 0
function tree_shade_init(e)
have_entity = 0
iterations=0 -- debug timer check -- can be removed
end
function tree_shade_main(e)
-- no need to keep count of trees you add with the tree.lua script attached. This is full auto.
if have_entity == 0 then
tcount=table.count(tree_array)
-- do we have this tree already?
have_entity=table.search(tcount,tree_array,e) -- pass tcount so we don't do it twice
if have_entity==0 then
tree_array[tcount+1] = e
end -- if
end -- table populate
-- let's slow down the processing by restricting this to every second (could be increased even more if tree shade benefit is periodical)
-- tested with 500+ trees - almost zero performance loss
local triggerT = poll_timer(1) -- 1 second - could be increased
if triggerT > 0 then -- use this line if you are not doing day/night etc. below is an example if you are
--if triggerT > 0 and tt_Hour > 5 and tt_Hour < 20 then -- if it's completely dark, not much point doing "shade" :)
shade=0
for i=1, tcount, 1 do
treeDist = math.ceil(GetPlayerDistance(tree_array[i]))
if treeDist < 150 then shade = shade + g_Entity[tree_array[i]]['health']*1.5
elseif treeDist < 200 then shade = shade + g_Entity[tree_array[i]]['health']/2
elseif treeDist < 300 then shade = shade + g_Entity[tree_array[i]]['health']/3
end
-- debug / visual rep -- these 2 lines can be removed, commented out.
iterations=iterations+1 -- can be removed
PromptDuration(tcount.." trees on map :: Current shade value: "..shade,1000) -- can be removed
end -- for
end -- timer
end -- function
-- ***************************************
function table.count(tbl)
local c=0
for k in pairs(tbl) do c=c+1 end
return c
end
-- ***************************************
function table.search(tbllen,tbl,tblval)
local z = 1
tblmatch = 0
for z=1, tbllen, 1 do
if tbl[z]==tblval then
tblmatch = 1
end
end
return tblmatch
end
-- ***************************************
function poll_timer(finish)
local stop_timex=math.ceil(g_Time/1000)
local t_elapsedx=start_time_tree-stop_timex
if t_elapsedx == 0 then
start_time_tree=stop_timex+1
tupdate=tupdate+1
else
start_time_tree=stop_timex+1
end
if tupdate == finish then
tupdate=0 -- reset for next use
return 1
else
return 0
end
end -- poll timer
Development/ Gaming Rigs
Sys 1: i7-4770 (3.5)/16Gb/128 SSD/3Tb/970gtx/2 x 23, 1 x 27 LCD - Sys 2: i7/8Gb/670gtx/1.5Tb/1 x 23 LCD - Sys 3: Amd Quad/8Gb/645gtx/1Tb/30" LCD