-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathRoleManagerPatch.cs
109 lines (90 loc) · 4.36 KB
/
RoleManagerPatch.cs
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
using System;
using System.Collections.Generic;
using System.Linq;
using AmongUs.GameOptions;
using HarmonyLib;
using Hazel;
namespace DillyzRoleApi_Rewritten
{
class RoleManagerPatch
{
[HarmonyPatch(typeof(RoleManager), nameof(RoleManager.SelectRoles))]
public class RoleManagerPatch_SelectRoles
{
public static void Postfix(RoleManager __instance)
{
DillyzRoleApiMain.Instance.Log.LogInfo("DO I SELECT ROLES NOW?");
// send reset roles packet
MessageWriter writer = AmongUsClient.Instance.StartRpcImmediately(PlayerControl.LocalPlayer.NetId, (byte)CustomRpc.ResetRoles, Hazel.SendOption.None, -1);
AmongUsClient.Instance.FinishRpcImmediately(writer);
CustomRole.roleNameMap.Clear();
// rng
Random roleRNG = new Random();
List<CustomRole> roles = CustomRole.allRoles;
// assign roles
for (int r = 0; r < roles.Count; r++)
{
CustomRole role = roles[r];
role.curActive = 0;
if (role.decoy || !role.roleSeleciton || role.ghostRole)
continue;
List<PlayerControl> availablePlayers = PlayerControl.AllPlayerControls.ToArray().ToList();
availablePlayers.RemoveAll(x => !DillyzUtil.templateRole(x));
if (role.side == CustomRoleSide.Independent || role.side == CustomRoleSide.LoneWolf)
availablePlayers.RemoveAll(x => DillyzUtil.roleSide(x) == CustomRoleSide.Impostor);
else
availablePlayers.RemoveAll(x => DillyzUtil.roleSide(x) != role.side);
for (int i = 0; i < role.setting_countPerGame; i++)
{
if (availablePlayers.Count == 0)
continue;
if (role.setting_chancePerGame == 0)
return;
if (role.setting_chancePerGame != 100)
{
int rolecahcnde = UnityEngine.Random.Range(0, 100);
if (role.setting_chancePerGame < rolecahcnde)
continue;
}
role.curActive++;
int roleIndex = roleRNG.Next(0, availablePlayers.Count);
PlayerControl selectedPlayer = availablePlayers[roleIndex];
availablePlayers.Remove(selectedPlayer);
DillyzUtil.RpcSetRole(selectedPlayer, role.name);
}
}
}
}
[HarmonyPatch(typeof(RoleManager), nameof(RoleManager.AssignRoleOnDeath))]
public class RoleManagerPatch_AssignRoleOnDeath
{
public static bool Prefix(RoleManager __instance, PlayerControl player, bool specialRolesAllowed)
{
if (player == null || !player.Data.IsDead)
return false;
RoleTypes roleTheyWant = RoleTypes.CrewmateGhost;
CustomRoleSide rs = DillyzUtil.roleSide(player);
if (rs == CustomRoleSide.Impostor)
roleTheyWant = RoleTypes.ImpostorGhost;
else if (specialRolesAllowed)
{
RoleTypes guardRole = RoleTypes.GuardianAngel;
List<PlayerControl> pc = PlayerControl.AllPlayerControls.ToArray().ToList();
pc.RemoveAll(x => !x.Data.IsDead || DillyzUtil.roleSide(x) != CustomRoleSide.Crewmate);
int num = pc.Count;
IRoleOptionsCollection roleOptions = GameOptionsManager.Instance.CurrentGameOptions.RoleOptions;
if (DillyzUtil.InFreeplay())
roleTheyWant = guardRole;
else if (num <= roleOptions.GetNumPerGame(guardRole))
{
int chancePerGame = roleOptions.GetChancePerGame(guardRole);
if (HashRandom.Next(101) < chancePerGame)
roleTheyWant = guardRole;
}
}
player.RpcSetRole(roleTheyWant);
return false;
}
}
}
}