Cleans up train latching/unlatching

Also adds a verb for turning off train engines.

Conflicts:
	code/modules/vehicles/cargo_train.dm
	code/modules/vehicles/train.dm
This commit is contained in:
mwerezak
2014-06-21 13:09:50 -04:00
committed by ZomgPonies
parent 7b4ef8f0f9
commit 7f425ecf9b
2 changed files with 87 additions and 123 deletions
+32 -65
View File
@@ -12,7 +12,6 @@
load_offset_y = 5
var/car_limit = 3 //how many cars an engine can pull before performance degrades
var/lead_engine = 1 //if the engine is the lead engine - set automatically
active_engines = 1
/obj/vehicle/train/cargo/trolley
@@ -43,9 +42,10 @@
if(on && cell.charge < power_use)
turn_off()
update_stats()
if(load && lead_engine)
if(load && is_train_head())
load << "The drive motor briefly whines, then crawls to a stop."
if(lead_engine && !on)
if(is_train_head() && !on)
return 0
return ..()
@@ -108,7 +108,7 @@
/obj/vehicle/train/cargo/engine/RunOver(var/mob/living/carbon/human/H)
..()
if(lead_engine && istype(load, /mob/living/carbon/human))
if(is_train_head() && istype(load, /mob/living/carbon/human))
var/mob/living/carbon/human/D = load
D << "\red \b You ran over [H]!"
visible_message("<B>\red \The [src] ran over [H]!</B>")
@@ -158,7 +158,7 @@
if(user != load)
return 0
if(lead_engine)
if(is_train_head())
if(direction == reverse_direction(dir))
return 0
if(Move(get_step(src, direction)))
@@ -177,73 +177,42 @@
usr << "The engine is already running."
return
if(turn_on())
usr << "You start [src]."
turn_on()
if (on)
usr << "You start [src]'s engine."
else
if(cell.charge < power_use)
usr << "[src] is out of power."
else
usr << "[src] won't start."
usr << "[src]'s engine won't start."
/obj/vehicle/train/cargo/engine/verb/climb_down(mob/user as mob)
set name = "Exit vehicle"
/obj/vehicle/train/cargo/engine/verb/stop_engine()
set name = "Stop engine"
set category = "Object"
set src in range(0)
if(!load)
set src in view(1)
if(!on)
usr << "The engine is already stopped."
return
if(user != load)
return
unload(user)
turn_off()
if (!on)
usr << "You stop [src]'s engine."
//-------------------------------------------
// Latching/unlatching procs
//-------------------------------------------
/obj/vehicle/train/cargo/trolley/latch(var/obj/vehicle/train/T)
if(..())
//if this is a trolley, and is now part of a train, anchor it so it cant be pushed around
if(lead)
anchored = 1
lead.anchored = 1
if(tow)
anchored = 1
tow.anchored = 1
return 1
else
return 0
/obj/vehicle/train/cargo/trolley/unlatch()
if(..())
//if this carraige isn't part of a train anymore; unanchor it so it can be pushed around
if(!tow && !lead)
anchored = 0
return 1
else
return 0
/obj/vehicle/train/cargo/engine/latch(var/obj/vehicle/train/T)
if(..())
//check if this is not the lead engine
if(lead)
lead_engine = 0
if(tow)
tow.anchored = 1
return 1
else
return 0
/obj/vehicle/train/cargo/engine/unlatch()
if(..())
//check if this is now the lead engine
if(!lead)
lead_engine = 1
return 1
else
return 0
/obj/vehicle/train/cargo/trolley/attach_to(obj/vehicle/train/T, mob/user)
..()
if (lead)
//This is now part of a train, anchor it so it cant be pushed around
anchored = 1
/obj/vehicle/train/cargo/trolley/unattach(mob/user)
..()
if (!lead && !tow)
//if this carriage isn't part of a train anymore; unanchor it so it can be pushed around
anchored = 0
//-------------------------------------------
// Loading/unloading procs
@@ -277,13 +246,11 @@
/obj/vehicle/train/cargo/engine/update_train_stats()
..()
speed_calc()
update_move_delay()
if(!lead) //check if this is the lead engine
lead_engine = 1
/obj/vehicle/train/cargo/engine/proc/speed_calc()
if(!lead_engine)
/obj/vehicle/train/cargo/engine/proc/update_move_delay()
if(!is_train_head())
move_delay = initial(move_delay) //so that engines that have been turned off don't lag behind
else
move_delay = max(0, (-car_limit * active_engines) + train_length - active_engines) //limits base overweight so you cant overspeed trains
+55 -58
View File
@@ -56,6 +56,7 @@
msg_admin_attack("[D.name] ([D.ckey]) hit [M.name] ([M.ckey]) with [src]. (<A HREF='?_src_=holder;adminplayerobservecoodjump=1;X=[src.x];Y=[src.y];Z=[src.z]'>JMP</a>)")
//-------------------------------------------
// Interaction procs
//-------------------------------------------
@@ -82,14 +83,10 @@
return
if(istype(C,/obj/vehicle/train))
if(latch(C))
user << "\blue You successfully connect the [C] to [src]."
else
user << "\red You were unable to connect the [C] to [src]."
return
if(!load(C))
user << "\red You were unable to load [C] on [src]."
latch(C)
else
if(!load(C))
user << "\red You were unable to load [C] on [src]."
/obj/vehicle/train/attack_hand(mob/user as mob)
if(!user.canmove || user.stat || user.restrained() || !Adjacent(user))
@@ -106,22 +103,53 @@
/obj/vehicle/train/verb/unlatch_v()
set name = "Unlatch"
set desc = "Unhitches this train from the one in front of it."
set category = "Object"
set src in view(1)
if(!usr.canmove || usr.stat || usr.restrained() || !Adjacent(usr))
return
if(unlatch())
usr << "\blue You unlatch [src]."
else
usr << "\red [src] is already unlatched."
unattach(usr)
//-------------------------------------------
// Latching/unlatching procs
//-------------------------------------------
/obj/vehicle/train/proc/latch(var/obj/vehicle/train/T)
//attempts to attach src as a follower of the train T
/obj/vehicle/train/proc/attach_to(obj/vehicle/train/T, mob/user)
if (get_dist(src, T) > 1)
user << "\red [src] is too far away from [T] to hitch them together."
return
if (lead)
user << "\red [src] is already hitched to something."
return
if (T.tow)
user << "\red [T] is already towing something."
return
//latch with src as the follower
lead = T
T.tow = src
update_stats()
//detaches the train from whatever is towing it
/obj/vehicle/train/proc/unattach(mob/user)
if (!lead)
user << "\red [src] is not hitched to anything."
return
lead.tow = null
lead.update_stats()
lead = null
/obj/vehicle/train/proc/latch(mob/user, obj/vehicle/train/T)
if(!istype(T) || !Adjacent(T))
return 0
@@ -130,50 +158,19 @@
return 0
*/
var/T_dir = get_dir(src, T)
var/T_dir = get_dir(src, T) //figure out where T is wrt src
if(dir & T_dir) //if car is ahead
if(!lead && !T.tow)
lead = T
T.tow = src
else
return 0
else if(reverse_direction(dir) & T_dir) //else if car is behind
if(!tow && !T.lead)
tow = T
T.lead = src
else
return 0
else
if(dir == T_dir) //if car is ahead
src.attach_to(T, user)
else if(reverse_direction(dir) == T_dir) //else if car is behind
T.attach_to(src, user)
//returns 1 if this is the lead car of the train
/obj/vehicle/train/proc/is_train_head()
if (lead)
return 0
update_stats()
return 1
/obj/vehicle/train/proc/unlatch(var/obj/vehicle/train/T)
if(!lead && !tow)
return 0
if(T)
if(T == tow)
tow = null
else if(T == lead)
lead = null
else
if(tow)
tow.unlatch(src)
tow = null
if(lead)
lead.unlatch(src)
lead = null
update_stats()
return 1
//-------------------------------------------------------
// Stat update procs
//
@@ -182,10 +179,10 @@
// size of the train, to limit super long trains.
//-------------------------------------------------------
/obj/vehicle/train/update_stats()
if(!tow)
update_train_stats()
if(tow)
return tow.update_stats() //take us to the very end
else
return tow.update_stats()
update_train_stats() //we're at the end
/obj/vehicle/train/proc/update_train_stats()
if(powered && on)
@@ -195,10 +192,10 @@
train_length = 1
if(tow)
if(istype(tow))
active_engines += tow.active_engines
train_length += tow.train_length
//update the next section of train ahead of us
if(lead)
if(istype(lead))
lead.update_train_stats()