-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathLayoutWorkerComplex.cs
More file actions
301 lines (280 loc) · 9.96 KB
/
LayoutWorkerComplex.cs
File metadata and controls
301 lines (280 loc) · 9.96 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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using RimWorld;
using UnityEngine;
using Verse;
public class LayoutWorkerComplex : LayoutWorker_Structure
{
private static readonly FloatRange ThreatPointsFactorRange = new FloatRange(0.25f, 0.35f);
private static readonly List<Thing> tmpSpawnedThreatThings = new List<Thing>();
private static readonly IntRange MergeRange = new IntRange(1, 4);
private static readonly List<ComplexThreat> useableThreats = new List<ComplexThreat>();
private static readonly Dictionary<int, List<ComplexThreatDef>> usedThreatsByRoom = new Dictionary<int, List<ComplexThreatDef>>();
public new ComplexLayoutDef Def => (ComplexLayoutDef)base.Def;
public LayoutWorkerComplex(LayoutDef def)
: base(def)
{
}
public virtual Faction GetFixedHostileFactionForThreats()
{
return null;
}
protected virtual void PreSpawnThreats(List<LayoutRoom> rooms, Map map, List<Thing> allSpawnedThings)
{
}
protected override StructureLayout GetStructureLayout(StructureGenParams parms, CellRect rect)
{
LayoutStructureSketch sketch = parms.sketch;
float areaPrunePercent = Def.areaPrunePercent;
int minRoomHeight = Def.minRoomHeight;
return RoomLayoutGenerator.GenerateRandomLayout(minRoomWidth: Def.minRoomWidth, minRoomHeight: minRoomHeight, areaPrunePercent: areaPrunePercent, canRemoveRooms: true, generateDoors: false, maxMergeRoomsRange: MergeRange, sketch: sketch, container: rect, corridor: Def.corridorDef, corridorExpansion: 2, corridorShapes: Def.corridorShapes, canDisconnectRooms: Def.canDisconnectRooms);
}
public override void Spawn(LayoutStructureSketch layoutStructureSketch, Map map, IntVec3 pos, float? threatPoints = null, List<Thing> allSpawnedThings = null, bool roofs = true, bool canReuseSketch = false, Faction faction = null)
{
List<Thing> list = allSpawnedThings ?? new List<Thing>();
base.Spawn(layoutStructureSketch, map, pos, threatPoints, list, roofs, canReuseSketch, faction);
List<LayoutRoom> rooms = layoutStructureSketch.structureLayout.Rooms;
SpawnThings(layoutStructureSketch, map, rooms, list);
if (threatPoints.HasValue && !Def.threats.NullOrEmpty())
{
PreSpawnThreats(rooms, map, list);
SpawnThreats(layoutStructureSketch, map, pos, threatPoints.Value, list, rooms);
}
PostSpawnStructure(rooms, map, list);
tmpSpawnedThreatThings.Clear();
}
private static void SpawnThings(LayoutStructureSketch layoutStructureSketch, Map map, List<LayoutRoom> rooms, List<Thing> spawnedThings)
{
if (layoutStructureSketch.thingsToSpawn.NullOrEmpty())
{
return;
}
HashSet<LayoutRoom> usedRooms = new HashSet<LayoutRoom>();
for (int num = layoutStructureSketch.thingsToSpawn.Count - 1; num >= 0; num--)
{
Thing thing = layoutStructureSketch.thingsToSpawn[num];
LayoutRoom roomUsed;
Rot4 rotUsed;
IntVec3 loc = LayoutWorker.FindBestSpawnLocation(rooms, thing.def, map, out roomUsed, out rotUsed, usedRooms);
if (!loc.IsValid)
{
loc = LayoutWorker.FindBestSpawnLocation(rooms, thing.def, map, out roomUsed, out rotUsed);
}
if (!loc.IsValid)
{
thing.Destroy();
layoutStructureSketch.thingsToSpawn.RemoveAt(num);
}
else
{
GenSpawn.Spawn(thing, loc, map, rotUsed);
spawnedThings.Add(thing);
layoutStructureSketch.thingsToSpawn.RemoveAt(num);
if (!layoutStructureSketch.thingDiscoveredMessage.NullOrEmpty())
{
string signalTag = "ThingDiscovered" + Find.UniqueIDsManager.GetNextSignalTagID();
foreach (CellRect rect in roomUsed.rects)
{
RectTrigger obj = (RectTrigger)ThingMaker.MakeThing(ThingDefOf.RectTrigger);
obj.signalTag = signalTag;
obj.Rect = rect;
GenSpawn.Spawn(obj, rect.CenterCell, map);
}
SignalAction_Message obj2 = (SignalAction_Message)ThingMaker.MakeThing(ThingDefOf.SignalAction_Message);
obj2.signalTag = signalTag;
obj2.message = layoutStructureSketch.thingDiscoveredMessage;
obj2.messageType = MessageTypeDefOf.PositiveEvent;
obj2.lookTargets = thing;
GenSpawn.Spawn(obj2, loc, map);
}
}
}
}
protected virtual void PostSpawnStructure(List<LayoutRoom> rooms, Map map, List<Thing> allSpawnedThings)
{
if (ModsConfig.IdeologyActive)
{
SpawnRoomRewards(rooms, map, allSpawnedThings);
SpawnCommsConsole(rooms, map);
}
}
private static void SpawnCommsConsole(List<LayoutRoom> rooms, Map map)
{
foreach (LayoutRoom item in rooms.InRandomOrder())
{
if (item.TryGetRandomCellInRoom(ThingDefOf.CommsConsole, map, out var cell, null, 1, 0, (IntVec3 c) => CanPlaceCommsConsoleAt(c, map)))
{
GenSpawn.Spawn(ThingDefOf.AncientCommsConsole, cell, map);
break;
}
}
}
private static bool CanPlaceCommsConsoleAt(IntVec3 cell, Map map)
{
foreach (IntVec3 item in GenAdj.OccupiedRect(cell, Rot4.North, ThingDefOf.AncientCommsConsole.Size).ExpandedBy(1))
{
if (item.GetEdifice(map) != null)
{
return false;
}
}
return true;
}
private void SpawnRoomRewards(List<LayoutRoom> rooms, Map map, List<Thing> allSpawnedThings)
{
if (Def.roomRewardCrateFactor <= 0f)
{
return;
}
int num = 0;
for (int i = 0; i < allSpawnedThings.Count; i++)
{
if (allSpawnedThings[i] is Building_Crate)
{
num++;
}
}
int num2 = Mathf.RoundToInt((float)rooms.Count * Def.roomRewardCrateFactor) - num;
if (num2 <= 0)
{
return;
}
ThingSetMakerDef thingSetMakerDef = Def.rewardThingSetMakerDef ?? ThingSetMakerDefOf.Reward_ItemsStandard;
foreach (LayoutRoom item in rooms.InRandomOrder())
{
if (item.requiredDef != null)
{
continue;
}
ThingDef ancientHermeticCrate = ThingDefOf.AncientHermeticCrate;
Map map2 = map;
Func<IntVec3, bool> validator = CanSpawnAt;
if (item.TryGetRandomCellInRoom(ancientHermeticCrate, map2, out var cell, null, 2, 0, validator))
{
Building_Crate building_Crate = (Building_Crate)GenSpawn.Spawn(ThingMaker.MakeThing(ThingDefOf.AncientHermeticCrate), cell, map, Rot4.South);
List<Thing> list = thingSetMakerDef.root.Generate(default(ThingSetMakerParams));
for (int num3 = list.Count - 1; num3 >= 0; num3--)
{
Thing thing = list[num3];
if (!building_Crate.TryAcceptThing(thing, allowSpecialEffects: false))
{
thing.Destroy();
}
}
num2--;
}
if (num2 <= 0)
{
break;
}
}
bool CanSpawnAt(IntVec3 c)
{
return GenSpawn.CanSpawnAt(ThingDefOf.AncientHermeticCrate, c, map, Rot4.South, canWipeEdifices: false);
}
}
private static List<ComplexThreatDef> GetUsedThreats(LayoutRoom room)
{
int roomMergedLowestID = GetRoomMergedLowestID(room);
return usedThreatsByRoom[roomMergedLowestID];
}
private static bool TryGetRoomUsedThreats(LayoutRoom room, out List<ComplexThreatDef> threats)
{
return usedThreatsByRoom.TryGetValue(GetRoomMergedLowestID(room), out threats);
}
private static void AddUsedThreat(LayoutRoom room, ComplexThreatDef threat)
{
int roomMergedLowestID = GetRoomMergedLowestID(room);
if (!usedThreatsByRoom.TryGetValue(roomMergedLowestID, out var value))
{
value = (usedThreatsByRoom[roomMergedLowestID] = new List<ComplexThreatDef>());
}
value.Add(threat);
}
private static int GetRoomMergedLowestID(LayoutRoom room)
{
int loadId = room.loadId;
for (int i = 0; i < room.merged.Count; i++)
{
if (room.merged[i].loadId < loadId)
{
loadId = room.merged[i].loadId;
}
}
return loadId;
}
private void SpawnThreats(LayoutStructureSketch structureSketch, Map map, IntVec3 center, float threatPoints, List<Thing> spawnedThings, List<LayoutRoom> rooms)
{
ComplexResolveParams threatParams = new ComplexResolveParams
{
map = map,
complexRect = structureSketch.layoutSketch.OccupiedRect.MovedBy(center),
hostileFaction = GetFixedHostileFactionForThreats(),
allRooms = rooms,
points = threatPoints
};
StringBuilder stringBuilder = null;
if (DebugViewSettings.logComplexGenPoints)
{
stringBuilder = new StringBuilder();
stringBuilder.AppendLine("----- Logging points for " + Def.defName + ". -----");
stringBuilder.AppendLine($"Total threat points: {threatPoints}");
stringBuilder.AppendLine($"Room count: {rooms.Count}");
stringBuilder.AppendLine($"Approx points per room: {threatParams.points}");
if (threatParams.hostileFaction != null)
{
stringBuilder.AppendLine($"Faction: {threatParams.hostileFaction}");
}
}
useableThreats.Clear();
usedThreatsByRoom.Clear();
useableThreats.AddRange(Def.threats.Where((ComplexThreat t) => Rand.Chance(t.chancePerComplex)));
float num = 0f;
int num2 = 100;
while (num < threatPoints && num2 > 0)
{
num2--;
LayoutRoom room = rooms.RandomElement();
threatParams.room = room;
threatParams.spawnedThings = spawnedThings;
float b = threatPoints - num;
threatParams.points = Mathf.Min(ThreatPointsFactorRange.RandomInRange * threatPoints, b);
if (useableThreats.Where(delegate(ComplexThreat t)
{
int num3 = 0;
foreach (KeyValuePair<int, List<ComplexThreatDef>> item in usedThreatsByRoom)
{
item.Deconstruct(out var _, out var value);
List<ComplexThreatDef> list = value;
num3 += list.Count((ComplexThreatDef td) => td == t.def);
}
if (num3 >= t.maxPerComplex)
{
return false;
}
List<ComplexThreatDef> threats;
return (!TryGetRoomUsedThreats(room, out threats) || threats.Count((ComplexThreatDef td) => td == t.def) < t.maxPerRoom) && t.def.Worker.CanResolve(threatParams);
}).TryRandomElementByWeight((ComplexThreat t) => t.selectionWeight, out var result))
{
if (stringBuilder != null)
{
stringBuilder.AppendLine();
stringBuilder.AppendLine("-> Resolving threat " + result.def.defName);
}
float threatPointsUsed = 0f;
result.def.Worker.Resolve(threatParams, ref threatPointsUsed, tmpSpawnedThreatThings, stringBuilder);
num += threatPointsUsed;
AddUsedThreat(room, result.def);
}
}
if (stringBuilder != null)
{
stringBuilder.AppendLine($"Total threat points spent: {num}");
Log.Message(stringBuilder.ToString());
}
useableThreats.Clear();
usedThreatsByRoom.Clear();
}
}