Ports more of /tg/'s powernet code.

Among other things this fixes a a bug where SMES would draw excess power from the powergrid they were outputting to, rather than drawing from.
This commit is contained in:
PsiOmega
2014-10-29 14:01:35 +01:00
parent c8e27555b9
commit a1c19b78be
14 changed files with 204 additions and 278 deletions
+27 -27
View File
@@ -23,9 +23,9 @@
name = "power cell rack PSU"
desc = "A rack of power cells working as a PSU."
charge = 0 //you dont really want to make a potato PSU which already is overloaded
online = 0
chargelevel = 0
output = 0
output_attempt = 0
input_level = 0
output_level = 0
input_level_max = 0
output_level_max = 0
icon_state = "gsmes"
@@ -37,9 +37,9 @@
/obj/machinery/power/smes/batteryrack/substation
name = "Substation PSU"
desc = "A rack of power cells working as a PSU. This one seems to be equipped for higher power loads."
output = 150000
chargelevel = 150000
online = 1
output_level = 150000
input_level = 150000
output_attempt = 1
// One high capacity cell, two regular cells. Lots of room for engineer upgrades
// Also five basic capacitors. Again, upgradeable.
@@ -89,13 +89,13 @@
capacity = C * 40 //Basic cells are such crap. Hyper cells needed to get on normal SMES levels.
/obj/machinery/power/smes/batteryrack/updateicon()
/obj/machinery/power/smes/batteryrack/update_icon()
overlays.Cut()
if(stat & BROKEN) return
if (online)
if (output_attempt)
overlays += image('icons/obj/power.dmi', "gsmes_outputting")
if(charging)
if(inputting)
overlays += image('icons/obj/power.dmi', "gsmes_charging")
var/clevel = chargedisplay()
@@ -113,7 +113,7 @@
if(open_hatch)
if(istype(W, /obj/item/weapon/crowbar))
if (charge < (capacity / 100))
if (!online && !chargemode)
if (!output_attempt && !input_attempt)
playsound(get_turf(src), 'sound/items/Crowbar.ogg', 50, 1)
var/obj/machinery/constructable_frame/machine_frame/M = new /obj/machinery/constructable_frame/machine_frame(src.loc)
M.state = 2
@@ -130,7 +130,7 @@
user << "<span class='warning'>Better let [src] discharge before dismantling it.</span>"
else if ((istype(W, /obj/item/weapon/stock_parts/capacitor) && (capacitors_amount < 5)) || (istype(W, /obj/item/weapon/cell) && (cells_amount < 5)))
if (charge < (capacity / 100))
if (!online && !chargemode)
if (!output_attempt && !input_attempt)
user.drop_item()
component_parts += W
W.loc = src
@@ -163,13 +163,13 @@
return
/obj/machinery/power/smes/batteryrack/makeshift/updateicon()
/obj/machinery/power/smes/batteryrack/makeshift/update_icon()
overlays.Cut()
if(stat & BROKEN) return
if (online)
if (output_attempt)
overlays += image('icons/obj/power.dmi', "gsmes_outputting")
if(charging)
if(inputting)
overlays += image('icons/obj/power.dmi', "gsmes_charging")
if (overcharge_percent > 100)
overlays += image('icons/obj/power.dmi', "gsmes_overcharge")
@@ -234,35 +234,35 @@
//store machine state to see if we need to update the icon overlays
var/last_disp = chargedisplay()
var/last_chrg = charging
var/last_onln = online
var/last_chrg = inputting
var/last_onln = output_attempt
var/last_overcharge = overcharge_percent
if(terminal)
if(chargemode)
var/target_load = min((capacity-charge)/SMESRATE, chargelevel) // charge at set rate, limited to spare capacity
if(input_attempt)
var/target_load = min((capacity-charge)/SMESRATE, input_level) // charge at set rate, limited to spare capacity
var/actual_load = draw_power(target_load) // add the load to the terminal side network
charge += actual_load * SMESRATE // increase the charge
if (actual_load >= target_load) // did the powernet have enough power available for us?
charging = 1
inputting = 1
else
charging = 0
inputting = 0
if(online) // if outputting
lastout = min( charge/SMESRATE, output) //limit output to that stored
charge -= lastout*SMESRATE // reduce the storage (may be recovered in /restore() if excessive)
add_avail(lastout) // add output to powernet (smes side)
if(output_attempt) // if outputting
output_used = min( charge/SMESRATE, output_level) //limit output to that stored
charge -= output_used*SMESRATE // reduce the storage (may be recovered in /restore() if excessive)
add_avail(output_used) // add output to powernet (smes side)
if(charge < 0.0001)
online = 0 // stop output if charge falls to zero
outputting(0) // stop output if charge falls to zero
overcharge_percent = round((charge / capacity) * 100)
if (overcharge_percent > 115) //115% is the minimum overcharge for anything to happen
overcharge_consequences()
// only update icon if state changed
if(last_disp != chargedisplay() || last_chrg != charging || last_onln != online || ((overcharge_percent > 100) ^ (last_overcharge > 100)))
updateicon()
if(last_disp != chargedisplay() || last_chrg != inputting || last_onln != output_attempt || ((overcharge_percent > 100) ^ (last_overcharge > 100)))
update_icon()
return
#undef SMESRATE