D2R-BMBot/Strucs/GameStruc.cs

463 lines
16 KiB
C#
Raw Normal View History

2023-04-20 21:39:08 -07:00
using System;
using System.IO;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Runtime.Remoting;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using static System.Collections.Specialized.BitVector32;
using static System.Windows.Forms.AxHost;
using System.Threading;
2024-04-05 21:50:39 -07:00
using static app.Enums;
2023-04-20 21:39:08 -07:00
namespace app
{
public class GameStruc
{
Form1 Form1_0;
public string GameName = "";
public string GameDifficulty = "";
public string GameOwnerName = "";
public List<string> AllGamesNames = new List<string>();
public List<int> AllGamesPlayersCount = new List<int>();
public List<string> AllPlayersNames = new List<string>();
public int SelectedGamePlayerCount = 0;
public int SelectedGameTime = 0;
2024-03-29 00:19:36 -07:00
public string SelectedGameName = "";
2023-04-20 21:39:08 -07:00
public List<string> AllGamesTriedNames = new List<string>();
2024-04-05 21:50:39 -07:00
public bool AlreadyChickening = false;
2023-04-20 21:39:08 -07:00
2023-04-27 20:42:59 -07:00
[DllImport("user32.dll")] static extern short VkKeyScan(char ch);
[StructLayout(LayoutKind.Explicit)]
struct Helper
{
[FieldOffset(0)] public short Value;
[FieldOffset(0)] public byte Low;
[FieldOffset(1)] public byte High;
}
2023-04-20 21:39:08 -07:00
public void SetForm1(Form1 form1_1)
{
Form1_0 = form1_1;
}
public void extract()
{
long gameNameOffset = (long)Form1_0.BaseAddress + (long)Form1_0.offsets["AllGamesOffset"];
byte[] unitTableBuffer = new byte[0xfff];
Form1_0.Mem_0.ReadRawMemory(gameNameOffset, ref unitTableBuffer, unitTableBuffer.Length);
string SavePathh = Form1_0.ThisEndPath + "DumpGameStruc";
File.Create(SavePathh).Dispose();
File.WriteAllBytes(SavePathh, unitTableBuffer);
GetAllGamesNames();
}
2023-04-27 20:42:59 -07:00
public void CreateNewGame()
{
2024-03-29 00:19:36 -07:00
Form1_0.SetGameStatus("CREATING GAME");
2023-04-27 20:42:59 -07:00
Form1_0.KeyMouse_0.MouseClicc(1190, 990); //clic 'salon' if not in server
Form1_0.KeyMouse_0.MouseClicc(1275, 65); //clic 'create game' if not in game create area
Form1_0.KeyMouse_0.MouseClicc(1550, 170); //clic 'gamename'
//type game name
for (int i = 0; i < 16; i++)
{
Form1_0.KeyMouse_0.PressKey(Keys.Back);
Thread.Sleep(3);
}
string GameName = CharConfig.GameName + Form1_0.CurrentGameNumber.ToString("000");
for (int i = 0; i < GameName.Length; i++)
{
var helper = new Helper { Value = VkKeyScan(GameName[i]) };
byte virtualKeyCode = helper.Low;
Form1_0.KeyMouse_0.PressKey2((Keys)virtualKeyCode);
Thread.Sleep(3);
}
Form1_0.KeyMouse_0.MouseClicc(1550, 240); //clic 'gamepass'
//type game pass
for (int i = 0; i < 16; i++)
{
Form1_0.KeyMouse_0.PressKey(Keys.Back);
Thread.Sleep(3);
}
for (int i = 0; i < CharConfig.GamePass.Length; i++)
{
var helper = new Helper { Value = VkKeyScan(CharConfig.GamePass[i]) };
byte virtualKeyCode = helper.Low;
Form1_0.KeyMouse_0.PressKey2((Keys)virtualKeyCode);
Thread.Sleep(3);
}
2024-03-29 00:19:36 -07:00
//select difficulty
Form1_0.KeyMouse_0.MouseClicc(1360 + (100 * CharConfig.GameDifficulty), 375);
2023-04-27 20:42:59 -07:00
Form1_0.KeyMouse_0.MouseClicc(1470, 670); //clic 'create game'
2024-04-05 21:50:39 -07:00
Form1_0.TriedToCreateNewGameCount++;
2024-03-29 00:19:36 -07:00
Form1_0.SetGameStatus("LOADING GAME");
2023-04-27 20:42:59 -07:00
//###############
/*GetAllGamesNames();
SelectGame(0, false);
SelectGame(0, true);*/
}
2023-04-20 21:39:08 -07:00
public void GetAllGamesNames()
{
AllGamesNames = new List<string>();
AllGamesPlayersCount = new List<int>();
ClicTopRow();
Form1_0.KeyMouse_0.MouseClicc(1190, 990); //clic 'salon' if not in server
Form1_0.KeyMouse_0.MouseClicc(1415, 65); //clic 'join game' if not in game list area
Form1_0.KeyMouse_0.MouseClicc(1720, 210); //clic refresh
Form1_0.WaitDelay(60);
long gameNameOffset = (long)Form1_0.BaseAddress + (long)Form1_0.offsets["AllGamesOffset"];
for (int i = 0; i < 40; i++)
{
long NameOffet = gameNameOffset + 0x08 + (i * 0x128);
long CountOffet = gameNameOffset + 0xf8 + (i * 0x128);
string TestName = Form1_0.Mem_0.ReadMemString(NameOffet);
if (TestName != "")
{
if (HasGameNameInList(TestName))
{
break;
}
AllGamesNames.Add(TestName);
AllGamesPlayersCount.Add((int) Form1_0.Mem_0.ReadByteRaw((IntPtr) CountOffet));
2024-03-29 00:19:36 -07:00
//Form1_0.method_1("Game: " + TestName + " - Players: " + ((int)Form1_0.Mem_0.ReadByteRaw((IntPtr)CountOffet)), Color.Red);
2023-04-20 21:39:08 -07:00
}
else
{
break;
}
}
//GetSelectedGameInfo();
}
public bool TriedThisGame(string TestN)
{
if (AllGamesTriedNames.Count > 0)
{
for (int i = 0; i < AllGamesTriedNames.Count; i++)
{
if (AllGamesTriedNames[i] == TestN)
{
return true;
}
}
}
return false;
}
public bool HasGameNameInList(string TestN)
{
if (AllGamesNames.Count > 0)
{
for (int i = 0; i < AllGamesNames.Count; i++)
{
if (AllGamesNames[i] == TestN)
{
return true;
}
}
}
return false;
}
public void GetSelectedGameInfo()
{
2024-03-29 00:19:36 -07:00
if ((int) Form1_0.offsets["GameSelectedOffset"] <= 64)
2023-04-27 20:42:59 -07:00
{
Form1_0.PatternsScan_0.PatternScan();
}
2024-03-29 00:19:36 -07:00
Form1_0.method_1("------------------------------------------", Color.Black);
2023-04-20 21:39:08 -07:00
//0x53F or 0x540 size
long gameOffset = (long)Form1_0.BaseAddress + (long)Form1_0.offsets["GameSelectedOffset"];
long PlayersNamesOffset = gameOffset + 0x138; //then 0x78 offset each others names
AllPlayersNames = new List<string>();
SelectedGamePlayerCount = (int)Form1_0.Mem_0.ReadByteRaw((IntPtr) (gameOffset + 0x108));
SelectedGameTime = Form1_0.Mem_0.ReadInt32Raw((IntPtr)(gameOffset + 0xf0));
2024-03-29 00:19:36 -07:00
Form1_0.method_1("Player Count: " + SelectedGamePlayerCount, Color.OrangeRed);
2023-04-20 21:39:08 -07:00
for (int i = 0; i < SelectedGamePlayerCount; i++)
{
long NameOffet = PlayersNamesOffset + (i * 0x78);
string TestName = Form1_0.Mem_0.ReadMemString(NameOffet);
if (TestName != "")
{
AllPlayersNames.Add(TestName);
2024-03-29 00:19:36 -07:00
Form1_0.method_1("Player Name: " + TestName, Color.OrangeRed);
}
}
SelectedGameName = "";
byte[] buffer = new byte[16];
Form1_0.Mem_0.ReadRawMemory(gameOffset + 0x08, ref buffer, 16);
for (int i2 = 0; i2 < 16; i2++)
{
if (buffer[i2] != 0x00)
{
SelectedGameName += (char)buffer[i2];
2023-04-20 21:39:08 -07:00
}
}
//LogGameTime();
}
public void SelectGame(int ThisIndex, bool EnterGame)
{
if (EnterGame)
{
if (AllPlayersNames.Count > 0)
{
GameOwnerName = AllPlayersNames[0];
}
else
{
return;
}
if (SelectedGamePlayerCount == 8)
{
return;
}
}
//Form1_0.method_1("Selecting game: " + ThisIndex + ", ENTER: " + EnterGame);
if (ThisIndex >= 0 && ThisIndex <= 13)
{
if (!EnterGame) ClicTopRow();
ClicGameIndex(ThisIndex);
if (EnterGame)
{
Thread.Sleep(10);
ClicGameIndex(ThisIndex);
Thread.Sleep(10);
ClicGameIndex(ThisIndex);
}
}
2024-04-05 21:50:39 -07:00
//dont select any game greater than this index as 'refreshing' the search can make a bug where there no more games listed
/*if (ThisIndex >= 14 && ThisIndex <= 26)
2023-04-20 21:39:08 -07:00
{
if (!EnterGame) ClicMidRow();
ClicGameIndex(ThisIndex - 13);
if (EnterGame)
{
Thread.Sleep(10);
ClicGameIndex(ThisIndex);
Thread.Sleep(10);
ClicGameIndex(ThisIndex);
}
}
if (ThisIndex >= 27 && ThisIndex <= 39)
{
if (!EnterGame) ClicBottomRow();
ClicGameIndex(ThisIndex - 26);
if (EnterGame)
{
Thread.Sleep(10);
ClicGameIndex(ThisIndex);
Thread.Sleep(10);
ClicGameIndex(ThisIndex);
}
2024-04-05 21:50:39 -07:00
}*/
2023-04-20 21:39:08 -07:00
Form1_0.WaitDelay(40);
2023-04-27 20:42:59 -07:00
GetSelectedGameInfo();
2023-04-20 21:39:08 -07:00
}
public void ClicGameIndex(int ThisIndex)
{
//1345, 260 (+25px each games)
Form1_0.KeyMouse_0.MouseClicc(1345, (int) (260 + (ThisIndex * 27.3)));
}
public void ClicTopRow()
{
Form1_0.KeyMouse_0.MouseClicc(1510, 270);
Form1_0.WaitDelay(10);
}
public void ClicMidRow()
{
Form1_0.KeyMouse_0.MouseClicc(1510, 465);
Form1_0.WaitDelay(10);
}
public void ClicBottomRow()
{
Form1_0.KeyMouse_0.MouseClicc(1510, 605);
Form1_0.WaitDelay(10);
}
public void CheckChickenGameTime()
{
2024-04-05 21:50:39 -07:00
if (!CharConfig.RunItemGrabScriptOnly)
2023-04-20 21:39:08 -07:00
{
2024-04-05 21:50:39 -07:00
if (!CharConfig.IsRushing)
{
if (!AlreadyChickening)
{
TimeSpan Checkkt = (DateTime.Now - Form1_0.GameStartedTime);
Form1_0.method_GameTimeLabel(Checkkt.Minutes.ToString("00") + ":" + Checkkt.Seconds.ToString("00") + ":" + Checkkt.Milliseconds.ToString("0"));
if (Checkkt.TotalMinutes > CharConfig.MaxGameTime)
{
Form1_0.method_1("Leaving reason: Chicken time", Color.Red);
Form1_0.LeaveGame(false);
AlreadyChickening = true;
}
}
}
2023-04-20 21:39:08 -07:00
}
}
public string GetTimeNow()
{
DateTime ThisTimee = DateTime.Now;
2024-04-05 21:50:39 -07:00
string HourTime = ThisTimee.Minute.ToString("00") + ":" + ThisTimee.Second.ToString("00");
2023-04-20 21:39:08 -07:00
string MonthTime = ThisTimee.Day + "-" + ThisTimee.Month + "-" + ThisTimee.Year;
return HourTime + " (" + MonthTime + ")";
}
public void SetNewGame()
{
2024-03-29 00:19:36 -07:00
//Form1_0.method_1("------------------------------------------", Color.DarkBlue);
2023-04-20 21:39:08 -07:00
Form1_0.method_1("New game started: " + GetTimeNow(), Color.DarkBlue);
Form1_0.GameStartedTime = DateTime.Now;
long gameNameOffset = (long)Form1_0.offsets["gameDataOffset"] + 0x40;
long gameNameAddress = (long)Form1_0.BaseAddress + gameNameOffset;
GameName = Form1_0.Mem_0.ReadMemString(gameNameAddress);
2024-04-05 21:50:39 -07:00
method_GameLabel(GameName);
2024-03-29 00:19:36 -07:00
Form1_0.method_1("Entered game: " + GameName, Color.DarkBlue);
2023-04-20 21:39:08 -07:00
AllGamesTriedNames = new List<string>();
}
2024-04-05 21:50:39 -07:00
public void method_GameLabel(string string_3)
{
//try
//{
2024-04-14 18:51:35 -07:00
/*if (Form1_0.labelGameName.InvokeRequired)
2024-04-05 21:50:39 -07:00
{
// Call this same method but append THREAD2 to the text
Action safeWrite = delegate { method_GameLabel(string_3); };
Form1_0.labelGameName.Invoke(safeWrite);
}
else
{
Form1_0.labelGameName.Text = string_3;
Application.DoEvents();
2024-04-14 18:51:35 -07:00
}*/
Form1_0.labelGameName.Text = string_3;
2024-04-05 21:50:39 -07:00
//}
//catch { }
}
2023-04-20 21:39:08 -07:00
public void LogGameTime()
{
TimeSpan ThisTimee = DateTime.Now - Form1_0.GameStartedTime;
2024-03-29 00:19:36 -07:00
Form1_0.method_1("Game Time: " + ThisTimee.Minutes.ToString("00") + ":" + ThisTimee.Seconds.ToString("00") + ":" + ThisTimee.Milliseconds.ToString("0"), Color.DarkBlue);
2023-04-20 21:39:08 -07:00
}
public Dictionary<string, int> World2Screen(long playerX, long playerY, long targetx, long targety)
{
//; scale = 27
//double scale = Form1_0.centerModeScale * Form1_0.renderScale * 100;
double scale = 40.8;
long xdiff = targetx - playerX;
long ydiff = targety - playerY;
double angle = 0.785398; //45 deg
double x = xdiff * Math.Cos(angle) - ydiff * Math.Sin(angle);
double y = xdiff * Math.Sin(angle) + ydiff * Math.Cos(angle);
int xS = (int) (Form1_0.CenterX + (x * scale));
//int yS = (int) (Form1_0.CenterY + (y * scale * 0.5) - 10);
int yS = (int)(Form1_0.CenterY + (y * scale * 0.5) - 30);
Dictionary<string, int> NewDict = new Dictionary<string, int>();
NewDict["x"] = xS;
NewDict["y"] = yS;
return NewDict;
}
public bool IsGameRunning()
{
Process[] ProcList = Process.GetProcessesByName("D2R");
if (ProcList.Length == 0)
{
return false;
}
return true;
}
public bool IsInGame()
{
2024-04-14 18:51:35 -07:00
try
2023-04-20 21:39:08 -07:00
{
2024-04-14 18:51:35 -07:00
long baseAddress = (long)Form1_0.BaseAddress + (long)Form1_0.offsets["unitTable"] - 64;
byte[] unitTableBuffer = new byte[1];
Form1_0.Mem_0.ReadRawMemory(baseAddress, ref unitTableBuffer, 1);
//Console.WriteLine(unitTableBuffer[0]);
if (unitTableBuffer[0] == 0x01)
{
return true;
}
2023-04-20 21:39:08 -07:00
}
2024-04-14 18:51:35 -07:00
catch { }
2023-04-20 21:39:08 -07:00
return false;
}
2024-04-05 21:50:39 -07:00
public List<Area> GetTerrorZones()
{
List<Area> areas = new List<Area>();
for (int i = 0; i < 7; i++)
{
uint tzArea = Form1_0.Mem_0.ReadUInt32((IntPtr) (0x299E2D8 + (i * 4)));
if (tzArea != 0)
{
areas.Add((Area)tzArea);
}
}
return areas;
}
2023-04-20 21:39:08 -07:00
}
}