Very Basic Growing Cacti!
#1
I was bored with cactus not growing, so I thought I'd take the opportunity to implement a more fun growth mechanism for cactusTongue

Trees are coming soon too btw using the same implementation!



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

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

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

function CactiPlugin:Initialize()
   
self:SetName( "Cacti" )
   
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.CactiBlocks = {};
   
self.CactiBlocks = {};

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

function IsFreeForCacti(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 IsCacti(block)
   return 
block == E_BLOCK_CACTUS;
end

function IsBurnable(block)
    return 
block == E_BLOCK_LOG
      
or block == E_BLOCK_LEAVES;
end

function CactiPlugin: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 == 81) then -- cactus
   Log
("cactus: " .. X .. "," .. Y .. "," .. Z .. " -- adding due to Place... " .. t);
   
World:SetBlock(X, Y, Z, E_BLOCK_CACTUS, 0);
   
end
   self
:AddSurroundings(X, Y, Z);
   
self:AddBlock(X, Y, Z);
   return 
false
end

function CactiPlugin: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 CactiPlugin:AddBlock(X, Y, Z)
   
local World = cRoot:Get():GetWorld();
   
local t = World:GetBlock(X, Y, Z);
   if(
t == E_BLOCK_CACTUS) then
         Log
("addblock adding");
         
table.insert(self.CactiBlocks, {X, Y, Z});
      
end
   end

function CactiPlugin:OnBlockDig( PacketData, Player )

end

function CactiPlugin:Tick( DeltaTime )
   if( 
self.NumTicks < 1 ) then   -- Instant grow cactus for testing purposes
      self
.NumTicks = self.NumTicks + 1
      
return
   
end
   local debug 
= true;
   
self.NumTicks = 0

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

   
local OldBlocks = self.CactiBlocks;
   
self.CactiBlocks = {};
   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(2,5)
         
Yvalue = Y-countValue
         local down 
= World:GetBlock(X, Yvalue, Z);
         
local up = World:GetBlock(X, Y+1, Z);
         if((
IsCacti(down))) then
         forkGrowth 
= math.random(0,7) -- probability of forking - this is a 1/3 chance.
         if(
forkGrowth<2) then
         
         Log
(" Forking growth");
              if(
forkGrowth==1) then
               World
:SetBlock(X-1, Y, Z, E_BLOCK_CACTUS, 8);
               
self:AddBlock(X-1, Y, Z);
               
World:SetBlock(X+1, Y, Z, E_BLOCK_CACTUS, 8);
               
self:AddBlock(X+1, Y, Z);
               
end
               
               
if(forkGrowth==0) then
               World
:SetBlock(X, Y, Z-1, E_BLOCK_CACTUS, 8);
               
self:AddBlock(X, Y, Z-1);
               
World:SetBlock(X, Y, Z+1, E_BLOCK_CACTUS, 8);
               
self:AddBlock(X, Y, Z+1);
               
end
               
         end
         Log
(" stopping growth!");

         else
               
Log("  up");
               
World:SetBlock(X, Y+1, Z, E_BLOCK_CACTUS, 8);
               
self:AddBlock(X, Y+1, Z);
end
end
end

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


These are all automatically generated by the plugin with "forking" occuring 1/3 times, completely randomly, and cactus height also random.

The destruction implementation of cactus hasn't been done yet because I can't figure out the digging API, but feel free to inform me Smile
[Image: 2nk10dj.jpg]
Reply
Thanks given by:
#2
Cool
Very Nice!

greets,
Tybor
Reply
Thanks given by:
#3
EpicTongue
Reply
Thanks given by:




Users browsing this thread: 1 Guest(s)