Awesome growing trees!
#1
I've set this up to be 100% forwards compatible, for now, trees drop leaves - so when you plant leaves you grow a tree!

Also, when you plant saplings, they grow into trees too.

I decided that it wasn't sensible to have insta-grow trees, but they still grow pretty quickly and can be anywhere up to 8 blocks tall (wood).

I've used a very very very basic implementation for the leaves, but IMO it still looks good Smile

PHP Code:
local TreePlugin = {}
TreePlugin.__index = TreePlugin

function TreePlugin:new()
   
local t = {}
   
setmetatable(t, TreePlugin)
   
local w = Lua__cPlugin:new()
   
tolua.setpeer(w, t)
   
w:tolua__set_instance(w)
   return 
w
end

function TreePlugin:OnDisable()
   
Log( Plugin:GetName() .. " v." .. Plugin:GetVersion() .. " is shutting down..." )
end

function TreePlugin:Initialize()
   
self:SetName( "Tree" )
   
self:SetVersion( 1 )

   
PluginManager = cRoot:Get():GetPluginManager()
   
PluginManager:AddHook( self, cPluginManager.E_PLUGIN_TICK )
   
PluginManager:AddHook( self, cPluginManager.E_PLUGIN_BLOCK_DIG )
   
PluginManager:AddHook( self, cPluginManager.E_PLUGIN_BLOCK_PLACE )
   
self.TreeBlocks = {};
   
self.TreeBlocks = {};

   
Log( "Initialized " .. self:GetName() .. " v." .. self:GetVersion() )
   
self.NumTicks = 0;
   return 
true
end

function IsFreeForTree(block)
   return 
block == E_BLOCK_AIR
      
or block == E_BLOCK_TORCH
      
or block == E_BLOCK_MINECART_TRACKS
      
or block == E_BLOCK_REDSTONE_TORCH_ON
      
or block == E_BLOCK_REDSTONE_TORCH_OFF;
end

function IsTree(block)
   return 
block == E_BLOCK_LOG;
end

function IsSapling(block)
   return 
block == E_BLOCK_SAPLING
   
or block == E_BLOCK_LEAVES;
end

function TreePlugin:OnBlockPlace( PacketData, Player )

   
local X = PacketData.m_PosX
   local Y 
= PacketData.m_PosY
   local Z 
= PacketData.m_PosZ
   X
, Y, Z = AddDirection( X, Y, Z, PacketData.m_Direction )
   
local t = PacketData.m_ItemType; -- the type of item used to call the place command

   local World 
= cRoot:Get():GetWorld();
   if(
t == 6 or t==18) then -- sapling
   Log
("sapling: " .. X .. "," .. Y .. "," .. Z .. " -- adding due to Place... " .. t);
   
World:SetBlock(X, Y, Z, E_BLOCK_LOG, 0);
   
end
   self
:AddSurroundings(X, Y, Z);
   
self:AddBlock(X, Y, Z);
   return 
false
end

function TreePlugin:AddSurroundings(X, Y, Z)
   
local level_points = {
      {
X, Y+1, Z},
      };
   for 
key,val in pairs(level_points) do
      
self:AddBlock(val[1], val[2], val[3]);
   
end
end

function TreePlugin:AddBlock(X, Y, Z)
   
local World = cRoot:Get():GetWorld();
   
local t = World:GetBlock(X, Y, Z);
   if(
t == E_BLOCK_LOG) then
         Log
("addblock adding");
         
table.insert(self.TreeBlocks, {X, Y, Z});
      
end
   end

function TreePlugin:OnBlockDig( PacketData, Player )

end

function TreePlugin:Tick( DeltaTime )
   if( 
self.NumTicks < 30 ) then   -- Visible tree growth, but still awesome.
      
self.NumTicks = self.NumTicks + 1
      
return
   
end
   local debug 
= true;
   
self.NumTicks = 0

   local World 
= cRoot:Get():GetWorld()

   
local OldBlocks = self.TreeBlocks;
   
self.TreeBlocks = {};
   for 
key,val in pairs(OldBlocks) do
      
local X = val[1]
      
local Y = val[2]
      
local Z = val[3]
      
local t = World:GetBlock(X, Y, Z);
      
Log("processing: " .. X .. "," .. Y .. "," .. Z .. " -- " .. t);
         
countValue = math.random(4,7)
         
Yvalue = Y-countValue
         local down 
= World:GetBlock(X, Yvalue, Z);
         
local up = World:GetBlock(X, Y+1, Z);
         
local sSelf = World:GetBlock(X, Y, Z);
         if((
IsTree(down))) then

-- treetop
World
:SetBlock(X, Y+2, Z, E_BLOCK_LEAVES, 8);
self:AddBlock(X, Y+2, Z);         
World:SetBlock(X, Y+1, Z, E_BLOCK_LEAVES, 8);
self:AddBlock(X, Y+1, Z);

-- 
z index plane 0
World
:SetBlock(X, Y+1, Z-1, E_BLOCK_LEAVES, 8);
self:AddBlock(X, Y+1, Z-1);
World:SetBlock(X, Y+1, Z+1, E_BLOCK_LEAVES, 8);
self:AddBlock(X, Y+1, Z+1);
-- 
x index plane 0
World
:SetBlock(X-1, Y+1, Z, E_BLOCK_LEAVES, 8);
self:AddBlock(X-1, Y+1, Z);
World:SetBlock(X+1, Y+1, Z, E_BLOCK_LEAVES, 8);
self:AddBlock(X+1, Y+1, Z);
           
-- 
z index plane 1
World
:SetBlock(X, Y, Z-1, E_BLOCK_LEAVES, 8);
self:AddBlock(X, Y, Z-1);
World:SetBlock(X, Y, Z+1, E_BLOCK_LEAVES, 8);
self:AddBlock(X, Y, Z+1);
-- 
x index plane 1
World
:SetBlock(X-1, Y, Z, E_BLOCK_LEAVES, 8);
self:AddBlock(X-1, Y, Z);
World:SetBlock(X+1, Y, Z, E_BLOCK_LEAVES, 8);
self:AddBlock(X+1, Y, Z);
-- 
xz index plane 1
World
:SetBlock(X-1, Y, Z-1, E_BLOCK_LEAVES, 8);
self:AddBlock(X-1, Y, Z-1);
World:SetBlock(X+1, Y, Z+1, E_BLOCK_LEAVES, 8);
self:AddBlock(X+1, Y, Z+1);
-- 
zx index plane 1
World
:SetBlock(X-1, Y, Z+1, E_BLOCK_LEAVES, 8);
self:AddBlock(X-1, Y, Z+1);
World:SetBlock(X+1, Y, Z-1, E_BLOCK_LEAVES, 8);
self:AddBlock(X+1, Y, Z-1);    


-- 
z index plane 2
World
:SetBlock(X, Y-1, Z-1, E_BLOCK_LEAVES, 8);
self:AddBlock(X, Y-1, Z-1);
World:SetBlock(X, Y-1, Z+1, E_BLOCK_LEAVES, 8);
self:AddBlock(X, Y-1, Z+1);
-- 
x index plane 2
World
:SetBlock(X-1, Y-1, Z, E_BLOCK_LEAVES, 8);
self:AddBlock(X-1, Y-1, Z);
World:SetBlock(X+1, Y-1, Z, E_BLOCK_LEAVES, 8);
self:AddBlock(X+1, Y-1, Z);
-- 
xz index plane 2
World
:SetBlock(X-1, Y-1, Z-1, E_BLOCK_LEAVES, 8);
self:AddBlock(X-1, Y-1, Z-1);
World:SetBlock(X+1, Y-1, Z+1, E_BLOCK_LEAVES, 8);
self:AddBlock(X+1, Y-1, Z+1);
-- 
zx index plane 2
World
:SetBlock(X-1, Y-1, Z+1, E_BLOCK_LEAVES, 8);
self:AddBlock(X-1, Y-1, Z+1);
World:SetBlock(X+1, Y-1, Z-1, E_BLOCK_LEAVES, 8);
self:AddBlock(X+1, Y-1, Z-1);               
               

         
Log(" stopping growth!");

         else
               
Log("  up");
               
World:SetBlock(X, Y+1, Z, E_BLOCK_LOG, 8);
               
self:AddBlock(X, Y+1, Z);
        if(
IsSapling(sSelf)) then
Log
("is Sapling! :D");
               
World:SetBlock(X, Y, Z, E_BLOCK_LOG, 8);
               
self:AddBlock(X, Y, Z);
end        
               
end
end
end

Plugin 
= TreePlugin:new()
cRoot:Get():GetPluginManager():AddPlugin( Plugin ) 

I hope to see another update of this awesome software soon Smile
This was created by planting the trees in a 5x5 rectangle then hollowing out a door and adding stairs Smile

[Image: sndx7b.png]
Reply
Thanks given by: TheJansonD
#2
To change the default Server behaviour of dropping leaf block you can try this:
PHP Code:
function TreePlugin:OnBlockDig( PacketData, Player )
  
local World = cRoot:Get():GetWorld()
  
local X = PacketData.m_PosX
  local Y 
= PacketData.m_PosY
  local Z 
= PacketData.m_PosZ

  
-- status 0 = started digging; 2 = block broken
  
if(PacketData.m_Status == 2) then
    
if(World:GetBlock(X,Y,Z) == E_BLOCK_LEAVES) then
      
-- override default MCServer behaviour; 20% Chance of dropping saplings
      local Amount 
= math.random(1,10)
      if(
Amount <= 2) then Amount = 1
      
else Amount = 0 end
      World
:DigBlock(X, Y, Z, cItem(E_BLOCK_SAPLING, Amount))
      return 
true
    end
  end

  
-- false = allowed; true = disallowed
  
return false
end 
greets,
Tybor
Reply
Thanks given by:
#3
That worked perfectly thanksBig Grin

I'll "leaf" in the leaves code for now, but I'll implement that into my plugin that I use.
Reply
Thanks given by:
#4
(02-28-2011, 03:48 AM)codename_B Wrote: I'll "leaf" in the leaves code for now, but I'll implement that into my plugin that I use.

Ups... corrected my typo.

greets,
Tybor
Reply
Thanks given by:
#5
haha i realy like this plugin xD
Reply
Thanks given by:




Users browsing this thread: 1 Guest(s)