Skip to content

Commit

Permalink
code refactor & secret_to_b
Browse files Browse the repository at this point in the history
  • Loading branch information
lengran committed May 14, 2024
1 parent 3a9b76e commit 79db1f4
Show file tree
Hide file tree
Showing 11 changed files with 192 additions and 123 deletions.
17 changes: 0 additions & 17 deletions ConfigTemplate.cs

This file was deleted.

119 changes: 32 additions & 87 deletions OpenPrefirePrac.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
using Vector = CounterStrikeSharp.API.Modules.Utils.Vector;
using System.Globalization;
using CounterStrikeSharp.API.Modules.Cvars;
using System.Text.Json;
using CounterStrikeSharp.API.Core.Commands;
using CounterStrikeSharp.API.Modules.Timers;
using CounterStrikeSharp.API.Modules.Entities;
Expand Down Expand Up @@ -62,7 +61,8 @@ public override void Load(bool hotReload)
RegisterListener<Listeners.OnMapStart>(OnMapStartHandler);
RegisterListener<Listeners.OnTick>(OnTickHandler);

LoadDefaultSettings();
_defaultPlayerSettings = new DefaultConfig(ModuleDirectory);
_defaultPlayerSettings.LoadDefaultSettings();

if (hotReload)
{
Expand Down Expand Up @@ -1234,64 +1234,6 @@ private void KillBot(CCSPlayerController bot)
bot.CommitSuicide(false, false);
}

private void LoadDefaultSettings()
{
string path = $"{ModuleDirectory}/default_cfg.json";

// Read default settings from PlayerStatus.cs
PlayerStatus tmpStatus = new PlayerStatus();
int tmpDifficulty = tmpStatus.HealingMethod;
int tmpTrainingMode = tmpStatus.TrainingMode;
int tmpBotWeapon = tmpStatus.BotWeapon;

if (!File.Exists(path))
{
// Use default settings
Console.WriteLine("[OpenPrefirePrac] No default settings provided. Will use default settings.");
}
else
{
// Load settings from default_cfg.json
JsonSerializerOptions options = new JsonSerializerOptions
{
ReadCommentHandling = JsonCommentHandling.Skip,
AllowTrailingCommas = true,

};

string jsonString = File.ReadAllText(path);

try
{
DefaultConfig jsonConfig = JsonSerializer.Deserialize<DefaultConfig>(jsonString, options)!;

if (jsonConfig.Difficulty > -1 && jsonConfig.Difficulty < 5)
{
tmpDifficulty = jsonConfig.Difficulty;
}

if (jsonConfig.TrainingMode > -1 && jsonConfig.TrainingMode < 2)
{
tmpTrainingMode = jsonConfig.TrainingMode;
}

if (jsonConfig.BotWeapon > -1 && jsonConfig.BotWeapon < 5)
{
tmpBotWeapon = jsonConfig.BotWeapon;
}

Console.WriteLine($"[OpenPrefirePrac] Using default settings: Difficulty = {tmpDifficulty}, TrainingMode = {tmpTrainingMode}, BotWeapon = {tmpBotWeapon}");
}
catch (System.Exception)
{
Console.WriteLine("[OpenPrefirePrac] Failed to load custom settings. Will use default settings.");

}
}

_defaultPlayerSettings = new DefaultConfig(tmpDifficulty, tmpTrainingMode, tmpBotWeapon);
}

private void StartPractice(CCSPlayerController player, int practiceIndex)
{
if (_playerCount == 0)
Expand Down Expand Up @@ -1807,42 +1749,45 @@ private void BreakBreakables()

private void OnTickHandler()
{
foreach (var player in _playerStatuses.Keys)
if (_defaultPlayerSettings!.BotAimLock)
{
if (player == null || !player.IsValid || !player.PawnIsAlive || player.PlayerPawn.Value == null)
{
continue;
}

// Aimlock bots
Vector ownerEyePos = new Vector(player.PlayerPawn.Value.AbsOrigin!.X, player.PlayerPawn.Value.AbsOrigin!.Y, player.PlayerPawn.Value.AbsOrigin!.Z + player.PlayerPawn.Value.ViewmodelOffsetZ);

foreach(var bot in _playerStatuses[player].Bots)
foreach (var player in _playerStatuses.Keys)
{
if (!bot.IsValid || !bot.PawnIsAlive || bot.PlayerPawn.Value == null)
if (player == null || !player.IsValid || !player.PawnIsAlive || player.PlayerPawn.Value == null)
{
continue;
}

Vector botEyePosition = new Vector(bot.PlayerPawn.Value.AbsOrigin!.X, bot.PlayerPawn.Value.AbsOrigin!.Y, bot.PlayerPawn.Value.AbsOrigin!.Z + bot.PlayerPawn.Value.ViewmodelOffsetZ);
// Aimlock bots
Vector ownerEyePos = new Vector(player.PlayerPawn.Value.AbsOrigin!.X, player.PlayerPawn.Value.AbsOrigin!.Y, player.PlayerPawn.Value.AbsOrigin!.Z + player.PlayerPawn.Value.ViewmodelOffsetZ);

// calculate angle
float deltaX = ownerEyePos.X - botEyePosition.X;
float deltaY = ownerEyePos.Y - botEyePosition.Y;
float deltaZ = ownerEyePos.Z - botEyePosition.Z;
double yaw = 180 * Math.Atan2(deltaY, deltaX) / Math.PI;
double tmp = Math.Sqrt(deltaX * deltaX + deltaY * deltaY);
double pitch= 180 * Math.Atan2(-1 * deltaZ, tmp) / Math.PI;
QAngle angle = new QAngle((float)pitch, (float)yaw, 0);

Server.NextFrame(() => {
if (pitch < 15 && pitch > -15)
foreach(var bot in _playerStatuses[player].Bots)
{
if (!bot.IsValid || !bot.PawnIsAlive || bot.PlayerPawn.Value == null)
{
bot.PlayerPawn.Value.Teleport(null, angle, null);
continue;
}
bot.PlayerPawn.Value.EyeAngles.X = (float)pitch;
bot.PlayerPawn.Value.EyeAngles.Y = (float)yaw;
});

Vector botEyePosition = new Vector(bot.PlayerPawn.Value.AbsOrigin!.X, bot.PlayerPawn.Value.AbsOrigin!.Y, bot.PlayerPawn.Value.AbsOrigin!.Z + bot.PlayerPawn.Value.ViewmodelOffsetZ);

// calculate angle
float deltaX = ownerEyePos.X - botEyePosition.X;
float deltaY = ownerEyePos.Y - botEyePosition.Y;
float deltaZ = ownerEyePos.Z - botEyePosition.Z;
double yaw = 180 * Math.Atan2(deltaY, deltaX) / Math.PI;
double tmp = Math.Sqrt(deltaX * deltaX + deltaY * deltaY);
double pitch= 180 * Math.Atan2(-1 * deltaZ, tmp) / Math.PI;
QAngle angle = new QAngle((float)pitch, (float)yaw, 0);

Server.NextFrame(() => {
if (pitch < 15 && pitch > -15)
{
bot.PlayerPawn.Value.Teleport(null, angle, null);
}
bot.PlayerPawn.Value.EyeAngles.X = (float)pitch;
bot.PlayerPawn.Value.EyeAngles.Y = (float)yaw;
});
}
}
}
}
Expand Down
92 changes: 92 additions & 0 deletions ServerConfig.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
using System.Text.Json;

namespace OpenPrefirePrac;

public class DefaultConfig
{
public int Difficulty { get; set; } = 3;

public int TrainingMode { get; set; } = 0;

public int BotWeapon {get; set; } = 0;

public bool BotAimLock {get; set; } = true;

private string _moduleDirectory = "";

// public DefaultConfig(int difficulty, int trainingMode, int botWeapon)
// {
// Difficulty = difficulty;
// TrainingMode = trainingMode;
// BotWeapon = botWeapon;
// }

public DefaultConfig()
{
// DeserializeConstructor
}

public DefaultConfig(string moduleDirectory)
{
_moduleDirectory = moduleDirectory;
}

public void LoadDefaultSettings()
{
string path = $"{_moduleDirectory}/default_cfg.json";

// Read default settings from PlayerStatus.cs
// PlayerStatus tmpStatus = new PlayerStatus();
// int tmpDifficulty = tmpStatus.HealingMethod;
// int tmpTrainingMode = tmpStatus.TrainingMode;
// int tmpBotWeapon = tmpStatus.BotWeapon;

if (!File.Exists(path))
{
// Use default settings
Console.WriteLine("[OpenPrefirePrac] No custom settings provided. Will use default settings.");
}
else
{
// Load settings from default_cfg.json
JsonSerializerOptions options = new JsonSerializerOptions
{
ReadCommentHandling = JsonCommentHandling.Skip,
AllowTrailingCommas = true,

};

string jsonString = File.ReadAllText(path);

try
{
DefaultConfig jsonConfig = JsonSerializer.Deserialize<DefaultConfig>(jsonString, options)!;

if (jsonConfig.Difficulty > -1 && jsonConfig.Difficulty < 5)
{
Difficulty = jsonConfig.Difficulty;
}

if (jsonConfig.TrainingMode > -1 && jsonConfig.TrainingMode < 2)
{
TrainingMode = jsonConfig.TrainingMode;
}

if (jsonConfig.BotWeapon > -1 && jsonConfig.BotWeapon < 5)
{
BotWeapon = jsonConfig.BotWeapon;
}

BotAimLock = jsonConfig.BotAimLock;

Console.WriteLine($"[OpenPrefirePrac] Using default settings: Difficulty = {Difficulty}, TrainingMode = {TrainingMode}, BotWeapon = {BotWeapon}, BotAimLock = {BotAimLock}");
}
catch (System.Exception)
{
Console.WriteLine("[OpenPrefirePrac] Failed to load custom settings. Will use default settings.");
}
}

// _defaultPlayerSettings = new DefaultConfig(tmpDifficulty, tmpTrainingMode, tmpBotWeapon);
}
}
6 changes: 5 additions & 1 deletion default_cfg.json.example
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,9 @@
// 2: Bots use AK47.
// 3: Bots use Scout.
// 4: Bots use AWP.
"BotWeapon": 0
"BotWeapon": 0,
// Aim lock for bots:
// false: Disabled.
// true: Enabled.
"BotAimLock": true
}
1 change: 1 addition & 0 deletions lang/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -79,5 +79,6 @@
"map.de_nuke.radio_to_ramp": "From radio to ramp",
"map.de_nuke.j_hall_to_upper": "Attack A site from Ramp/J-Hall",
"map.de_nuke.t_outside_to_secret": "From T-side outside to secret",
"map.de_nuke.secret_to_b": "Attack B site from secret",
"map.de_nuke.ropzPlay_to_upper": "Attack Upper doing the ropz maneuver"
}
3 changes: 2 additions & 1 deletion lang/pt-BR.json
Original file line number Diff line number Diff line change
Expand Up @@ -78,5 +78,6 @@
"map.de_nuke.t_side_entrance_of_lobby": "Entrada do saguão (lado T)",
"map.de_nuke.radio_to_ramp": "Do rádio à rampa",
"map.de_nuke.j_hall_to_upper": "Ataque ao site A da rampa/J-Hall",
"map.de_nuke.t_outside_to_secret": "Do lado T externo ao secreto"
"map.de_nuke.t_outside_to_secret": "Do lado T externo ao secreto",
"map.de_nuke.secret_to_b": "Atacar o site B a partir do segredo"
}
3 changes: 2 additions & 1 deletion lang/zh.json
Original file line number Diff line number Diff line change
Expand Up @@ -78,5 +78,6 @@
"map.de_nuke.t_side_entrance_of_lobby": "(匪家侧)匪厅入口",
"map.de_nuke.radio_to_ramp": "从连接进攻铁板",
"map.de_nuke.j_hall_to_upper": "从三楼下回防/进攻 A 区",
"map.de_nuke.t_outside_to_secret": "从匪家外场到 K1"
"map.de_nuke.t_outside_to_secret": "从匪家外场到 K1",
"map.de_nuke.secret_to_b": "从 K1 进攻 B 点"
}
2 changes: 1 addition & 1 deletion maps/de_nuke/hut_to_a.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
ramp_to_b t_side_entrance_of_lobby
ramp_to_b t_side_entrance_of_lobby t_outside_to_secret
8 0.7
-32.381592 -923.926941 -401.968750 2.903875 -2.290397 0.000000
455.967987 -1089.620239 -391.96875 4.861946 167.509247 0.000000 False
Expand Down
2 changes: 1 addition & 1 deletion maps/de_nuke/j_hall_to_upper.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
ramp_to_b hut_to_a radio_to_ramp
ramp_to_b hut_to_a radio_to_ramp t_outside_to_secret
8 0.8
632.624756 69.560097 -401.968750 -0.501703 -0.342242 0.000000
1210.969971 -78.537659 -415.96875 -0.000014 156.793747 0.000000 False 1
Expand Down
68 changes: 55 additions & 13 deletions maps/de_nuke/secret_to_b.txt
Original file line number Diff line number Diff line change
@@ -1,16 +1,46 @@
t_outside_to_lower ropzPlay_to_upper NOT_FINISHED
5 1
t_outside_to_secret ropzPlay_to_upper ramp_to_b
5 0.8
1297.500854 -2422.035400 -403.419342 39.874054 4.086581 0.000000
1591.859131 -2172.046143 -639.46228 -22.156977 -112.063347 0.000000 False
1442.031616 -2156.524170 -639.843567 -6.171827 -51.633884 0.000000 False
1586.466309 -1989.995728 -639.96875 -9.590594 -101.653015 0.000000 False
1655.970215 -1932.730347 -639.843567 -0.042594 -132.605942 0.000000 False
1647.968628 -1800.031250 -639.96875 -1.243803 -107.627319 0.000000 True
1311.968750 -1792.032593 -634.142944 0.388597 -54.836288 0.000000 False
1064.035400 -1991.968750 -639.96875 0.296187 -0.844085 0.000000 False
1072.031616 -1733.840332 -634.96875 0.881383 -35.309238 0.000000 False
1191.968384 -1816.206665 -634.96875 0.758183 -35.401638 0.000000 True
1006.216492 -1590.788818 -639.96875 0.172988 -50.718887 0.000000 False
1586.466309 -1989.995728 -639.96875 -9.590594 -101.653015 0.000000 False 1
1460.231445 -1994.095093 -639.96875 -10.549720 -77.503922 0.000000 False 2
1442.031616 -2156.524170 -639.843567 -6.171827 -51.633884 0.000000 False 3
1647.968628 -1800.031250 -639.96875 -1.243803 -107.627319 0.000000 False 4
1302.614624 -1792.028809 -639.96875 -1.023621 -49.274040 0.000000 False 5
1645.968506 -1968.694092 -639.96875 0.032376 -150.848236 0.000000 False 6
1072.031616 -1733.840332 -634.96875 0.881383 -35.309238 0.000000 False 7
1064.035400 -1991.968750 -639.96875 0.296187 -0.844085 0.000000 False 8
930.436829 -1562.031494 -639.96875 33.977985 -36.529819 0.000000 False
1036.995728 -1359.199341 -607.96875 2.605999 -71.898148 0.000000 False
1064.031616 -1178.734131 -712.806396 -5.621822 -79.557037 0.000000 False
1175.078369 -1180.267212 -711.784302 -5.270000 -89.322693 0.000000 False
1199.201782 -936.031372 -767.96875 -10.704069 -102.283417 0.000000 False
624.781555 -1527.331543 -607.96875 3.551958 -17.267735 0.000000 False
399.824249 -1553.642944 -712.079651 -5.137854 -9.517614 0.000000 False
399.551178 -1658.898682 -712.261658 -5.732026 -0.591761 0.000000 False
231.476944 -1557.596069 -767.96875 -15.170043 -5.585825 0.000000 False
97.031250 -1296.031494 -775.96875 -0.957948 -61.049747 0.000000 False
287.968719 -1399.113403 -743.46875 7.423978 -111.780624 0.000000 False
287.970795 -1495.967285 -775.96875 0.251976 -155.076996 0.000000 False
841.828369 4.593948 -639.96875 5.409671 -111.603767 0.000000 False
892.447571 -460.406006 -767.96875 0.041688 -123.419792 -0.000000 False
1015.968750 -424.031250 -639.96875 6.465701 -127.530228 0.000000 False
1111.820923 -373.031250 -639.96875 5.651744 -129.796036 0.000000 False
677.700928 -1077.749878 -771.46228 -0.420309 -145.178436 0.000000 False
738.610962 -1212.341919 -768.96875 -0.354311 -165.154465 0.000000 False
943.966675 -1163.045044 -639.96875 10.557659 -166.042816 0.000000 False
943.968750 -1276.040405 -767.96875 -0.266316 -175.392868 0.000000 False
943.968689 -1317.968750 -639.96875 11.437688 178.878647 0.000000 False
340.028229 -560.031250 -767.96875 -0.222306 -89.029968 -0.000000 False
471.517822 5.031034 -639.96875 5.145691 -93.377708 0.000000 False
943.968445 -1059.593140 -639.96875 13.615743 -170.518143 0.000000 False
641.219299 -1317.953613 -639.96875 25.297705 91.273888 0.000000 False
340.031158 -1317.968750 -639.96875 19.225683 42.521595 0.000000 False
1292.945923 -920.362122 -767.96875 -0.442297 -165.687347 0.000000 False
1451.462280 -1023.967346 -767.96875 -0.090297 179.220215 0.000000 False
819.087036 -272.031250 -767.96875 -0.363872 -90.777885 0.000000 False
816.027893 -458.714722 -767.96875 -1.234306 -22.327612 0.000000 False
1387.942749 -380.005920 -639.96875 7.819846 -103.368500 0.000000 True
1077.031372 -655.968750 -639.96875 -0.804158 4.159261 0.000000 False
1436.119019 -2422.326416 -497.708405
1352.386841 -2420.211182 -441.886993
1429.347290 -2421.271484 -493.194
Expand All @@ -25,4 +55,16 @@ t_outside_to_lower ropzPlay_to_upper NOT_FINISHED
1537.028687 -1954.535400 -630.96875
1496.487793 -1916.256470 -630.96875
1138.852417 -1906.516846 -630.96875
1135.290894 -1616.158325 -630.96875
1135.290894 -1616.158325 -630.96875
516.677856 -1614.138306 -630.96875
299.657928 -1614.385132 -758.96875
193.286682 -1616.597778 -758.96875
189.184158 -1321.232910 -766.96875
444.583710 -1316.010986 -758.412842
439.442810 -728.002930 -762.96875
890.744080 -726.850342 -758.96875
889.292358 -985.632263 -758.96875
1326.014160 -982.677002 -758.96875
1327.552246 -878.886841 -756.018677
1328.151489 -679.763428 -630.96875
1325.696655 -513.187256 -630.96875
Loading

0 comments on commit 79db1f4

Please sign in to comment.