Skip to content

Instantly share code, notes, and snippets.

@rdococ
Last active April 9, 2024 11:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rdococ/2edc346f5f74ea1ce731a5f996d3ac24 to your computer and use it in GitHub Desktop.
Save rdococ/2edc346f5f74ea1ce731a5f996d3ac24 to your computer and use it in GitHub Desktop.
I think someone eats this
--[[
Dirt v4
It's... dirt. And a few related elements.
v4:
- Added FIRT
- DIRT falling into water now separates into CLST & SLCN
v3:
- Heat dries DIRT
- DIRT melting temperature corrected
v2:
- Increased DIRT moisture spread
- Fixed ADOB colours when generated from MUD
- Increased MUD -> ADOB transition temperature
]]
local elem, sim = elements, sim
local elemDef, elemProp = elem.element, elem.property
local partProp, partKill, exists, partCreate = sim.partProperty, sim.partKill, sim.partExists, sim.partCreate
local random, abs, sqrt = math.random, math.abs, math.sqrt
local DIRT, MUD, GRAS, ADOB, FIRT = elem.allocate("dirt", "dirt"), elem.allocate("dirt", "mud"), elem.allocate("dirt", "gras"), elem.allocate("dirt", "adob"), elem.allocate("dirt", "firt")
local function set(tbl, ...) for _, el in ipairs({...}) do tbl[el] = true end end
local waterLike = {}
set(waterLike, elem.DEFAULT_PT_WATR, elem.DEFAULT_PT_SLTW, elem.DEFAULT_PT_DSTW, elem.DEFAULT_PT_BUBW)
elemDef(DIRT, elemDef(elem.DEFAULT_PT_CLST))
elemProp(DIRT, "Name", "DIRT")
elemProp(DIRT, "Description", "Dirt. Absorbs water like a sponge, supports GRAS spread.")
elemProp(DIRT, "Color", 0x885A33)
elemProp(DIRT, "Hardness", 20)
elemProp(DIRT, "Weight", 60)
elemProp(DIRT, "Properties", elemProp(DIRT, "Properties") + elem.PROP_HOT_GLOW)
elemProp(DIRT, "HighTemperature", 3273.15)
elemProp(DIRT, "Create", function (self, x, y, type, v)
partProp(self, sim.FIELD_TMP2, random(0, 64))
end)
elemProp(DIRT, "Update", function (self, x, y, s, nt)
local temp = partProp(self, sim.FIELD_TEMP)
if temp >= 373.15 and partProp(self, sim.FIELD_LIFE) > 0 then
partProp(self, sim.FIELD_LIFE, partProp(self, sim.FIELD_LIFE) - 1)
elseif temp <= 273.15 and random(1, 100) == 1 then
partProp(self, sim.FIELD_TYPE, FIRT)
end
local vx, vy = partProp(self, sim.FIELD_VX), partProp(self, sim.FIELD_VY)
local v2 = vx * vx + vy * vy
if v2 > 1 then
for dx = -1, 1 do
for dy = -1, 1 do
local part = sim.pmap(x + dx, y + dy)
if part and part ~= self and exists(part) then
local type = partProp(part, sim.FIELD_TYPE)
if waterLike[type] then
partKill(self)
partCreate(-3, x, y, random(0, 1) == 0 and elem.DEFAULT_PT_CLST or elem.DEFAULT_PT_SLCN)
return
end
end
end
end
end
if random(1, 20) ~= 1 then return end
local initialLife = partProp(self, sim.FIELD_LIFE)
for dx = -2, 2 do
for dy = -2, 2 do
local part = sim.pmap(x + dx, y + dy)
if part and part ~= self and exists(part) then
local type = partProp(part, sim.FIELD_TYPE)
if type == DIRT then
if abs(dx) < 2 and abs(dy) < 2 then
local dx, dy = partProp(part, sim.FIELD_X) - x, partProp(part, sim.FIELD_Y) - y
local nd = dx ^ 2 + dy ^ 2 - 0.5
dx, dy = dx * 0.1, dy * 0.1
partProp(self, sim.FIELD_VX, partProp(self, sim.FIELD_VX) + dx)
partProp(self, sim.FIELD_VY, partProp(self, sim.FIELD_VY) + dy)
partProp(part, sim.FIELD_VX, partProp(part, sim.FIELD_VX) - dx)
partProp(part, sim.FIELD_VY, partProp(part, sim.FIELD_VY) - dy)
end
if initialLife > 0 and random(1, 4) == 1 then
local life, partLife = partProp(self, sim.FIELD_LIFE), partProp(part, sim.FIELD_LIFE)
if life - partLife > random(1, 16) then
partProp(self, sim.FIELD_LIFE, life - 1)
partProp(part, sim.FIELD_LIFE, partLife + 1)
end
end
elseif waterLike[type] then
if random(1, 30) == 1 then
local life = partProp(self, sim.FIELD_LIFE)
partProp(self, sim.FIELD_LIFE, life + 4)
partKill(part)
end
elseif type == GRAS then
local life = partProp(self, sim.FIELD_LIFE)
if life > 0 then
local growDX, growDY = random(-1, 1), random(-1, 1)
local growPart = sim.pmap(x + growDX, y + growDY)
partProp(self, sim.FIELD_LIFE, life - 1)
if not growPart or not exists(growPart) then
partCreate(-1, x + growDX, y + growDY, GRAS)
elseif random(1, 20) == 1 then
partProp(part, sim.FIELD_LIFE, partProp(part, sim.FIELD_LIFE) + 1)
end
end
end
end
end
end
if partProp(self, sim.FIELD_LIFE) > 64 then
partProp(self, sim.FIELD_TYPE, MUD)
end
end)
elemProp(DIRT, "Graphics", function (self, r, g, b)
local shade, moisture = partProp(self, sim.FIELD_TMP2), partProp(self, sim.FIELD_LIFE)
local brightness = 1 - (moisture / 128)
r, g, b = r + shade, g + shade, b + shade
r, g, b = r * brightness, g * brightness, b * brightness
return 0, ren.PMODE_FLAT, 255, r, g, b, 255, r, g, b
end)
elemDef(GRAS, elemDef(elem.DEFAULT_PT_PLNT))
elemProp(GRAS, "Name", "GRAS")
elemProp(GRAS, "Description", "Grass. Thrives on moist dirt.")
elemProp(GRAS, "Color", 0x009900)
elemProp(GRAS, "Properties", elem.TYPE_SOLID + elem.PROP_NEUTPENETRATE)
elemProp(GRAS, "Create", function (self, x, y, type, v)
partProp(self, sim.FIELD_TMP2, random(0, 64))
end)
elemProp(GRAS, "Update", function (self, x, y, s, nt)
local part = sim.pmap(x, y - 1)
local life = partProp(self, sim.FIELD_LIFE)
if part and part ~= self and exists(part) then
local type = partProp(part, sim.FIELD_TYPE)
if waterLike[type] then
partKill(part)
life = life + (random(1, 20) == 1 and 1 or 0)
partProp(self, sim.FIELD_LIFE, life)
elseif type == GRAS then
partProp(self, sim.FIELD_LIFE, 0)
partProp(part, sim.FIELD_LIFE, life)
return
else
return
end
end
if life > 0 then
partProp(self, sim.FIELD_LIFE, 0)
local newGrass = partCreate(-1, x, y - 1, GRAS)
partProp(newGrass, sim.FIELD_LIFE, life - 1)
partProp(newGrass, sim.FIELD_TMP2, partProp(self, sim.FIELD_TMP2))
end
end)
elemProp(GRAS, "Graphics", function (self, r, g, b)
local shade = partProp(self, sim.FIELD_TMP2)
r, g, b = r + shade, g + shade, b + shade
return 0, ren.PMODE_FLAT, 255, r, g, b, 255, r, g, b
end)
elemDef(MUD, elemDef(elem.DEFAULT_PT_WATR))
elemProp(MUD, "Name", "MUD")
elemProp(MUD, "Description", "Mud. Mixture of water and dirt. Dries into adobe.")
elemProp(MUD, "Color", 0x773A00)
elemProp(MUD, "Hardness", 20)
elemProp(MUD, "Weight", 60)
elemProp(MUD, "HighTemperature", math.huge)
elemProp(MUD, "Update", function (self, x, y, s, nt)
if partProp(self, sim.FIELD_TEMP) > 473.15 then
partProp(self, sim.FIELD_TYPE, ADOB)
partProp(self, sim.FIELD_TMP2, random(0, 64))
end
if random(1, 5) ~= 1 then return end
for dx = -1, 1 do
for dy = -1, 1 do
local part = sim.pmap(x + dx, y + dy)
if part and part ~= self and exists(part) then
local type = partProp(part, sim.FIELD_TYPE)
if type == MUD then
local dx, dy = partProp(part, sim.FIELD_X) - x, partProp(part, sim.FIELD_Y) - y
local nd = dx ^ 2 + dy ^ 2 - 0.5
dx, dy = dx * 0.1, dy * 0.1
partProp(self, sim.FIELD_VX, partProp(self, sim.FIELD_VX) + dx)
partProp(self, sim.FIELD_VY, partProp(self, sim.FIELD_VY) + dy)
partProp(part, sim.FIELD_VX, partProp(part, sim.FIELD_VX) - dx)
partProp(part, sim.FIELD_VY, partProp(part, sim.FIELD_VY) - dy)
end
end
end
end
end)
elemDef(ADOB, elemDef(elem.DEFAULT_PT_ROCK))
elemProp(ADOB, "Name", "ADOB")
elemProp(ADOB, "Description", "Adobe. Dried mud, useful building material.")
elemProp(ADOB, "Color", 0xAA8855)
elemProp(ADOB, "Hardness", 20)
elemProp(ADOB, "Create", function (self, x, y, type, v)
partProp(self, sim.FIELD_TMP2, random(0, 64))
end)
elemProp(ADOB, "Update", function (self, x, y, s, nt)
if random(1, 5) ~= 1 then return end
for dx = -2, 2 do
for dy = -2, 2 do
local part = sim.pmap(x + dx, y + dy)
if part and part ~= self and exists(part) then
local type = partProp(part, sim.FIELD_TYPE)
if waterLike[type] then
partProp(self, sim.FIELD_TYPE, MUD)
partKill(part)
return
end
end
end
end
end)
elemProp(ADOB, "Graphics", function (self, r, g, b)
local shade = partProp(self, sim.FIELD_TMP2)
r, g, b = r + shade, g + shade, b + shade
return 0, ren.PMODE_FLAT, 255, r, g, b, 255, r, g, b
end)
elemDef(FIRT, elemDef(DIRT))
elemProp(FIRT, "Name", "FIRT")
elemProp(FIRT, "Description", "Firt. The result of exposing dirt to freezing temperatures.")
elemProp(FIRT, "Color", 0x88AAFF)
elemProp(FIRT, "Temperature", 258.15)
elemProp(FIRT, "HighTemperature", math.huge)
elemProp(FIRT, "HighTemperatureTransition", DIRT)
elemProp(FIRT, "Create", function (self, x, y, type, v)
partProp(self, sim.FIELD_TMP2, random(0, 64))
end)
elemProp(FIRT, "Update", function (self, x, y, s, nt)
if partProp(self, sim.FIELD_TEMP) >= 273.15 and random(1, 100) == 1 then
partProp(self, sim.FIELD_LIFE, 48)
partProp(self, sim.FIELD_TYPE, DIRT)
end
end)
elemProp(FIRT, "Graphics", function (self, r, g, b)
local shade = partProp(self, sim.FIELD_TMP2)
r, g, b = r + shade, g + shade, b + shade
return 0, ren.PMODE_FLAT, 255, r, g, b, 255, r, g, b
end)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment