-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSteamWorldRandomizer.cs
More file actions
133 lines (113 loc) · 3.94 KB
/
SteamWorldRandomizer.cs
File metadata and controls
133 lines (113 loc) · 3.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
using System.Collections.Generic;
namespace LiveSplit.SteamWorldDig {
public class SteamWorldRandomizer
{
private SteamWorldMemory mem;
private static SeedRandom random;
private List<string> haveItems;
private readonly int seed;
private List<Location> locations;
private List<string> upgrades;
private List<string> areas;
public SteamWorldRandomizer(SteamWorldMemory mem, int seed, List<Location> locations)
{
random = new SeedRandom(seed);
this.locations = locations;
this.seed = seed;
this.mem = mem;
haveItems = new List<string>();
upgrades = mem.GetUpgradeNames();
areas = mem.GetAreaNames();
}
public int Randomize()
{
bool ret;
ret = PermuteItems(upgrades, Location.RandomizeType.Upgrade);
if (!ret) return -1;
ret = PermuteItems(areas, Location.RandomizeType.Area);
if (!ret) return -2;
ret = CheckValid();
if (!ret) return 0;
/* Write into the game */
foreach (var location in locations)
{
if (location.Type == Location.RandomizeType.Upgrade)
{
mem.RandomizeUpgrade(location.Name, location.Grant);
}
if (location.Type == Location.RandomizeType.Area)
{
mem.RandomizeArea(location.Name, location.Grant);
}
}
return 1;
}
private bool CheckValid()
{
haveItems.Clear();
bool didAdd = false;
do
{
didAdd = false;
foreach (var location in locations)
{
/* We don't care if we already have the item */
if (haveItems.Contains(location.Grant))
{
continue;
}
/* Check that we have access to the location */
if (location.CanAccess(haveItems))
{
/* Make a copy of our items and add the granted item, then check if we can escape */
List<string> itemsAdded = new List<string>(haveItems);
itemsAdded.Add(location.Grant);
if (location.CanEscape(itemsAdded))
{
/* We can successfully grab the item and escape! Add the item to our list */
haveItems.Add(location.Grant);
didAdd = true;
}
}
}
}
while (didAdd && !haveItems.Contains("vectron_boss"));
return haveItems.Contains("vectron_boss");
}
private static void Shuffle<T>(IList<T> list)
{
int n = list.Count;
while (n > 1)
{
n--;
int k = random.Next(n + 1);
T value = list[k];
list[k] = list[n];
list[n] = value;
}
}
private bool PermuteItems(List<string> items, Location.RandomizeType type)
{
List<string> randomized_items = new List<string>(items);
Shuffle(randomized_items);
int i = 0;
foreach (var location in locations)
{
if (location.Type == type)
{
if (i == randomized_items.Count)
{
return false;
}
location.Grant = randomized_items[i];
i++;
}
}
if (i != randomized_items.Count)
{
return false;
}
return true;
}
}
}