Differences From
Artifact [e59f2eda8d]:
70 70 E.schematicGroups.link('starsoul_electronics:battery', {
71 71 title = 'Battery', icon = 'starsoul-item-battery.png';
72 72 description = 'Portable power storage cells are essential to all aspects of survival';
73 73 })
74 74
75 75 E.schematicGroups.link('starsoul_electronics:decayCell', {
76 76 title = 'Decay Cell', icon = 'starsoul-item-decaycell.png';
77 - 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.";
77 + 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.";
78 78 })
79 79
80 80
81 81 -------------------------
82 82 -- batteries & dynamos --
83 83 -------------------------
84 84
................................................................................
96 96 return fn(stack,
97 97 stack:get_definition()._starsoul[ty],
98 98 stack:get_meta(), ...)
99 99 end
100 100 end
101 101
102 102 -- return a wear level that won't destroy the item
103 -local function safeWear(fac) return math.min(math.max(fac,0),1) * 0xFFFE end
104 -local function safeWearToFac(w) return w/0xFFFE end
103 +-- local function safeWear(fac) return math.min(math.max(fac,0),1) * 0xFFFE end
104 +-- local function safeWearToFac(w) return w/0xFFFE end
105 +
106 +E.battery.update = accessor('battery', function(stack, batClass, meta)
107 + -- local cap = E.battery.capacity(stack)
108 + local charge = meta:get_float 'starsoul_electronics:battery_charge'
109 + meta:set_string('count_meta', string.format('%s%%', math.floor(charge * 100)))
110 + meta:set_int('count_alignment', bit.lshift(3, 2) + 2)
111 +end)
105 112
106 113 -- E.battery.capacity(bat) --> charge (J)
107 114 E.battery.capacity = accessor('battery', function(stack, batClass, meta)
108 - local dmg = meta:get_int('starsoul_electronics:battery_degrade') -- µJ/μW
115 + local dmg = meta:get_int 'starsoul_electronics:battery_degrade' -- µJ/μW
109 116 local dmg_J = dmg / 1000
110 117 return (batClass.capacity - dmg_J)
111 118 end)
112 119
113 120 -- E.battery.charge(bat) --> charge (J)
114 121 E.battery.charge = accessor('battery', function(stack, batClass, meta)
115 - local fac = 1 - safeWearToFac(stack:get_wear())
122 + local fac = meta:get_float 'starsoul_electronics:battery_charge'
123 + -- local fac = 1 - safeWearToFac(stack:get_wear())
116 124 return E.battery.capacity(stack) * fac
117 125 end)
118 126
119 127 -- E.battery.dischargeRate(bat) --> dischargeRate (W)
120 128 E.battery.dischargeRate = accessor('battery', function(stack, batClass, meta)
121 - local dmg = meta:get_int('starsoul_electronics:battery_degrade') -- µJ/μW
129 + local dmg = meta:get_int 'starsoul_electronics:battery_degrade' -- µJ/μW
122 130 local dmg_W = dmg / 1000
123 131 return batClass.dischargeRate - dmg_W
124 132 end);
125 133
126 134
127 135 -- E.battery.drawCurrent(bat, power, time, test) --> supply (J), wasteHeat (J)
128 136 -- bat = battery stack
................................................................................
138 146
139 147 if not test then
140 148 local degrade = m:get_int 'starsoul_electronics:battery_degrade' or 0
141 149 degrade = degrade + maxPower * bc.decay
142 150 -- for each joule of power drawn, capacity degrades by `decay` J
143 151 -- this should ordinarily be on the order of mJ or smaller
144 152 m:set_int('starsoul_electronics:battery_degrade', degrade)
145 - s:set_wear(safeWear(1 - (ch / E.battery.capacity(s))))
153 + -- s:set_wear(safeWear(1 - (ch / E.battery.capacity(s))))
154 + m:set_float('starsoul_electronics:battery_charge', ch / E.battery.capacity(s))
155 + E.battery.update(s)
146 156 end
147 157
148 158 return maxPower, 0 -- FIXME specify waste heat
149 159 end)
150 160
151 161 -- E.battery.recharge(bat, power, time) --> draw (J)
152 162 -- bat = battery stack
................................................................................
154 164 -- time s = the amount of time available for this transaction
155 165 -- draw J = how much power was actually drawn in $time seconds
156 166 E.battery.recharge = accessor('battery', function(s, bc, m, power, time)
157 167 local ch = E.battery.charge(s)
158 168 local cap = E.battery.capacity(s)
159 169 local maxPower = math.min(E.battery.dischargeRate(s)*time, power)
160 170 local total = math.min(ch + maxPower, cap)
161 - s:set_wear(safeWear(1 - (total/cap)))
171 + -- s:set_wear(safeWear(1 - (total/cap)))
172 + m:set_float('starsoul_electronics:battery_charge', total/cap)
173 + E.battery.update(s)
162 174 return maxPower, 0 -- FIXME
163 175 end)
164 176
165 177 E.battery.setCharge = accessor('battery', function(s, bc, m, newPower)
166 178 local cap = E.battery.capacity(s)
167 179 local power = math.min(cap, newPower)
168 - s:set_wear(safeWear(1 - (power/cap)))
180 + -- s:set_wear(safeWear(1 - (power/cap)))
181 + m:set_float('starsoul_electronics:battery_charge', power/cap)
182 + E.battery.update(s)
183 +end)
184 +E.battery.setChargeF = accessor('battery', function(s, bc, m, newPowerF)
185 + local power = math.min(1.0, newPowerF)
186 + m:set_float('starsoul_electronics:battery_charge', power)
187 + E.battery.update(s)
169 188 end)
170 189
171 190 E.dynamo = { kind = {} }
172 191
173 192 E.dynamo.drawCurrent = accessor('dynamo', function(s,c,m, power, time, test)
174 193 return c.vtable.drawCurrent(s, power, time, test)
175 194 end)
................................................................................
207 226 { title = 'Size', affinity = 'info';
208 227 desc = lib.math.si('m', def.fab.size.print) };
209 228 };
210 229 };
211 230 _starsoul = {
212 231 event = {
213 232 create = function(st, how)
214 - if not how.gift then -- cheap hack to make starting batteries fully charged
233 + --[[if not how.gift then -- cheap hack to make starting batteries fully charged
215 234 E.battery.setCharge(st, 0)
216 - end
235 + end]]
236 + E.battery.update(st)
217 237 end;
218 238 };
219 239 fab = def.fab;
220 240 dynamo = {
221 241 vtable = E.dynamo.kind.battery;
222 242 };
223 243 battery = def;
................................................................................
832 852 end;
833 853 __index = {
834 854 read = function(self)
835 855 local dat = E.chip.read(self.chip)
836 856 return dat.files[self.inode]
837 857 end;
838 858 write = function(self,data)
839 - print('writing', self.chip, self.inode)
859 + -- print('writing', self.chip, self.inode)
840 860 return E.chip.fileWrite(self.chip, self.inode, data)
841 861 end;
842 862 erase = function(self)
843 863 local dat = E.chip.read(self.chip)
844 864 table.remove(dat.files, self.inode)
845 865 E.chip.write(self.chip, dat)
846 866 self.inode = nil