starlit  Diff

Differences From Artifact [e59f2eda8d]:

To Artifact [4ca9d32d80]:


70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
..
96
97
98
99
100
101
102
103
104







105
106
107
108
109
110
111
112
113
114

115
116
117
118
119
120
121
122
123
124
125
126
127
128
...
138
139
140
141
142
143
144
145


146
147
148
149
150
151
152
...
154
155
156
157
158
159
160
161


162
163
164
165
166
167
168







169
170
171
172
173
174
175
...
207
208
209
210
211
212
213
214
215
216

217
218
219
220
221
222
223
...
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
E.schematicGroups.link('starsoul_electronics:battery', {
	title = 'Battery', icon = 'starsoul-item-battery.png';
	description = 'Portable power storage cells are essential to all aspects of survival';
})

E.schematicGroups.link('starsoul_electronics:decayCell', {
	title = 'Decay Cell', icon = 'starsoul-item-decaycell.png';
	description = "Radioisotope generators can pack much more power into a smaller amount of space than conventional battery, but they can't be recharged, dump power and heat whether they're in use or not, and their power yield drop towards zero over their usable lifetime.";
})


-------------------------
-- batteries & dynamos --
-------------------------

................................................................................
		return fn(stack,
		          stack:get_definition()._starsoul[ty],
		          stack:get_meta(), ...)
	end
end

-- return a wear level that won't destroy the item
local function safeWear(fac) return math.min(math.max(fac,0),1) * 0xFFFE end
local function safeWearToFac(w) return w/0xFFFE end








-- E.battery.capacity(bat) --> charge (J)
E.battery.capacity = accessor('battery', function(stack, batClass, meta)
	local dmg = meta:get_int('starsoul_electronics:battery_degrade') -- µJ/μW
	local dmg_J = dmg / 1000
	return (batClass.capacity - dmg_J)
end)

-- E.battery.charge(bat) --> charge (J)
E.battery.charge = accessor('battery', function(stack, batClass, meta)

	local fac = 1 - safeWearToFac(stack:get_wear())
	return E.battery.capacity(stack) * fac
end)

-- E.battery.dischargeRate(bat) --> dischargeRate (W)
E.battery.dischargeRate = accessor('battery', function(stack, batClass, meta)
	local dmg = meta:get_int('starsoul_electronics:battery_degrade') -- µJ/μW
	local dmg_W = dmg / 1000
	return batClass.dischargeRate - dmg_W
end);


-- E.battery.drawCurrent(bat, power, time, test) --> supply (J), wasteHeat (J)
--       bat   = battery stack
................................................................................

	if not test then
		local degrade = m:get_int 'starsoul_electronics:battery_degrade' or 0
		degrade = degrade + maxPower * bc.decay
		-- for each joule of power drawn, capacity degrades by `decay` J
		-- this should ordinarily be on the order of mJ or smaller
		m:set_int('starsoul_electronics:battery_degrade', degrade)
		s:set_wear(safeWear(1 - (ch / E.battery.capacity(s))))


	end

	return maxPower, 0 -- FIXME specify waste heat
end)

-- E.battery.recharge(bat, power, time) --> draw (J)
--     bat   = battery stack
................................................................................
--    time s = the amount of time available for this transaction
--    draw J = how much power was actually drawn in $time seconds
E.battery.recharge = accessor('battery', function(s, bc, m, power, time)
	local ch = E.battery.charge(s)
	local cap = E.battery.capacity(s)
	local maxPower = math.min(E.battery.dischargeRate(s)*time, power)
	local total = math.min(ch + maxPower, cap)
	s:set_wear(safeWear(1 - (total/cap)))


	return maxPower, 0 -- FIXME
end)

E.battery.setCharge = accessor('battery', function(s, bc, m, newPower)
	local cap = E.battery.capacity(s)
	local power = math.min(cap, newPower)
	s:set_wear(safeWear(1 - (power/cap)))







end)

E.dynamo = { kind = {} }

E.dynamo.drawCurrent = accessor('dynamo', function(s,c,m, power, time, test)
	return c.vtable.drawCurrent(s, power, time, test)
end)
................................................................................
				{ title = 'Size', affinity = 'info';
					desc = lib.math.si('m', def.fab.size.print) };
			};
		};
		_starsoul = {
			event = {
				create = function(st, how)
					if not how.gift then -- cheap hack to make starting batteries fully charged
						E.battery.setCharge(st, 0)
					end

				end;
			};
			fab = def.fab;
			dynamo = {
				vtable = E.dynamo.kind.battery;
			};
			battery = def;
................................................................................
	end;
	__index = {
		read = function(self)
			local dat = E.chip.read(self.chip)
			return dat.files[self.inode]
		end;
		write = function(self,data)
			print('writing', self.chip, self.inode)
			return E.chip.fileWrite(self.chip, self.inode, data)
		end;
		erase = function(self)
			local dat = E.chip.read(self.chip)
			table.remove(dat.files, self.inode)
			E.chip.write(self.chip, dat)
			self.inode = nil







|







 







|
|
>
>
>
>
>
>
>



|






>
|





|







 







|
>
>







 







|
>
>






|
>
>
>
>
>
>
>







 







|

|
>







 







|







70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
..
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
...
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
...
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
...
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
...
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
E.schematicGroups.link('starsoul_electronics:battery', {
	title = 'Battery', icon = 'starsoul-item-battery.png';
	description = 'Portable power storage cells are essential to all aspects of survival';
})

E.schematicGroups.link('starsoul_electronics:decayCell', {
	title = 'Decay Cell', icon = 'starsoul-item-decaycell.png';
	description = "Radioisotope generators can pack much more power into a smaller amount of space than conventional batteries, but they can't be recharged, dump power and heat whether they're in use or not, and their power yield drops towards zero over their usable lifetime.";
})


-------------------------
-- batteries & dynamos --
-------------------------

................................................................................
		return fn(stack,
		          stack:get_definition()._starsoul[ty],
		          stack:get_meta(), ...)
	end
end

-- return a wear level that won't destroy the item
-- local function safeWear(fac) return math.min(math.max(fac,0),1) * 0xFFFE end
-- local function safeWearToFac(w) return w/0xFFFE end

E.battery.update = accessor('battery', function(stack, batClass, meta)
	-- local cap = E.battery.capacity(stack)
	local charge = meta:get_float 'starsoul_electronics:battery_charge'
	meta:set_string('count_meta', string.format('%s%%', math.floor(charge * 100)))
	meta:set_int('count_alignment', bit.lshift(3, 2) + 2)
end)

-- E.battery.capacity(bat) --> charge (J)
E.battery.capacity = accessor('battery', function(stack, batClass, meta)
	local dmg = meta:get_int 'starsoul_electronics:battery_degrade' -- µJ/μW
	local dmg_J = dmg / 1000
	return (batClass.capacity - dmg_J)
end)

-- E.battery.charge(bat) --> charge (J)
E.battery.charge = accessor('battery', function(stack, batClass, meta)
	local fac = meta:get_float 'starsoul_electronics:battery_charge'
	-- local fac = 1 - safeWearToFac(stack:get_wear())
	return E.battery.capacity(stack) * fac
end)

-- E.battery.dischargeRate(bat) --> dischargeRate (W)
E.battery.dischargeRate = accessor('battery', function(stack, batClass, meta)
	local dmg = meta:get_int 'starsoul_electronics:battery_degrade' -- µJ/μW
	local dmg_W = dmg / 1000
	return batClass.dischargeRate - dmg_W
end);


-- E.battery.drawCurrent(bat, power, time, test) --> supply (J), wasteHeat (J)
--       bat   = battery stack
................................................................................

	if not test then
		local degrade = m:get_int 'starsoul_electronics:battery_degrade' or 0
		degrade = degrade + maxPower * bc.decay
		-- for each joule of power drawn, capacity degrades by `decay` J
		-- this should ordinarily be on the order of mJ or smaller
		m:set_int('starsoul_electronics:battery_degrade', degrade)
		-- s:set_wear(safeWear(1 - (ch / E.battery.capacity(s))))
		m:set_float('starsoul_electronics:battery_charge', ch / E.battery.capacity(s))
		E.battery.update(s)
	end

	return maxPower, 0 -- FIXME specify waste heat
end)

-- E.battery.recharge(bat, power, time) --> draw (J)
--     bat   = battery stack
................................................................................
--    time s = the amount of time available for this transaction
--    draw J = how much power was actually drawn in $time seconds
E.battery.recharge = accessor('battery', function(s, bc, m, power, time)
	local ch = E.battery.charge(s)
	local cap = E.battery.capacity(s)
	local maxPower = math.min(E.battery.dischargeRate(s)*time, power)
	local total = math.min(ch + maxPower, cap)
	-- s:set_wear(safeWear(1 - (total/cap)))
	m:set_float('starsoul_electronics:battery_charge', total/cap)
	E.battery.update(s)
	return maxPower, 0 -- FIXME
end)

E.battery.setCharge = accessor('battery', function(s, bc, m, newPower)
	local cap = E.battery.capacity(s)
	local power = math.min(cap, newPower)
	-- s:set_wear(safeWear(1 - (power/cap)))
	m:set_float('starsoul_electronics:battery_charge', power/cap)
	E.battery.update(s)
end)
E.battery.setChargeF = accessor('battery', function(s, bc, m, newPowerF)
	local power = math.min(1.0, newPowerF)
	m:set_float('starsoul_electronics:battery_charge', power)
	E.battery.update(s)
end)

E.dynamo = { kind = {} }

E.dynamo.drawCurrent = accessor('dynamo', function(s,c,m, power, time, test)
	return c.vtable.drawCurrent(s, power, time, test)
end)
................................................................................
				{ title = 'Size', affinity = 'info';
					desc = lib.math.si('m', def.fab.size.print) };
			};
		};
		_starsoul = {
			event = {
				create = function(st, how)
					--[[if not how.gift then -- cheap hack to make starting batteries fully charged
						E.battery.setCharge(st, 0)
					end]]
					E.battery.update(st)
				end;
			};
			fab = def.fab;
			dynamo = {
				vtable = E.dynamo.kind.battery;
			};
			battery = def;
................................................................................
	end;
	__index = {
		read = function(self)
			local dat = E.chip.read(self.chip)
			return dat.files[self.inode]
		end;
		write = function(self,data)
			-- print('writing', self.chip, self.inode)
			return E.chip.fileWrite(self.chip, self.inode, data)
		end;
		erase = function(self)
			local dat = E.chip.read(self.chip)
			table.remove(dat.files, self.inode)
			E.chip.write(self.chip, dat)
			self.inode = nil