-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFlagBase.as
227 lines (186 loc) · 5.29 KB
/
FlagBase.as
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
// Flag base logic
#include "CTF_FlagCommon.as"
#include "GameplayEvents.as";
const string flag_return = "flag return";
const string flag_name = "ctf_flag";
void onInit(CBlob@ this)
{
if (getNet().isServer())
{
CBlob@ flag = server_CreateBlob(flag_name, this.getTeamNum(), this.getPosition());
if (flag !is null)
{
this.server_AttachTo(flag, "FLAG");
this.set_u16("flag id", flag.getNetworkID());
flag.set_u16("base_id", this.getNetworkID());
this.Sync("flag_id", true);
}
}
//cannot fall out of map
this.SetMapEdgeFlags(u8(CBlob::map_collide_up) |
u8(CBlob::map_collide_down) |
u8(CBlob::map_collide_sides));
//we actually have our own way of ignoring damage
//but this is important for a lot of other scripts
this.Tag("invincible");
this.addCommandID(flag_return);
}
void onTick(CBlob@ this)
{
if (getNet().isServer())
{
if (this.getTickSinceCreated() > 30)
{
if (!this.hasTag("nobuild sector added"))
{
this.Tag("nobuild sector added");
Vec2f pos = this.getPosition();
CMap@ map = this.getMap();
map.server_AddSector(pos + Vec2f(-12, -32), pos + Vec2f(12, 16), "no build", "", this.getNetworkID());
//clear the no build zone so we dont get unbreakable blocks
for (int x = -12; x < 12; x += 8)
{
for (int y = -32; y < 8; y += 8)
{
map.server_SetTile(pos + Vec2f(x, y), CMap::tile_empty);
}
}
map.server_SetTile(pos + Vec2f(-8, 12), CMap::tile_bedrock);
map.server_SetTile(pos + Vec2f(0, 12), CMap::tile_bedrock);
map.server_SetTile(pos + Vec2f(8, 12), CMap::tile_bedrock);
this.set_Vec2f("stick position", this.getPosition());
}
else
{
this.setPosition(this.get_Vec2f("stick position"));
this.setVelocity(Vec2f());
}
}
if (!this.hasAttached())
{
if (!this.hasTag("flag missing"))
{
this.Tag("flag missing");
this.Sync("flag missing", true);
}
u16 id = this.get_u16("flag id");
CBlob@ b = getBlobByNetworkID(id);
if (b !is null)
{
//check return conditions
if (b.hasTag("capture_return"))
{
b.server_DetachAll();
this.SendCommand(this.getCommandID(flag_return));
this.server_AttachTo(b, "FLAG");
b.SetFacingLeft(this.isFacingLeft());
}
if (!b.isAttached() && b.hasTag("return"))
{
returnFlag(this, b, "return");
}
if (b.hasTag("stalemate_return"))
{
returnFlag(this, b, "stalemate_return");
}
}
else
{
this.Tag("flag captured");
//Vec2f pos = this.getPosition();
//this.getMap().RemoveSectorsAtPosition(pos, "no build", this.getNetworkID());
//this.getMap().server_AddSector(pos + Vec2f(-12, -8), pos + Vec2f(12, 16), "no build", "", this.getNetworkID());
//this.server_Die();
}
}
else
{
if (this.hasTag("flag missing"))
{
this.Untag("flag missing");
this.Sync("flag missing", true);
}
}
}
}
void returnFlag(CBlob@ this, CBlob@ flag, string tag)
{
flag.server_DetachAll();
this.SendCommand(this.getCommandID(flag_return));
flag.Untag(tag); //local
this.server_AttachTo(flag, "FLAG");
flag.SetFacingLeft(this.isFacingLeft());
}
void onCommand(CBlob@ this, u8 cmd, CBitStream @params)
{
if (cmd == this.getCommandID(flag_return))
{
CBitStream params;
params.write_Vec2f(this.getPosition());
u16 id = this.get_u16("flag id");
CBlob@ b = getBlobByNetworkID(id);
if (b !is null)
{
if (getNet().isServer())
{
b.SendCommand(b.getCommandID("return"), params);
}
b.Untag("return");
}
}
}
f32 onHit(CBlob@ this, Vec2f worldPoint, Vec2f velocity, f32 damage, CBlob@ hitterBlob, u8 customData)
{
//ignore all damage
return 0.0f;
}
//sprite
void onInit(CSprite@ this)
{
this.SetZ(-10.0f);
}
//release held flag when touched
void onCollision(CBlob@ this, CBlob@ blob, bool solid)
{
if (blob is null || !getNet().isServer()) return;
if (!blob.hasTag("player")) return; //early out for non-player collision
if (!this.hasAttached()) return; //early out if we dont have a flag attached
if (blob.getTeamNum() != this.getTeamNum())
{
if (canPickupFlag(blob))
{
this.server_DetachAll();
u16 id = this.get_u16("flag id");
CBlob@ b = getBlobByNetworkID(id);
if (b !is null)
{
blob.server_AttachTo(b, "PICKUP"); //attach to player
CBitStream params;
params.write_u16(blob.getNetworkID());
b.SendCommand(b.getCommandID("pickup"), params);
}
}
}
else //our team
{
CBlob@ b = blob.getCarriedBlob();
//carrying enemy flag
if (b !is null && b.getName() == flag_name && b.getTeamNum() != this.getTeamNum())
{
SendGameplayEvent(createFlagCaptureEvent(blob.getPlayer()));
//smash the flag
//this.server_Hit(b, b.getPosition(), Vec2f(), 5.0f, 0xfa, true);
b.Tag("capture_return");
if (sv_tcpr)
{
if (blob !is null && blob.getPlayer() !is null)
{
tcpr("FlagCaptured {\"player\":\"" + blob.getPlayer().getUsername() + "\",\"ticks\":" + getGameTime() + "}");
}
}
CBitStream params;
params.write_u16(blob.getNetworkID());
b.SendCommand(b.getCommandID("capture"), params);
}
}
}