aboutsummaryrefslogtreecommitdiffstats
path: root/code/phase/AssignPhase.cs
blob: f69ca7bed4b6da2aed1614e59d9ccf3ad7865e46 (plain)
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
using Sandbox;
using Sandbox.UI;
using System;
using System.Collections.Generic;
using System.Linq;

namespace MurderGame;

public partial class AssignPhase : BasePhase
{
	public override string Title => "Assigning teams";
	public int TicksElapsed;
	
	private List<string> NatoNames = new()
	{
		"Alpha", 
		"Bravo", 
		"Charlie",
		"Delta",
		"Echo",
		"Foxtrot",
		"Golf",
		"Hotel",
		"India",
		"Juliet",
		"Kilo",
		"Lima",
		"Mike",
		"November",
		"Oscar",
		"Papa",
		"Quebec",
		"Romeo",
		"Sierra",
		"Tango",
		"Uniform",
		"Victor",
		"Whiskey",
		"X-Ray",
		"Yankee",
		"Zulu"
	};
	
	private List<uint> Colors = new()
	{
		0x0074D9, // blue
		0x7FDBFF, // aqua
		0x39CCCC, // teal
		0xF012BE, // fuchsia
		0xFF4136, // red
		0xFF851B, // orange
		0xFFDC00, // yellow
		0x3D9970, // olive
		0x2ECC40, // lime
		0x01FF70  // green
	};

	public override void Activate()
	{
		// cleanup -- start
		foreach (var entity in Entity.All.OfType<DroppedWeapon>())
		{
			entity.Delete();
		}
		DeleteFootprints();
		// cleanup -- end

		var detectivesNeeded = 1;
		var murderersNeeded = 1;
		
		Random random = new(Guid.NewGuid().GetHashCode());
		
		var spawnPoints = Entity.All.OfType<SpawnPoint>().OrderBy( _ => random.Next() ).ToList();
		var clients = Game.Clients.ToList().OrderBy( _ => random.Next() );
		var natoNamesRemaining = new List<string>(NatoNames.OrderBy( _ => random.Next() ));
		var colorsRemaining = new List<uint>(Colors.OrderBy( _ => random.Next() ));

		foreach ( var client in clients )
		{
			if (client.Pawn != null)
			{
				((Player) client.Pawn).Cleanup();
				client.Pawn.Delete();
			}
			
			Player pawn = new();
			client.Pawn = pawn;
			
			if (spawnPoints.Count == 0)
			{
				ChatBox.Say( "Could not spawn " + client.Name + " as there are not enough spawn points." );
				pawn.Team = Team.Spectator;
				continue;
			}
			pawn.DressFromClient( client );
			
			// re-use names and colours if needed
			if (natoNamesRemaining.Count == 0)
			{
				natoNamesRemaining = new List<string>(NatoNames);
			}
			if (colorsRemaining.Count == 0)
			{
				colorsRemaining = new List<uint>(Colors);
			}

			// assign team
			if (murderersNeeded > 0)
			{
				pawn.Team = Team.Murderer;
				--murderersNeeded;
			}
			else if (detectivesNeeded > 0)
			{
				pawn.Team = Team.Detective;
				--detectivesNeeded;
			}
			else
			{
				pawn.Team = Team.Bystander;
			}
			Log.Info( "Assigning " + client.Name + " to team " + pawn.GetTeamName() );

			// position pawn
			var spawnPoint = spawnPoints[0];
			spawnPoints.RemoveAt( 0 );
			var tx = spawnPoint.Transform;
			tx.Position += Vector3.Up * 10.0f;
			pawn.Transform = tx;
			
			// assign nato name
			var natoName = natoNamesRemaining[0];
			natoNamesRemaining.RemoveAt( 0 );
			pawn.CharacterName = natoName;
			
			// assign nato name
			var hexColor = colorsRemaining[0];
			colorsRemaining.RemoveAt( 0 );
			pawn.Color = Color.FromRgb(hexColor);

			RoleOverlay.Show( To.Single( client ) );
		}
		base.TimeLeft = 5;
	}

	[ClientRpc]
	public static void DeleteFootprints()
	{
		foreach (var entity in Entity.All.OfType<Footprint>())
		{
			entity.Delete();
		}
	}

	public override void Deactivate()
	{
		foreach (var client in Game.Clients)
		{
			RoleOverlay.Hide( To.Single( client ) );
		}
	}

	public override void Tick()
	{
		++TicksElapsed;
		if ( TimeLeft == -1 || TicksElapsed % Game.TickRate != 0 || --base.TimeLeft != 0 )
		{
			return;
		}

		IsFinished = true;
		NextPhase = new PlayPhase();
	}

}