starlit  Diff

Differences From Artifact [de51a702a5]:

To Artifact [e94ad91b55]:


1
2
3
4
5
6
7
8
9
10
11
12
..
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
..
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
..
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
...
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
...
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
...
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
...
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
local lib = starlit.mod.lib
local world = starlit.world

function world.date()
	local days = minetest.get_day_count()
	local year = math.floor(days / world.planet.orbit);
	local day = days % world.planet.orbit;
	return {
		year = year, day = day;
		season = day / world.planet.orbit + 0.5; -- begin summer
	}
end
................................................................................
local heatRange = {min = -50, max = 50} -- translate mt temps into real temps

-- this function provides the basis for temperature calculation,
-- which is performed by adding this value to the ambient temperature,
-- determined by querying nearby group:heatSource items in accordance
-- with the inverse-square law
function world.climate.eval(pos, tod, season)
	local data = minetest.get_biome_data(pos)
	local biome = world.ecology.biomes.db[minetest.get_biome_name(data.biome)]
-- 	print('climate:', dump(data))
	local heat, humid = data.heat, data.humidity
	heat = lerp(heat/100, heatRange.min, heatRange.max)
	tod = tod or minetest.get_timeofday()
	heat = lerp(math.abs(tod - 0.5)*2, heat, heat + biome.nightTempDelta)
-- 	print('base heat', heat)

	local td = world.date()
	heat = heat + gradient(biome.seasonalTemp, season or td.season)
-- 	print('seasonal heat', heat)
	if pos.y > 0 then
................................................................................

local vdsq = lib.math.vdsq
function world.climate.temp(pos, timeshift) --> irradiance at pos in W
	local cl = world.climate.eval(pos)
	local radCenters = starlit.region.radiator.store:get_areas_for_pos(pos, false, true)
	local irradiance = 0
	for _,e in pairs(radCenters) do
		local rpos = minetest.string_to_pos(e.data)
		local rdef = assert(minetest.registered_nodes[assert(minetest.get_node(rpos)).name])
		local rc = rdef._starlit.radiator
		local r_max = rc.radius(rpos)

		local dist_sq = vdsq(rpos,pos)
		if dist_sq <= r_max^2 then
			-- cheap bad way
			-- if minetest.line_of_sight(rpos,pos) then
			--
			-- expensive way
			local obstruct = 0
			local ray = Raycast(rpos, pos, true, true)
			for p in ray do
				if p.type == 'node' then obstruct = obstruct + 1 end
			end
................................................................................
	end
	local w = world.climate.weatherAt(pos, timeshift)

	return irradiance + cl.surfaceTemp
end

function world.ecology.biomeAt(pos)
	return world.ecology.biomes.db[minetest.get_biome_name(minetest.get_biome_data(pos).biome)]
end


minetest.after(0, function()
	world.climate.weatherMap.kind = minetest.get_perlin {
		seed = 0x925afe;
		octaves = 2;
		spread = vector.new(256,256,120);
	};
	world.climate.weatherMap.severity = minetest.get_perlin {
		seed = 0x39de1d;
		octaves = 1;
		spread = vector.new(256,256,60);
	};
end)

function world.climate.weatherAt(pos, timeshift)
	timeshift = timeshift or 0
	local wv  = world.climate.weatherMap.kind:get_3d(vector.new(pos.x, pos.z, minetest.get_gametime() + timeshift))
	local sev = world.climate.weatherMap.severity:get_3d(vector.new(pos.x, pos.z, minetest.get_gametime() + timeshift))
	local b = world.ecology.biomeAt(pos)
	local w = 'starlit:clear'
	for i,v in ipairs(b.weather) do
		if wv < v[1] then
			w = v[2]
			break
		end
................................................................................
world.climate.weather.link('starlit:meteorShower', {
	name = 'Meteor Shower';
	danger = 2;
})

world.ecology.biomes.foreach('starlit:biome-gen', {}, function(id, b)
	b.def.name = id
	minetest.register_biome(b.def)
end)

world.ecology.plants.foreach('starlit:plant-gen', {}, function(id, b)
	local stageCt = #b.stages
	local function stageID(n)
		if n == stageCt then return id end
		return id .. string.format('_stage_%s', n)
................................................................................
				end;
			};
		}
		if st.swap then
			base.node_dig_prediction = ""
			function base.after_dig_node(pos, node, digger)
				node.name = stageID(st.swap)
				minetest.swap_node(pos, node)
				return true
			end
		end
		if st.biolum then base.light_source = st.biolum; end
		return base
	end
	for i, v in ipairs(b.stages) do
		local n = regStage(i, v)
		minetest.register_node(stageID(i), n)
		b.stageNodes[i] = stageID(i)
	end
	b.fullyGrown = stageID(stageCt)

	local dec = {
		deco_type = 'simple';
		decoration = b.stageNodes;
		height = 1;
		param2 = b.meshOpt or 0;
	}
	for k,v in pairs(b.decoration) do dec[k] = v end
	b.decoration = minetest.register_decoration(dec)
end)

local toward = lib.math.toward
local hfinterval = 1.5
starlit.startJob('starlit:temps', hfinterval, function(delta)

	-- our base thermal conductivity (κ) is measured in °C/°C/s. say the
................................................................................
	--   d  = Tₑ − Tₚ = -40°C
	--   ΔT = κ×d = -.4°C/s
	-- too cold:
	--		x = beginning of danger zone
	--    κ × (x - Tₚ) = y where y < Tₚ
	-- our final change in temperature is computed as tΔC where t is time
	local kappa = starlit.constant.heat.thermalConductivity
	local now = minetest.get_gametime()
	for name,user in pairs(starlit.activeUsers) do
		local tr = user:species().tempRange
		local t = starlit.world.climate.temp(user.entity:get_pos())

		local weather,wsev = starlit.world.climate.weatherAt(user.entity:get_pos())
		local wfac
		if user.env.weather == nil
................................................................................
world.ecology.trees.foreach('starlit:tree-gen', {}, function(id, t)
	for i,td in ipairs(t.decorate) do
		local dec = {
			deco_type = 'lsystem';
			treedef = t.def;
		}
		for k,v in pairs(td) do dec[k]=v end
		minetest.register_decoration(dec)
	end
end)

minetest.register_abm {
	label = "plant growth";
	nodenames = {'group:plant_grow'};
	chance = 15;
	interval = 20;
	catch_up = true;
	action = function(pos, node)
		local def = minetest.registered_nodes[node.name]._starlit.plant
		-- 5 W: maximum power for UV lamps
		-- 7 W: maximum solar power
		local uv = (minetest.get_natural_light(pos) / 15) * 7
		-- TODO compute artificial contribution
		local req = lib.tbl.defaults({
			uv = 3;
			soil = 'soil';
			temp = -10;
			humid = nil;
		}, def.growReq);

		-- TODO check other reqs

		if uv > req.uv then
			local plant = starlit.world.ecology.plants.db[def.id]
			local nextStage = plant.stageNodes[def.stage + 1]
			minetest.swap_node(pos, {name=nextStage})
		end
	end;
}




|







 







|
|



|







 







|
|






|







 







|



|
|




|








|
|







 







|







 







|








|











|







 







|







 







|



|






|


|













|



1
2
3
4
5
6
7
8
9
10
11
12
..
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
..
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
..
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
...
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
...
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
...
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
...
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
local lib = starlit.mod.lib
local world = starlit.world

function world.date()
	local days = core.get_day_count()
	local year = math.floor(days / world.planet.orbit);
	local day = days % world.planet.orbit;
	return {
		year = year, day = day;
		season = day / world.planet.orbit + 0.5; -- begin summer
	}
end
................................................................................
local heatRange = {min = -50, max = 50} -- translate mt temps into real temps

-- this function provides the basis for temperature calculation,
-- which is performed by adding this value to the ambient temperature,
-- determined by querying nearby group:heatSource items in accordance
-- with the inverse-square law
function world.climate.eval(pos, tod, season)
	local data = core.get_biome_data(pos)
	local biome = world.ecology.biomes.db[core.get_biome_name(data.biome)]
-- 	print('climate:', dump(data))
	local heat, humid = data.heat, data.humidity
	heat = lerp(heat/100, heatRange.min, heatRange.max)
	tod = tod or core.get_timeofday()
	heat = lerp(math.abs(tod - 0.5)*2, heat, heat + biome.nightTempDelta)
-- 	print('base heat', heat)

	local td = world.date()
	heat = heat + gradient(biome.seasonalTemp, season or td.season)
-- 	print('seasonal heat', heat)
	if pos.y > 0 then
................................................................................

local vdsq = lib.math.vdsq
function world.climate.temp(pos, timeshift) --> irradiance at pos in W
	local cl = world.climate.eval(pos)
	local radCenters = starlit.region.radiator.store:get_areas_for_pos(pos, false, true)
	local irradiance = 0
	for _,e in pairs(radCenters) do
		local rpos = core.string_to_pos(e.data)
		local rdef = assert(core.registered_nodes[assert(core.get_node(rpos)).name])
		local rc = rdef._starlit.radiator
		local r_max = rc.radius(rpos)

		local dist_sq = vdsq(rpos,pos)
		if dist_sq <= r_max^2 then
			-- cheap bad way
			-- if core.line_of_sight(rpos,pos) then
			--
			-- expensive way
			local obstruct = 0
			local ray = Raycast(rpos, pos, true, true)
			for p in ray do
				if p.type == 'node' then obstruct = obstruct + 1 end
			end
................................................................................
	end
	local w = world.climate.weatherAt(pos, timeshift)

	return irradiance + cl.surfaceTemp
end

function world.ecology.biomeAt(pos)
	return world.ecology.biomes.db[core.get_biome_name(core.get_biome_data(pos).biome)]
end


core.after(0, function()
	world.climate.weatherMap.kind = core.get_perlin {
		seed = 0x925afe;
		octaves = 2;
		spread = vector.new(256,256,120);
	};
	world.climate.weatherMap.severity = core.get_perlin {
		seed = 0x39de1d;
		octaves = 1;
		spread = vector.new(256,256,60);
	};
end)

function world.climate.weatherAt(pos, timeshift)
	timeshift = timeshift or 0
	local wv  = world.climate.weatherMap.kind:get_3d(vector.new(pos.x, pos.z, core.get_gametime() + timeshift))
	local sev = world.climate.weatherMap.severity:get_3d(vector.new(pos.x, pos.z, core.get_gametime() + timeshift))
	local b = world.ecology.biomeAt(pos)
	local w = 'starlit:clear'
	for i,v in ipairs(b.weather) do
		if wv < v[1] then
			w = v[2]
			break
		end
................................................................................
world.climate.weather.link('starlit:meteorShower', {
	name = 'Meteor Shower';
	danger = 2;
})

world.ecology.biomes.foreach('starlit:biome-gen', {}, function(id, b)
	b.def.name = id
	core.register_biome(b.def)
end)

world.ecology.plants.foreach('starlit:plant-gen', {}, function(id, b)
	local stageCt = #b.stages
	local function stageID(n)
		if n == stageCt then return id end
		return id .. string.format('_stage_%s', n)
................................................................................
				end;
			};
		}
		if st.swap then
			base.node_dig_prediction = ""
			function base.after_dig_node(pos, node, digger)
				node.name = stageID(st.swap)
				core.swap_node(pos, node)
				return true
			end
		end
		if st.biolum then base.light_source = st.biolum; end
		return base
	end
	for i, v in ipairs(b.stages) do
		local n = regStage(i, v)
		core.register_node(stageID(i), n)
		b.stageNodes[i] = stageID(i)
	end
	b.fullyGrown = stageID(stageCt)

	local dec = {
		deco_type = 'simple';
		decoration = b.stageNodes;
		height = 1;
		param2 = b.meshOpt or 0;
	}
	for k,v in pairs(b.decoration) do dec[k] = v end
	b.decoration = core.register_decoration(dec)
end)

local toward = lib.math.toward
local hfinterval = 1.5
starlit.startJob('starlit:temps', hfinterval, function(delta)

	-- our base thermal conductivity (κ) is measured in °C/°C/s. say the
................................................................................
	--   d  = Tₑ − Tₚ = -40°C
	--   ΔT = κ×d = -.4°C/s
	-- too cold:
	--		x = beginning of danger zone
	--    κ × (x - Tₚ) = y where y < Tₚ
	-- our final change in temperature is computed as tΔC where t is time
	local kappa = starlit.constant.heat.thermalConductivity
	local now = core.get_gametime()
	for name,user in pairs(starlit.activeUsers) do
		local tr = user:species().tempRange
		local t = starlit.world.climate.temp(user.entity:get_pos())

		local weather,wsev = starlit.world.climate.weatherAt(user.entity:get_pos())
		local wfac
		if user.env.weather == nil
................................................................................
world.ecology.trees.foreach('starlit:tree-gen', {}, function(id, t)
	for i,td in ipairs(t.decorate) do
		local dec = {
			deco_type = 'lsystem';
			treedef = t.def;
		}
		for k,v in pairs(td) do dec[k]=v end
		core.register_decoration(dec)
	end
end)

core.register_abm {
	label = "plant growth";
	nodenames = {'group:plant_grow'};
	chance = 15;
	interval = 20;
	catch_up = true;
	action = function(pos, node)
		local def = core.registered_nodes[node.name]._starlit.plant
		-- 5 W: maximum power for UV lamps
		-- 7 W: maximum solar power
		local uv = (core.get_natural_light(pos) / 15) * 7
		-- TODO compute artificial contribution
		local req = lib.tbl.defaults({
			uv = 3;
			soil = 'soil';
			temp = -10;
			humid = nil;
		}, def.growReq);

		-- TODO check other reqs

		if uv > req.uv then
			local plant = starlit.world.ecology.plants.db[def.id]
			local nextStage = plant.stageNodes[def.stage + 1]
			core.swap_node(pos, {name=nextStage})
		end
	end;
}