Skip to content

Commit

Permalink
X2CommunityCore#275 Removes InitLootTable calls and fixes RemoveEntry
Browse files Browse the repository at this point in the history
  • Loading branch information
Musashi1584 committed Aug 18, 2017
1 parent 5aad08c commit dfa0c75
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 43 deletions.
10 changes: 5 additions & 5 deletions X2CommunityHighlander.XCOM_sln
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,18 @@ Microsoft Visual Studio Solution File, Format Version 12.00
# XCOM ModBuddy Solution File, Format Version 11.00
VisualStudioVersion = 12.0.21005.1
MinimumVisualStudioVersion = 10.0.40219.1
Project("{5DAE07AF-E217-45C1-8DE7-FF99D6011E8A}") = "X2CommunityHighlander", "X2CommunityHighlander\X2CommunityHighlander.x2proj", "{B6499644-295D-4FEF-A98C-894D4C57D9C0}"
Project("{5DAE07AF-E217-45C1-8DE7-FF99D6011E8A}") = "X2CommunityHighlander", "X2CommunityHighlander\X2CommunityHighlander.x2proj", "{7D66C20E-7B03-407E-A6C7-9FA1F14BE188}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|XCOM 2 = Debug|XCOM 2
Default|XCOM 2 = Default|XCOM 2
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{B6499644-295D-4FEF-A98C-894D4C57D9C0}.Debug|XCOM 2.ActiveCfg = Debug|XCOM 2
{B6499644-295D-4FEF-A98C-894D4C57D9C0}.Debug|XCOM 2.Build.0 = Debug|XCOM 2
{B6499644-295D-4FEF-A98C-894D4C57D9C0}.Default|XCOM 2.ActiveCfg = Debug|XCOM 2
{B6499644-295D-4FEF-A98C-894D4C57D9C0}.Default|XCOM 2.Build.0 = Debug|XCOM 2
{7D66C20E-7B03-407E-A6C7-9FA1F14BE188}.Debug|XCOM 2.ActiveCfg = Debug|XCOM 2
{7D66C20E-7B03-407E-A6C7-9FA1F14BE188}.Debug|XCOM 2.Build.0 = Debug|XCOM 2
{7D66C20E-7B03-407E-A6C7-9FA1F14BE188}.Default|XCOM 2.ActiveCfg = Debug|XCOM 2
{7D66C20E-7B03-407E-A6C7-9FA1F14BE188}.Default|XCOM 2.Build.0 = Debug|XCOM 2
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
100 changes: 62 additions & 38 deletions X2CommunityHighlander/Src/XComGame/Classes/X2LootTable.uc
Original file line number Diff line number Diff line change
Expand Up @@ -18,111 +18,135 @@ native function RollForLootTable(const out name LootTableName, out array<name> R

// Start Issue #275 - Add a loot table interface
// static function are for use in the DLCInfo OnPostTemplatesCreated event
public function AddEntryAndReload(name TableName, LootTableEntry TableEntry)
public function AddEntry(name TableName, LootTableEntry TableEntry)
{
AddEntry(self, TableName, TableEntry);
InitLootTables();
AddEntryIntern(self, TableName, TableEntry);
}

public function RemoveEntryAndReload(name TableName, LootTableEntry TableEntry)
public function RemoveEntry(name TableName, LootTableEntry TableEntry)
{
RemoveEntry(self, TableName, TableEntry);
InitLootTables();
RemoveEntryIntern(self, TableName, TableEntry);
}

public function AddLootTableAndReload(LootTable AddLootTable)
public function AddLootTable(LootTable AddLootTable)
{
local LootTableEntry Loot;
foreach AddLootTable.Loots(Loot)
{
AddEntry(self, AddLootTable.TableName, Loot);
AddEntryIntern(self, AddLootTable.TableName, Loot);
}
InitLootTables();
}

public function RemoveLootTableAndReload(LootTable LootTable)
public function RemoveLootTable(LootTable LootTable)
{
RemoveLootTable(LootTable);
InitLootTables();
}

public static function AddLootTable(LootTable AddLootTable)
public static function AddLootTableStatic(LootTable AddLootTable)
{
local X2LootTable LootTable;
local LootTableEntry Loot;

LootTable = X2LootTable(class'Engine'.static.FindClassDefaultObject(string(default.Class.Name)));
foreach AddLootTable.Loots(Loot)
{
AddEntry(LootTable, AddLootTable.TableName, Loot);
AddEntryIntern(LootTable, AddLootTable.TableName, Loot);
}
}

public static function RemoveLootTable(LootTable RemoveLootTable)
public static function RemoveLootTableStatic(LootTable RemoveLootTable)
{
local X2LootTable LootTable;

LootTable = X2LootTable(class'Engine'.static.FindClassDefaultObject(string(default.Class.Name)));
LootTable.LootTables.RemoveItem(RemoveLootTable);
}

public static function AddEntryToLootTable(name TableName, LootTableEntry AddTableEntry)
public static function AddEntryStatic(name TableName, LootTableEntry AddTableEntry)
{
local X2LootTable LootTable;

LootTable = X2LootTable(class'Engine'.static.FindClassDefaultObject(string(default.Class.Name)));
AddEntry(LootTable, TableName, AddTableEntry);
AddEntryIntern(LootTable, TableName, AddTableEntry);
}

public static function RemoveEntryFromLootTable(name TableName, LootTableEntry TableEntry)
public static function RemoveEntryStatic(name TableName, LootTableEntry TableEntry)
{
local X2LootTable LootTable;
LootTable = X2LootTable(class'Engine'.static.FindClassDefaultObject(string(default.Class.Name)));
RemoveEntry(LootTable, TableName, TableEntry);
RemoveEntryIntern(LootTable, TableName, TableEntry);
}

private static function RemoveEntry(X2LootTable LootTable, name TableName, LootTableEntry TableEntry)
private static function RemoveEntryIntern(X2LootTable LootTable, name TableName, LootTableEntry TableEntry)
{
local int Index;
local int Index, EntryIndex;

Index = LootTable.LootTables.Find('TableName', TableName);

if (Index != INDEX_NONE)
{
LootTable.LootTables[Index].Loots.RemoveItem(TableEntry);
for (EntryIndex = 0; EntryIndex < LootTable.LootTables[Index].Loots.Length; EntryIndex++)
{
if (LootTable.LootTables[Index].Loots[EntryIndex].RollGroup == TableEntry.RollGroup &&
LootTable.LootTables[Index].Loots[EntryIndex].TemplateName == TableEntry.TemplateName &&
LootTable.LootTables[Index].Loots[EntryIndex].TableRef == TableEntry.TableRef
)
{
// Remove the table entry
LootTable.LootTables[Index].Loots.Remove(EntryIndex, 1);
// Recalculate the chances for the roll group
RecalculateChances(LootTable, Index, TableEntry.RollGroup);
break;
}
}
}
}

// When the sum of chances is greater 100% after adding an entry, recalculate chances to 100% total
private static function AddEntry(X2LootTable LootTable, name TableName, LootTableEntry AddTableEntry)
private static function AddEntryIntern(X2LootTable LootTable, name TableName, LootTableEntry TableEntry)
{
local LootTableEntry TableEntry;
local int Index, OldChance, NewChance, SumChances, TableEntryIndex;
local int Index;

Index = LootTable.LootTables.Find('TableName', TableName);

if (Index != INDEX_NONE)
{
// Add the new table entry
LootTable.LootTables[Index].Loots.AddItem(AddTableEntry);
LootTable.LootTables[Index].Loots.AddItem(TableEntry);

// Recalculate the chances
foreach LootTable.LootTables[Index].Loots(TableEntry)
{
if (TableEntry.RollGroup == AddTableEntry.RollGroup)
SumChances += TableEntry.Chance;
}
// Recalculate the chances for the roll group
RecalculateChances(LootTable, Index, TableEntry.RollGroup);
}
}

// When the sum of chances is unequal 100% after adding/removing an entry, recalculate chances to 100% total
static function RecalculateChances(X2LootTable LootTable, int Index, int RollGroup)
{
local LootTableEntry TableEntry;
local int OldChance, NewChance, SumChances, NewSumChances, TableEntryIndex;

foreach LootTable.LootTables[Index].Loots(TableEntry)
{
if (TableEntry.RollGroup == RollGroup)
SumChances += TableEntry.Chance;
}

if (SumChances > 100)
if (SumChances != 100)
{
for (TableEntryIndex = 0; TableEntryIndex < LootTable.LootTables[Index].Loots.Length; TableEntryIndex++)
{
for (TableEntryIndex = 0; TableEntryIndex < LootTable.LootTables[Index].Loots.Length; TableEntryIndex++)
if (LootTable.LootTables[Index].Loots[TableEntryIndex].RollGroup == RollGroup)
{
if (LootTable.LootTables[Index].Loots[TableEntryIndex].RollGroup == AddTableEntry.RollGroup)
OldChance = LootTable.LootTables[Index].Loots[TableEntryIndex].Chance;
NewChance = Round(100 / SumChances * OldChance);

// Add round based differences to the last entry
NewSumChances += NewChance;
if(TableEntryIndex == LootTable.LootTables[Index].Loots.Length - 1 && NewSumChances != 100)
{
OldChance = LootTable.LootTables[Index].Loots[TableEntryIndex].Chance;
NewChance = Round(100 / SumChances * OldChance);
LootTable.LootTables[Index].Loots[TableEntryIndex].Chance = NewChance;
NewChance += (100 - NewSumChances);
}

LootTable.LootTables[Index].Loots[TableEntryIndex].Chance = NewChance;
}
}
}
Expand Down

0 comments on commit dfa0c75

Please sign in to comment.