@@ -77,7 +77,20 @@ private void OnDoAfter(EntityUid uid, DefibrillatorComponent component, Defibril
77
77
Zap ( uid , target , args . User , component ) ;
78
78
}
79
79
80
- public bool CanZap ( EntityUid uid , EntityUid target , EntityUid ? user = null , DefibrillatorComponent ? component = null )
80
+ /// <summary>
81
+ /// Checks if you can actually defib a target.
82
+ /// </summary>
83
+ /// <param name="uid">Uid of the defib</param>
84
+ /// <param name="target">Uid of the target getting defibbed</param>
85
+ /// <param name="user">Uid of the entity using the defibrillator</param>
86
+ /// <param name="component">Defib component</param>
87
+ /// <param name="targetCanBeAlive">
88
+ /// If true, the target can be alive. If false, the function will check if the target is alive and will return false if they are.
89
+ /// </param>
90
+ /// <returns>
91
+ /// Returns true if the target is valid to be defibed, false otherwise.
92
+ /// </returns>
93
+ public bool CanZap ( EntityUid uid , EntityUid target , EntityUid ? user = null , DefibrillatorComponent ? component = null , bool targetCanBeAlive = false )
81
94
{
82
95
if ( ! Resolve ( uid , ref component ) )
83
96
return false ;
@@ -98,15 +111,25 @@ public bool CanZap(EntityUid uid, EntityUid target, EntityUid? user = null, Defi
98
111
if ( ! _powerCell . HasActivatableCharge ( uid , user : user ) )
99
112
return false ;
100
113
101
- if ( _mobState . IsAlive ( target , mobState ) )
114
+ if ( ! targetCanBeAlive && _mobState . IsAlive ( target , mobState ) )
102
115
return false ;
103
116
104
- if ( ! component . CanDefibCrit && _mobState . IsCritical ( target , mobState ) )
117
+ if ( ! targetCanBeAlive && ! component . CanDefibCrit && _mobState . IsCritical ( target , mobState ) )
105
118
return false ;
106
119
107
120
return true ;
108
121
}
109
122
123
+ /// <summary>
124
+ /// Tries to start defibrillating the target. If the target is valid, will start the defib do-after.
125
+ /// </summary>
126
+ /// <param name="uid">Uid of the defib</param>
127
+ /// <param name="target">Uid of the target getting defibbed</param>
128
+ /// <param name="user">Uid of the entity using the defibrillator</param>
129
+ /// <param name="component">Defib component</param>
130
+ /// <returns>
131
+ /// Returns true if the defibrillation do-after started, otherwise false.
132
+ /// </returns>
110
133
public bool TryStartZap ( EntityUid uid , EntityUid target , EntityUid user , DefibrillatorComponent ? component = null )
111
134
{
112
135
if ( ! Resolve ( uid , ref component ) )
@@ -118,25 +141,42 @@ public bool TryStartZap(EntityUid uid, EntityUid target, EntityUid user, Defibri
118
141
_audio . PlayPvs ( component . ChargeSound , uid ) ;
119
142
return _doAfter . TryStartDoAfter ( new DoAfterArgs ( EntityManager , user , component . DoAfterDuration , new DefibrillatorZapDoAfterEvent ( ) ,
120
143
uid , target , uid )
121
- {
122
- NeedHand = true ,
123
- BreakOnMove = ! component . AllowDoAfterMovement
124
- } ) ;
144
+ {
145
+ NeedHand = true ,
146
+ BreakOnMove = ! component . AllowDoAfterMovement
147
+ } ) ;
125
148
}
126
149
127
- public void Zap ( EntityUid uid , EntityUid target , EntityUid user , DefibrillatorComponent ? component = null , MobStateComponent ? mob = null , MobThresholdsComponent ? thresholds = null )
150
+ /// <summary>
151
+ /// Tries to defibrillate the target with the given defibrillator.
152
+ /// </summary>
153
+ public void Zap ( EntityUid uid , EntityUid target , EntityUid user , DefibrillatorComponent ? component = null )
128
154
{
129
- if ( ! Resolve ( uid , ref component ) || ! Resolve ( target , ref mob , ref thresholds , false ) )
155
+ if ( ! Resolve ( uid , ref component ) )
130
156
return ;
131
157
132
- // clowns zap themselves
133
- if ( HasComp < ClumsyComponent > ( user ) & & user != target )
134
- {
135
- Zap ( uid , user , user , component ) ;
158
+ if ( ! _powerCell . TryUseActivatableCharge ( uid , user : user ) )
136
159
return ;
137
- }
138
160
139
- if ( ! _powerCell . TryUseActivatableCharge ( uid , user : user ) )
161
+ var selfEvent = new SelfBeforeDefibrillatorZapsEvent ( user , uid , target ) ;
162
+ RaiseLocalEvent ( user , selfEvent ) ;
163
+
164
+ target = selfEvent . DefibTarget ;
165
+
166
+ // Ensure thet new target is still valid.
167
+ if ( selfEvent . Cancelled || ! CanZap ( uid , target , user , component , true ) )
168
+ return ;
169
+
170
+ var targetEvent = new TargetBeforeDefibrillatorZapsEvent ( user , uid , target ) ;
171
+ RaiseLocalEvent ( target , targetEvent ) ;
172
+
173
+ target = targetEvent . DefibTarget ;
174
+
175
+ if ( targetEvent . Cancelled || ! CanZap ( uid , target , user , component , true ) )
176
+ return ;
177
+
178
+ if ( ! TryComp < MobStateComponent > ( target , out var mob ) ||
179
+ ! TryComp < MobThresholdsComponent > ( target , out var thresholds ) )
140
180
return ;
141
181
142
182
_audio . PlayPvs ( component . ZapSound , uid ) ;
0 commit comments