Skip to content

Commit

Permalink
Finished adding Hatch- morphing and such (CC, Overseer)
Browse files Browse the repository at this point in the history
  • Loading branch information
bellaPatricia committed Aug 17, 2014
1 parent 2b55a05 commit 24113c5
Show file tree
Hide file tree
Showing 9 changed files with 421 additions and 76 deletions.
114 changes: 105 additions & 9 deletions AnotherSc2Hack/Classes/BackEnds/GameInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -703,7 +703,9 @@ private void AssignUnitCommands(ref PredefinedTypes.Unit u, int i)
u.Id.Equals(PredefinedTypes.UnitId.ZbLiar) ||
u.Id.Equals(PredefinedTypes.UnitId.ZbHive) ||
u.Id.Equals(PredefinedTypes.UnitId.ZuBanelingCocoon) ||
u.Id.Equals(PredefinedTypes.UnitId.ZuBroodlordCocoon) ||
u.Id.Equals(PredefinedTypes.UnitId.ZuEgg) ||
u.Id.Equals(PredefinedTypes.UnitId.ZuOverseerCocoon) ||
u.Id.Equals(PredefinedTypes.UnitId.TbEbay) ||
u.Id.Equals(PredefinedTypes.UnitId.TbTechlabFactory) ||
u.Id.Equals(PredefinedTypes.UnitId.TbTechlabRax) ||
Expand All @@ -717,6 +719,7 @@ private void AssignUnitCommands(ref PredefinedTypes.Unit u, int i)
u.Id.Equals(PredefinedTypes.UnitId.PbTemplararchives) ||
u.Id.Equals(PredefinedTypes.UnitId.PbTwilightcouncil) ||
u.Id.Equals(PredefinedTypes.UnitId.PbRoboticssupportbay) ||
u.Id.Equals(PredefinedTypes.UnitId.PuMothershipCore) ||
u.Id.Equals(PredefinedTypes.UnitId.ZbEvolutionChamber) ||
u.Id.Equals(PredefinedTypes.UnitId.ZbSpire) ||
u.Id.Equals(PredefinedTypes.UnitId.ZbSpawningPool) ||
Expand Down Expand Up @@ -981,10 +984,14 @@ private Int32 GetGUnitReadUnitCount()

var lUnitIds = new List<PredefinedTypes.UnitProduction>();

/* Content of Abilities */
/* Content of Abilities (pAbilities) */
var iUnitAbilitiesPointer =
BitConverter.ToUInt32(InteropCalls.Help_ReadProcessMemory(HStarcraft, Of.UnitStruct + 0xDC + Of.UnitStructSize * iUnitNum, 4), 0);





/* Bitwise AND */
iUnitAbilitiesPointer = iUnitAbilitiesPointer & 0xFFFFFFFC;

Expand All @@ -997,7 +1004,7 @@ private Int32 GetGUnitReadUnitCount()

/* Reading the Bytearray */

/* Read the result of iUnitAbilitiesPointer */
/* Read the result of iUnitAbilitiesPointer (p1) */
var iUnitAbilitiesPointerResult = BitConverter.ToInt32(InteropCalls.Help_ReadProcessMemory(HStarcraft,
(int)iUnitAbilitiesPointer, 4),
0);
Expand All @@ -1010,6 +1017,8 @@ private Int32 GetGUnitReadUnitCount()

for (var i = 0; i < byteBuffer.Length; i++)
{
#if !DEBUG

if (byteBuffer[i].Equals(0x19))
{
iIndexToLookAt = i;
Expand All @@ -1018,9 +1027,10 @@ private Int32 GetGUnitReadUnitCount()
break;
}

#else
#region Debug/ Tests

/* var _iContentOfPointer =
var _iContentOfPointer =
BitConverter.ToUInt32(
InteropCalls.Help_ReadProcessMemory(HStarcraft, (Int32)iUnitAbilitiesPointer + 0x18 + 4 * i, 4),
0);
Expand All @@ -1034,13 +1044,35 @@ private Int32 GetGUnitReadUnitCount()
? true
: false;

var result2 =
BitConverter.ToUInt32(
InteropCalls.Help_ReadProcessMemory(HStarcraft, iUnitAbilitiesPointerResult - 3 + 0xA4 + (i * 4), 4), 0);

/* var result3 =
BitConverter.ToInt32(
InteropCalls.Help_ReadProcessMemory(HStarcraft, iUnitAbilitiesPointerResult - 3, 4), 0);*/

var resultOfResult2 =
BitConverter.ToUInt32(
InteropCalls.Help_ReadProcessMemory(HStarcraft, (UInt32)(result2 + 4), 4), 0);

/* var resultOfResult3 =
BitConverter.ToInt32(
InteropCalls.Help_ReadProcessMemory(HStarcraft, result3, 4), 0);*/

var strAbilityName =
Encoding.UTF8.GetString(InteropCalls.Help_ReadProcessMemory(HStarcraft, resultOfResult2,
16));


var _iArrayOfBytes =
BitConverter.ToInt32(
InteropCalls.Help_ReadProcessMemory(HStarcraft, (Int32)_iContentOfPointer + 0x34, 4), 0);

var _iTempPtr = BitConverter.ToInt32(
InteropCalls.Help_ReadProcessMemory(HStarcraft, (Int32)_iArrayOfBytes, 4), 0);


var _productionChunk = InteropCalls.Help_ReadProcessMemory(HStarcraft, _iTempPtr, 0x80);

var _iType = BitConverter.ToInt32(_productionChunk, 0x44);
Expand All @@ -1050,19 +1082,34 @@ private Int32 GetGUnitReadUnitCount()
var _iMineralCost = BitConverter.ToInt32(_productionChunk, 0x74);
var _iVespineCost = BitConverter.ToInt32(_productionChunk, 0x78);

var _iSomething =
BitConverter.ToInt32(
InteropCalls.Help_ReadProcessMemory(HStarcraft, (Int32) _iContentOfPointer + 0x4, 4), 0);

Debug.WriteLine("\n");
Debug.WriteLine("i: " + i);
Debug.WriteLine("NumberOfQueuedUnits: " + _iNumberOfQueuedUnits);
Debug.WriteLine("Bytebuffer Item: " + byteBuffer[i].ToString("X2") + "(" + byteBuffer[i] + ")");
Debug.WriteLine("Minerals: " + _iMineralCost);
Debug.WriteLine("Reactor Attached: " + _bReactorAttached);
Debug.WriteLine("iType: " + _iType);
Debug.WriteLine("Time Left: " + _iTimeLeft);*/
Debug.WriteLine("iPointerResult - 3: 0x" + (iUnitAbilitiesPointerResult - 3).ToString("X8"));
Debug.WriteLine("Result2 (0xA4): 0x" + result2.ToString("X8"));
Debug.WriteLine("Result of Result2: 0x" + result2.ToString("X8"));
Debug.WriteLine("Ability Name: " + strAbilityName);
//Debug.WriteLine("NumberOfQueuedUnits: " + _iNumberOfQueuedUnits);
//Debug.WriteLine("Bytebuffer Item: 0x" + byteBuffer[i].ToString("X2") + " - " + byteBuffer[i]);
//Debug.WriteLine("Minerals: " + _iMineralCost);
//Debug.WriteLine("Reactor Attached: " + _bReactorAttached);
//Debug.WriteLine("iType: " + _iType);
//Debug.WriteLine("Time Left: " + _iTimeLeft);
//Debug.WriteLine("iContent of pointer: 0x" + _iContentOfPointer.ToString("X8"));
//Debug.WriteLine("Temp ptr: 0x" + _iTempPtr.ToString("X8"));
//Debug.WriteLine("iSometzhing: 0x" + _iSomething.ToString("X8"));


#endregion

#endif
}



/* Read the DATA Pointer (that leads to our result) */
var iContentOfPointer =
BitConverter.ToUInt32(
Expand Down Expand Up @@ -1115,6 +1162,55 @@ private Int32 GetGUnitReadUnitCount()
lUnitIds.Add(prd2);
}

//That will happen if there is no unit to be build or something is morphing like a Hatch => Lair
//Thus: Check if the sourcebuilding is a hatch or something..
else if (iNumberOfQueuedUnits <= 0)
{
if (structureId.Equals(PredefinedTypes.UnitId.ZbHatchery) ||
structureId.Equals(PredefinedTypes.UnitId.ZbLiar) ||
structureId.Equals(PredefinedTypes.UnitId.ZbSpire) ||
structureId.Equals(PredefinedTypes.UnitId.ZuBroodlordCocoon) ||
structureId.Equals(PredefinedTypes.UnitId.ZuOverseerCocoon) ||
structureId.Equals(PredefinedTypes.UnitId.TbCcGround) ||
structureId.Equals(PredefinedTypes.UnitId.PuMothershipCore) ||
structureId.Equals(PredefinedTypes.UnitId.PuArchon))
{
var iUnitCommandQueuePointer = InteropCalls.ReadUInt32(HStarcraft,
Of.UnitStruct + 0xD4 + Of.UnitStructSize * iUnitNum);

if (iUnitCommandQueuePointer > 0)
{
var iUnitCommandQueueChunk = InteropCalls.Help_ReadProcessMemory(HStarcraft,
iUnitCommandQueuePointer + 0x98,
0xCC);

var iType2 = -1; //BitConverter.ToInt32(iUnitCommandQueueChunk, 0x44);
var iTimeMax2 = BitConverter.ToUInt32(iUnitCommandQueueChunk, 0x1C);
var iTimeLeft2 = (float)BitConverter.ToUInt32(iUnitCommandQueueChunk, 0);
var iMineralCost2 = BitConverter.ToInt32(iUnitCommandQueueChunk, 0xC4);
var iVespineCost2 = BitConverter.ToInt32(iUnitCommandQueueChunk, 0xC8);

var prd2 = new PredefinedTypes.UnitProduction
{
Id =
HelpFunctions.GetUnitIdFromLogicalId(structureId, iType2, (Int32) iTimeMax2,
iMineralCost2, iVespineCost2),
MineralCost = iMineralCost2,
ProductionStatus = 100 - (iTimeLeft2/iTimeMax2)*100,
ProductionTimeLeft = iTimeLeft2/65536,
ReactorAttached = false,
Supply = 0,
SupplyRaw = 0,
UnitsInProduction = 0,
VespineCost = iVespineCost2
};

lUnitIds.Add(prd2);

}
}
}

var productionChunk = InteropCalls.Help_ReadProcessMemory(HStarcraft, iTempPtr, 0x80);

#region Debug and Tests
Expand Down
73 changes: 69 additions & 4 deletions AnotherSc2Hack/Classes/BackEnds/HelpFunctions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -919,20 +919,35 @@ public static PredefinedTypes.UnitId GetUnitIdFromLogicalId(PredefinedTypes.Unit
if (logicalId.Equals(0))
return PredefinedTypes.UnitId.NbXelNagaTower;

var strStuff = Convert.ToString(logicalId, 16);
strStuff = "1" + strStuff.Substring(1);
if (!logicalId.Equals(-1))
{

var inumber = int.Parse(strStuff, NumberStyles.HexNumber);
var strStuff = Convert.ToString(logicalId, 16);
strStuff = "1" + strStuff.Substring(1);

logicalId = inumber;
var inumber = int.Parse(strStuff, NumberStyles.HexNumber);

logicalId = inumber;
}

#region Terran

#region CC - Orbital - PF

if (structureBuildFrom.Equals(PredefinedTypes.UnitId.TbCcGround))
{
//E.G. Upgrade to Lair/ Hive
if (logicalId == -1)
{
if (mineralCost == 150 && vespineCost == 0)
return PredefinedTypes.UnitId.TupUpgradeToOrbital;

if (mineralCost == 150 && vespineCost == 150)
return PredefinedTypes.UnitId.TupUpgradeToPlanetary;
}

return PredefinedTypes.UnitId.TuScv;
}

if (structureBuildFrom.Equals(PredefinedTypes.UnitId.TbPlanetary))
return PredefinedTypes.UnitId.TuScv;
Expand Down Expand Up @@ -1235,6 +1250,16 @@ public static PredefinedTypes.UnitId GetUnitIdFromLogicalId(PredefinedTypes.Unit

#region Protoss

if (structureBuildFrom.Equals(PredefinedTypes.UnitId.PuMothershipCore))
{
//E.G. Upgrade to Lair/ Hive
if (logicalId == -1)
{
if (mineralCost == 300 && vespineCost == 300)
return PredefinedTypes.UnitId.PupUpgradeToMothership;
}
}

#region Units

#region Nexus
Expand Down Expand Up @@ -1494,9 +1519,29 @@ public static PredefinedTypes.UnitId GetUnitIdFromLogicalId(PredefinedTypes.Unit
if (logicalId.Equals(0x10003))
return PredefinedTypes.UnitId.ZupBurrow;

//E.G. Upgrade to Lair/ Hive
if (logicalId == -1)
{
if (mineralCost == 150 && vespineCost == 100)
return PredefinedTypes.UnitId.ZupUpgradeToLair;

if (mineralCost == 200 && vespineCost == 150)
return PredefinedTypes.UnitId.ZupUpgradeToHive;
}

return PredefinedTypes.UnitId.ZuQueen;
}

if (structureBuildFrom.Equals(PredefinedTypes.UnitId.ZbSpire))
{
//E.G. Upgrade to Lair/ Hive
if (logicalId == -1)
{
if (mineralCost == 100 && vespineCost == 150)
return PredefinedTypes.UnitId.ZupUpgradeToGreaterSpire;
}
}

#region Units

if (structureBuildFrom.Equals(PredefinedTypes.UnitId.ZuBanelingCocoon))
Expand All @@ -1505,6 +1550,26 @@ public static PredefinedTypes.UnitId GetUnitIdFromLogicalId(PredefinedTypes.Unit
return PredefinedTypes.UnitId.ZuBaneling;
}

if (structureBuildFrom.Equals(PredefinedTypes.UnitId.ZuBroodlordCocoon))
{
//E.G. Upgrade to Lair/ Hive
if (logicalId == -1)
{
if (mineralCost == 150 && vespineCost == 150)
return PredefinedTypes.UnitId.ZupUpgradeToBroodlord;
}
}

if (structureBuildFrom.Equals(PredefinedTypes.UnitId.ZuOverseerCocoon))
{
//E.G. Upgrade to Lair/ Hive
if (logicalId == -1)
{
if (mineralCost == 50 && vespineCost == 50)
return PredefinedTypes.UnitId.ZupUpgradeToOverseer;
}
}

/* For the eggs, we have to cchack using other values..
Player 2 has the Type waay different. */
if (structureBuildFrom.Equals(PredefinedTypes.UnitId.ZuEgg))
Expand Down
23 changes: 23 additions & 0 deletions AnotherSc2Hack/Classes/BackEnds/InteropCalls.cs
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,16 @@ public enum DwmEc : uint
EnableComposition = 1,
}

public static Int32 ReadInt32(IntPtr handle, uint address)
{
return BitConverter.ToInt32(Help_ReadProcessMemory(handle, address, sizeof(Int32)), 0);
}

public static UInt32 ReadUInt32(IntPtr handle, int address)
{
return BitConverter.ToUInt32(Help_ReadProcessMemory(handle, address, sizeof(UInt32)), 0);
}

public static byte[] Help_ReadProcessMemory(IntPtr handle, int address, int size)
{
if (size < 0)
Expand All @@ -336,6 +346,19 @@ public static byte[] Help_ReadProcessMemory(IntPtr handle, int address, int size
IntPtr bytesRead;
var buffer = new byte[size];

ReadProcessMemory(handle, (IntPtr)address, buffer, size, out bytesRead);

return buffer;
}

public static byte[] Help_ReadProcessMemory(IntPtr handle, uint address, int size)
{
if (size < 0)
return new byte[0];

IntPtr bytesRead;
var buffer = new byte[size];

ReadProcessMemory(handle, (IntPtr) address, buffer, size, out bytesRead);

return buffer;
Expand Down
Loading

0 comments on commit 24113c5

Please sign in to comment.