Repository navigation
Expand file tree
/
Copy pathCTFGame.cs
More file actions
249 lines (202 loc) · 8.03 KB
/
Copy pathCTFGame.cs
File metadata and controls
249 lines (202 loc) · 8.03 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
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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
/*
Copyright 2010 MCSharp team (Modified for use with MCZall/MCLawl/MCForge)
Dual-licensed under the Educational Community License, Version 2.0 and
the GNU General Public License, Version 3 (the "Licenses"); you may
not use this file except in compliance with the Licenses. You may
obtain a copy of the Licenses at
http://www.osedu.org/licenses/ECL-2.0
http://www.gnu.org/licenses/gpl-3.0.html
Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an "AS IS"
BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
or implied. See the Licenses for the specific language governing
permissions and limitations under the Licenses.
*/
using System;
using System.Collections.Generic;
using System.Threading;
/* CTF TO-DO LIST
* Team-chat
* Delayed respawns. Dunno, put player somewhere in the mid-term? Put them out of the map? I dunno.
* Fix the java client crash on RemoveMember that rarely but consistantly occurs.
*/
namespace MCForge
{
public class CTFGame
{
public List<Team> teams = new List<Team>();
public Level mapOn;
public int maxPoints = 3;
public bool gameOn = false;
public bool friendlyfire = false;
public System.Timers.Timer onTeamCheck = new System.Timers.Timer(500);
public System.Timers.Timer flagReturn = new System.Timers.Timer(1000);
public int returnCount = 0;
public void GameStart()
{
mapOn.ChatLevel("Capture the flag game has started!");
foreach (Team team in teams)
{
ReturnFlag(null, team, false);
foreach (Player p in team.players)
{
team.SpawnPlayer(p);
}
}
onTeamCheck.Start();
onTeamCheck.Elapsed += delegate
{
foreach (Team team in teams)
{
foreach (Player p in team.players)
{
if (!p.loggedIn || p.level != mapOn)
{
team.RemoveMember(p);
}
}
}
};
flagReturn.Start();
flagReturn.Elapsed += delegate
{
foreach (Team team in teams)
{
if (!team.flagishome && team.holdingFlag == null)
{
team.ftcount++;
if (team.ftcount > 30)
{
mapOn.ChatLevel("The " + team.teamstring + " flag has returned to their base.");
team.ftcount = 0;
ReturnFlag(null, team, false);
}
}
}
};
Thread flagThread = new Thread(new ThreadStart(delegate
{
while (gameOn)
{
foreach (Team team in teams)
{
team.Drawflag();
}
Thread.Sleep(200);
}
})); flagThread.Start();
}
public void GameEnd(Team winTeam)
{
mapOn.ChatLevel("The game has ended! " + winTeam.teamstring + " has won with " + winTeam.points + " point(s)!");
foreach (Team team in teams)
{
ReturnFlag(null, team, false);
foreach (Player p in team.players)
{
p.hasflag = null;
p.carryingFlag = false;
}
team.points = 0;
}
gameOn = false;
}
public void GrabFlag(Player p, Team team)
{
if (p.carryingFlag) { return; }
ushort x = (ushort)(p.pos[0] / 32);
ushort y = (ushort)((p.pos[1] / 32) + 3);
ushort z = (ushort)(p.pos[2] / 32);
team.tempFlagblock.x = x; team.tempFlagblock.y = y; team.tempFlagblock.z = z; team.tempFlagblock.type = mapOn.GetTile(x, y, z);
mapOn.Blockchange(x, y, z, Team.GetColorBlock(team.color));
mapOn.ChatLevel(p.color + p.prefix + p.name + Server.DefaultColor + " has stolen the " + team.teamstring + " flag!");
p.hasflag = team;
p.carryingFlag = true;
team.holdingFlag = p;
team.flagishome = false;
if (p.aiming)
{
p.ClearBlockchange();
p.aiming = false;
}
}
public void CaptureFlag(Player p, Team playerTeam, Team capturedTeam)
{
playerTeam.points++;
mapOn.Blockchange(capturedTeam.tempFlagblock.x, capturedTeam.tempFlagblock.y, capturedTeam.tempFlagblock.z, capturedTeam.tempFlagblock.type);
mapOn.ChatLevel(p.color + p.prefix + p.name + Server.DefaultColor + " has captured the " + capturedTeam.teamstring + " flag!");
if (playerTeam.points >= maxPoints)
{
GameEnd(playerTeam);
return;
}
mapOn.ChatLevel(playerTeam.teamstring + " now has " + playerTeam.points + " point(s).");
p.hasflag = null;
p.carryingFlag = false;
ReturnFlag(null, capturedTeam, false);
}
public void DropFlag(Player p, Team team)
{
mapOn.ChatLevel(p.color + p.prefix + p.name + Server.DefaultColor + " has dropped the " + team.teamstring + " flag!");
ushort x = (ushort)(p.pos[0] / 32);
ushort y = (ushort)((p.pos[1] / 32) - 1);
ushort z = (ushort)(p.pos[2] / 32);
mapOn.Blockchange(team.tempFlagblock.x, team.tempFlagblock.y, team.tempFlagblock.z, team.tempFlagblock.type);
team.flagLocation[0] = x;
team.flagLocation[1] = y;
team.flagLocation[2] = z;
p.hasflag = null;
p.carryingFlag = false;
team.holdingFlag = null;
team.flagishome = false;
}
public void ReturnFlag(Player p, Team team, bool verbose)
{
if (p != null && p.spawning) { return; }
if (verbose)
{
if (p != null)
{
mapOn.ChatLevel(p.color + p.prefix + p.name + Server.DefaultColor + " has returned the " + team.teamstring + " flag!");
}
else
{
mapOn.ChatLevel("The " + team.teamstring + " flag has been returned.");
}
}
team.holdingFlag = null;
team.flagLocation[0] = team.flagBase[0];
team.flagLocation[1] = team.flagBase[1];
team.flagLocation[2] = team.flagBase[2];
team.flagishome = true;
}
public void AddTeam(string color)
{
char teamCol = (char)color[1];
Team workteam = new Team();
workteam.color = teamCol;
workteam.points = 0;
workteam.mapOn = mapOn;
char[] temp = c.Name("&" + teamCol).ToCharArray();
temp[0] = char.ToUpper(temp[0]);
string tempstring = new string(temp);
workteam.teamstring = "&" + teamCol + tempstring + " team" + Server.DefaultColor;
teams.Add(workteam);
mapOn.ChatLevel(workteam.teamstring + " has been initialized!");
}
public void RemoveTeam(string color)
{
char teamCol = (char)color[1];
Team workteam = teams.Find(team => team.color == teamCol);
List<Player> storedP = new List<Player>();
for (int i = 0; i < workteam.players.Count; i++)
{
storedP.Add(workteam.players[i]);
}
foreach (Player p in storedP)
{
workteam.RemoveMember(p);
}
}
}
}