Cuberite Forum
Very Basic Growing Cacti! - Printable Version

+- Cuberite Forum (https://forum.cuberite.org)
+-- Forum: Plugins (https://forum.cuberite.org/forum-1.html)
+--- Forum: Plugin Releases (https://forum.cuberite.org/forum-2.html)
+---- Forum: Archived Plugins (https://forum.cuberite.org/forum-18.html)
+---- Thread: Very Basic Growing Cacti! (/thread-29.html)



Very Basic Growing Cacti! - codename_B - 02-28-2011

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(tCactiPlugin)
   
local w Lua__cPlugin:new()
   
tolua.setpeer(wt)
   
w:tolua__set_instance(w)
   return 
w
end

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

function CactiPlugin:Initialize()
   
self:SetName"Cacti" )
   
self:SetVersion)

   
PluginManager cRoot:Get():GetPluginManager()
   
PluginManager:AddHookselfcPluginManager.E_PLUGIN_TICK )
   
PluginManager:AddHookselfcPluginManager.E_PLUGIN_BLOCK_DIG )
   
PluginManager:AddHookselfcPluginManager.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:OnBlockPlacePacketDataPlayer )

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

   local World 
cRoot:Get():GetWorld();
   if(
== 81then -- cactus
   Log
("cactus: " .. .. "," .. .. "," .. .. " -- adding due to Place... " .. t);
   
World:SetBlock(XYZE_BLOCK_CACTUS0);
   
end
   self
:AddSurroundings(XYZ);
   
self:AddBlock(XYZ);
   return 
false
end

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

function CactiPlugin:AddBlock(XYZ)
   
local World cRoot:Get():GetWorld();
   
local t World:GetBlock(XYZ);
   if(
== E_BLOCK_CACTUSthen
         Log
("addblock adding");
         
table.insert(self.CactiBlocks, {XYZ});
      
end
   end

function CactiPlugin:OnBlockDigPacketDataPlayer )

end

function CactiPlugin:TickDeltaTime )
   if( 
self.NumTicks 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(XYZ);
      
Log("processing: " .. .. "," .. .. "," .. .. " -- " .. t);
         
countValue math.random(2,5)
         
Yvalue Y-countValue
         local down 
World:GetBlock(XYvalueZ);
         
local up World:GetBlock(XY+1Z);
         if((
IsCacti(down))) then
         forkGrowth 
math.random(0,7) -- probability of forking this is a 1/3 chance.
         if(
forkGrowth<2then
         
         Log
(" Forking growth");
              if(
forkGrowth==1then
               World
:SetBlock(X-1YZE_BLOCK_CACTUS8);
               
self:AddBlock(X-1YZ);
               
World:SetBlock(X+1YZE_BLOCK_CACTUS8);
               
self:AddBlock(X+1YZ);
               
end
               
               
if(forkGrowth==0then
               World
:SetBlock(XYZ-1E_BLOCK_CACTUS8);
               
self:AddBlock(XYZ-1);
               
World:SetBlock(XYZ+1E_BLOCK_CACTUS8);
               
self:AddBlock(XYZ+1);
               
end
               
         end
         Log
(" stopping growth!");

         else
               
Log("  up");
               
World:SetBlock(XY+1ZE_BLOCK_CACTUS8);
               
self:AddBlock(XY+1Z);
end
end
end

Plugin 
CactiPlugin:new()
cRoot:Get():GetPluginManager():AddPluginPlugin 


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]


RE: Very Basic Growing Cacti! - Tybor - 02-28-2011

Cool
Very Nice!

greets,
Tybor


RE: Very Basic Growing Cacti! - FakeTruth - 02-28-2011

EpicTongue