Skip to content

Commit

Permalink
X2CommunityCore#275 even out round based differences using the larges…
Browse files Browse the repository at this point in the history
…t remainder method
  • Loading branch information
Musashi1584 committed Aug 21, 2017
1 parent 84b2f94 commit 35b5d04
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 18 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", "{AD0ECFCE-FC7E-4473-9925-A75338E1E8E9}"
Project("{5DAE07AF-E217-45C1-8DE7-FF99D6011E8A}") = "X2CommunityHighlander", "X2CommunityHighlander\X2CommunityHighlander.x2proj", "{828C9658-0F02-4AB3-90BE-F7F1711B643F}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|XCOM 2 = Debug|XCOM 2
Default|XCOM 2 = Default|XCOM 2
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{AD0ECFCE-FC7E-4473-9925-A75338E1E8E9}.Debug|XCOM 2.ActiveCfg = Debug|XCOM 2
{AD0ECFCE-FC7E-4473-9925-A75338E1E8E9}.Debug|XCOM 2.Build.0 = Debug|XCOM 2
{AD0ECFCE-FC7E-4473-9925-A75338E1E8E9}.Default|XCOM 2.ActiveCfg = Debug|XCOM 2
{AD0ECFCE-FC7E-4473-9925-A75338E1E8E9}.Default|XCOM 2.Build.0 = Debug|XCOM 2
{828C9658-0F02-4AB3-90BE-F7F1711B643F}.Debug|XCOM 2.ActiveCfg = Debug|XCOM 2
{828C9658-0F02-4AB3-90BE-F7F1711B643F}.Debug|XCOM 2.Build.0 = Debug|XCOM 2
{828C9658-0F02-4AB3-90BE-F7F1711B643F}.Default|XCOM 2.ActiveCfg = Debug|XCOM 2
{828C9658-0F02-4AB3-90BE-F7F1711B643F}.Default|XCOM 2.Build.0 = Debug|XCOM 2
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
47 changes: 34 additions & 13 deletions X2CommunityHighlander/Src/XComGame/Classes/X2LootTable.uc
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ cpptext
virtual void RollForLootTableGroup(const FLootTable& LootTable, INT Group, TArray<FName>& RolledLoot);
}

// Issue #275 - Add a loot table interface
struct Reminder {
var int EntryIndex;
var float ChanceReminder;
};

// Issue #41 - making non-private to DLC/Mods can make run-time adjustments
// requires re-invoking InitLootTables again
var config array<LootTable> LootTables;
Expand Down Expand Up @@ -174,11 +180,20 @@ private static function RecalculateLootTableChanceIntern(X2LootTable LootTable,
}
}



function int SortReminder(Reminder A, Reminder B)
{
return A.ChanceReminder < B.ChanceReminder ? -1 : 0;
}

// When the sum of chances is unequal 100% after adding/removing an entry, recalculate chances to 100% total
private static function RecalculateChancesForRollGroup(X2LootTable LootTable, int Index, int RollGroup)
{
local LootTableEntry TableEntry;
local int OldChance, NewChance, SumChances, NewSumChances, TableEntryIndex, RoundDiff, DiffIndex;
local int OldChance, NewChance, SumChances, NewSumChances, TableEntryIndex, RoundDiff;
local array<Reminder> Reminders;
local Reminder EntryReminder;

foreach LootTable.LootTables[Index].Loots(TableEntry)
{
Expand All @@ -193,35 +208,41 @@ private static function RecalculateChancesForRollGroup(X2LootTable LootTable, in
if (LootTable.LootTables[Index].Loots[TableEntryIndex].RollGroup == RollGroup)
{
OldChance = LootTable.LootTables[Index].Loots[TableEntryIndex].Chance;
NewChance = Round(100 / SumChances * OldChance);

NewChance = 100 / SumChances * OldChance;
NewSumChances += NewChance;

EntryReminder.ChanceReminder = (100 / SumChances * OldChance) - NewChance;
EntryReminder.EntryIndex = TableEntryIndex;
Reminders.AddItem(EntryReminder);

LootTable.LootTables[Index].Loots[TableEntryIndex].Chance = NewChance;
}
}
// even out round based differences


// even out round based differences using the largest remainder method
Reminders.Sort(SortReminder);
RoundDiff = (100 - NewSumChances);
NewSumChances = 0;
TableEntryIndex = 0;
for (TableEntryIndex = 0; TableEntryIndex < LootTable.LootTables[Index].Loots.Length; TableEntryIndex++)
while (RoundDiff != 0)
{
if (LootTable.LootTables[Index].Loots[TableEntryIndex].RollGroup == RollGroup)
foreach Reminders(EntryReminder)
{
if (RoundDiff > 0)
{
LootTable.LootTables[Index].Loots[TableEntryIndex].Chance += 1;
LootTable.LootTables[Index].Loots[EntryReminder.EntryIndex].Chance += 1;
RoundDiff--;
}
else if (RoundDiff < 0)
{
LootTable.LootTables[Index].Loots[TableEntryIndex].Chance -= 1;
DiffIndex++;
LootTable.LootTables[Index].Loots[EntryReminder.EntryIndex].Chance -= 1;
RoundDiff++;
}
else if (RoundDiff == 0)
{
break;
}
NewSumChances += LootTable.LootTables[Index].Loots[TableEntryIndex].Chance;
}
}
LootTable.LootTables[Index].Loots[0].Chance += (100 - NewSumChances);
}
}
// End Issue #275

0 comments on commit 35b5d04

Please sign in to comment.