-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathstability.as
More file actions
196 lines (169 loc) · 5 KB
/
stability.as
File metadata and controls
196 lines (169 loc) · 5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
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
195
196
bool g_running_stability = false;
int numSkip = 0;
int numChecks = 0;
dictionary visited_parts; // used to mark positions as already visited when doing the stability search
array<EHandle> stability_ents; // list of ents to check for stability
void checkStabilityEnt(EHandle ent)
{
if (!ent)
return;
for (uint i = 0; i < stability_ents.length(); i++)
{
if (stability_ents[i] and stability_ents[i].GetEntity().entindex() == ent.GetEntity().entindex())
return;
}
stability_ents.insertLast(ent);
}
void debug_stability(Vector start, Vector end)
{
if (getPartAtPos(end) !is null)
te_beampoints(start, end, "sprites/laserbeam.spr", 0, 100, 255,1,0,GREEN);
else
te_beampoints(start, end, "sprites/laserbeam.spr", 0, 100, 255,1,0,Color(255, 0, 0, 0));
}
bool searchFromPart(func_breakable_custom@ part)
{
if (visited_parts.exists(part.entindex()))
{
numSkip++;
return false;
}
visited_parts[part.entindex()] = true;
numChecks++;
if (part.pev.colormap == B_FOUNDATION or part.pev.colormap == B_FOUNDATION_TRI) {
return true;
}
for (uint i = 0; i < part.connections.length(); i++)
{
if (part.connections[i])
{
if (searchFromPart(cast<func_breakable_custom@>(CastToScriptClass(part.connections[i].GetEntity()))))
return true;
}
}
return false;
}
void part_broken(CBaseEntity@ pActivator, CBaseEntity@ pCaller, USE_TYPE useType, float flValue)
{
if (pCaller.pev.effects & EF_NODRAW == 0)
{
PlayerState@ state = getPlayerStateBySteamID(pCaller.pev.noise1, pCaller.pev.noise2);
if (state !is null)
state.partDestroyed(pCaller);
}
propogate_part_destruction(pCaller);
}
void propogate_part_destruction(CBaseEntity@ ent)
{
int type = ent.pev.colormap;
int socket = socketType(type);
if (ent.pev.classname == "func_breakable_custom")
{
func_breakable_custom@ bpart = cast<func_breakable_custom@>(CastToScriptClass(ent));
for (uint i = 0; i < bpart.connections.length(); i++)
checkStabilityEnt(bpart.connections[i]);
}
// destroy objects parented to this one
array<EHandle> children = getPartsByParent(ent.pev.weapons);
for (uint i = 0; i < children.length(); i++)
{
CBaseEntity@ child = children[i];
if (child.entindex() == ent.entindex())
continue;
child.TakeDamage(child.pev, child.pev, 9e99, 0);
if (child.pev.classname == "func_ladder")
{
g_EntityFuncs.Remove(child);
}
}
if (type == B_LADDER_HATCH or type == B_LADDER or type == B_WOOD_SHUTTERS)
{
// kill tied entities (ladder, secondary door)
array<EHandle> parents = getPartsByID(ent.pev.weapons);
for (uint i = 0; i < parents.length(); i++)
{
CBaseEntity@ parent = parents[i].GetEntity();
parent.TakeDamage(parent.pev, parent.pev, parent.pev.health, 0);
if (parent.pev.classname == "func_ladder")
{
g_EntityFuncs.Remove(parent);
}
}
}
if (!g_running_stability)
stabilityCheck();
}
void stabilityCheck()
{
g_running_stability = true;
int numIter = 0;
while(stability_ents.length() > 0)
{
visited_parts.deleteAll();
CBaseEntity@ src_part = stability_ents[0];
if (src_part is null)
{
stability_ents.removeAt(0);
continue;
}
if (src_part.pev.classname != "func_breakable_custom")
{
println("stabilityCheck: Not a support part!");
stability_ents.removeAt(0);
continue;
}
func_breakable_custom@ bpart = cast<func_breakable_custom@>(CastToScriptClass(src_part));
int type = src_part.pev.colormap;
int socket = socketType(type);
Vector pos = src_part.pev.origin;
if (src_part.pev.colormap == B_FOUNDATION or socket == SOCKET_HIGH_WALL or src_part.pev.colormap == B_FOUNDATION_TRI)
{
stability_ents.removeAt(0);
continue;
}
// try to find a connected path to a foundation
// otherwise break the part
numChecks = 0;
numSkip = 0;
bool supported = searchFromPart(bpart);
//println("Stability for part " + src_part.pev.weapons + " finished in " + numChecks + " checks (" + numSkip + " skipped). Result is " + supported);
if (!supported) {
propogate_part_destruction(src_part);
src_part.TakeDamage(src_part.pev, src_part.pev, src_part.pev.health, 0);
if (src_part.pev.classname == "func_ladder")
{
g_EntityFuncs.Remove(src_part);
}
}
stability_ents.removeAt(0);
break;
}
if (stability_ents.length() > 0)
g_Scheduler.SetTimeout("stabilityCheck", 0.05);
else
{
g_Scheduler.SetTimeout("deleteNullBuildEnts", 0.05);
g_running_stability = false;
}
}
void deleteNullBuildEnts()
{
// check for destroyed ents
for (uint i = 0; i < g_build_parts.length(); i++)
{
func_breakable_custom@ ent = cast<func_breakable_custom@>(CastToScriptClass(g_build_parts[i].GetEntity()));
if (ent is null)
{
g_build_parts.removeAt(i);
i--;
}
else
{
if (ent.pev.weapons != ent.id)
{
println("stabilityCheck: Bad ID! " + ent.id + " != " + ent.pev.weapons);
ent.pev.weapons = ent.id;
}
}
}
}