Bush/Tree Element

  • FM22
    28th Jul 2014 Member 2 Permalink

    This code adds an element called SEDS (SEED was taken by the LIFE type) to the game. When watered, SEDS grows into a bush/tree thing. It soaks up the water as it grows.

    --FM22
    local seed=elements.allocate("FMOD", "SEDS")
    elements.element(elements.FMOD_PT_SEDS, elements.element(elements.DEFAULT_PT_SAND))
    elements.property(elements.FMOD_PT_SEDS, "Name", "SEDS")
    elements.property(elements.FMOD_PT_SEDS, "Description", "Seeds. Water them to create bushes.")
    elements.property(elements.FMOD_PT_SEDS, "Flammable", 5)
    local function grow(i,x,y,s,n)
    if tpt.get_property('life',x,y)>0 then
    tpt.set_property('life',tpt.get_property('life',x,y)-1,x,y)
    end
    for i=-1,1,1 do
    for j=-1,1,1 do
    if tpt.get_property('life',x,y)==0 and tpt.get_property('type',x+i,y+j)==tpt.el.watr.id then
    tpt.set_property('life',50,x,y)
    tpt.set_property('tmp',tpt.get_property('tmp',x,y)+1,x,y)
    tpt.delete(x+i,y+j)
    end
    end
    end
    if tpt.get_property('tmp',x,y)>4 then
    local height=math.random(-40,-20)
    local xo=0
    for col=-2,2,1 do
    xo=col
    for row=0,height,-1 do
    xo=xo+math.random(-1,1)
    if tpt.get_property('type',x+xo,y+row)<3 or tpt.get_property('type',x+xo,y+row)==tpt.el.wood.id or tpt.el.plnt.id or tpt.el.vine.id then
    tpt.delete(x+xo,y+row)
    tpt.create(x+xo,y+row,'wood')
    for rx=-1,1,1 do
    for ry=-1,1,1 do
    if tpt.get_property('type',x+xo+rx,y+row+ry)<3 and math.random(1,10)==1 then
    tpt.delete(x+xo+rx,y+row+ry)
    tpt.create(x+xo+rx,y+row+ry,'vine')
    end
    end
    end
    else
    break
    end
    end
    end
    tpt.set_property('type',tpt.el.wood.id,x,y)
    end
    end
    tpt.element_func(grow,seed)

    Edited once by FM22. Last: 28th Jul 2014