Description:
Improve contract spawning.
Commit status:
[Not Reviewed]
References:
Diff options:
Comments:
0 Commit comments
0 Inline Comments
Unresolved TODOs:
There are no unresolved TODOs
|
1 | NO CONTENT: modified file, binary diff hidden |
|
1 | NO CONTENT: modified file, binary diff hidden |
@@ -1,6 +1,8 | |||
|
1 | 1 | using System; |
|
2 | using System.Linq; | |
|
2 | 3 | using System.Collections.Generic; |
|
3 | 4 | using Encompass; |
|
5 | using JM.LinqFaster; | |
|
4 | 6 | |
|
5 | 7 | using Microsoft.Xna.Framework; |
|
6 | 8 | |
@@ -10,11 +12,12 | |||
|
10 | 12 | namespace isometricparkfna.Spawners { |
|
11 | 13 | |
|
12 | 14 | [Receives(typeof(SpawnContractMessage))] |
|
13 | [Reads(typeof(AreaComponent))] | |
|
15 | [Reads(typeof(AreaComponent), typeof(ContractStatusComponent))] | |
|
14 | 16 | class ContractSpawner : Spawner<SpawnContractMessage> |
|
15 | 17 | { |
|
16 | 18 | private Random random_generator; |
|
17 | 19 | |
|
20 | public const int DEFAULT_MIN_SQUARES = 10; | |
|
18 | 21 | public const int DEFAULT_SQUARES = 25; |
|
19 | 22 | public const int CONTRACT_MINIMUM = 50; |
|
20 | 23 | public const int CONTRACT_MAXIMUM = 250; |
@@ -63,10 +66,11 | |||
|
63 | 66 | attempts += 1; |
|
64 | 67 | } |
|
65 | 68 | } |
|
66 | squares.AddRange(squares_to_add); | |
|
69 | var remaining = max_size - squares.Count; | |
|
70 | squares.AddRange(squares_to_add.Take(remaining)); | |
|
67 | 71 | squares_to_add.Clear(); |
|
68 | 72 | |
|
69 | if (attempts >= maxAttempts) | |
|
73 | if (attempts >= maxAttempts && squares.Count < max_size) | |
|
70 | 74 | { |
|
71 | 75 | System.Console.WriteLine(string.Format("Failed to generate enough squares. {0} were requested, only {1} were found ({2} attempts)", max_size, squares.Count, attempts)); |
|
72 | 76 | break; |
@@ -77,34 +81,44 | |||
|
77 | 81 | |
|
78 | 82 | protected override void Spawn(in SpawnContractMessage message) |
|
79 | 83 | { |
|
80 | var contract = CreateEntity(); | |
|
81 | 84 | |
|
82 |
|
|
|
83 |
|
|
|
85 | //for now: | |
|
86 | var occupied = new List<Vector2>(); | |
|
84 | 87 | |
|
85 | foreach(ref readonly var entity in ReadEntities<AreaComponent>()) | |
|
86 | { | |
|
87 | var entitySquares = GetComponent<AreaComponent>(entity).squares; | |
|
88 | occupied.AddRange(entitySquares); | |
|
89 | } | |
|
88 | foreach (var (entity, status) in ReadEntities<AreaComponent>().SelectWhereF((e) => (e, GetComponent<ContractStatusComponent>(e)), | |
|
89 | (e) => (e.Item2.status != ContractStatus.Expired)) | |
|
90 | ) | |
|
91 | { | |
|
92 | var entitySquares = GetComponent<AreaComponent>(entity).squares; | |
|
93 | occupied.AddRange(entitySquares); | |
|
94 | } | |
|
90 | 95 | |
|
91 |
|
|
|
92 |
|
|
|
96 | var start_x = random_generator.Next(0, 50); | |
|
97 | var start_y = random_generator.Next(0, 50); | |
|
98 | ||
|
99 | int max_squares = (message.max_squares == 0) ? DEFAULT_SQUARES : message.max_squares; | |
|
100 | int min_squares = (message.min_squares == null) ? DEFAULT_MIN_SQUARES : (int)message.min_squares; | |
|
101 | Vector2[] squares = (message.squares == null) ? CreateArea(start_x, start_y, max_squares, new HashSet<Vector2>(occupied)) : message.squares; | |
|
93 | 102 | |
|
94 | int max_squares = (message.max_squares == 0) ? DEFAULT_SQUARES : message.max_squares; | |
|
95 | Vector2[] squares = (message.squares == null) ? CreateArea(start_x, start_y, max_squares, new HashSet<Vector2>(occupied)) : message.squares; | |
|
103 | if (squares.Length > min_squares) | |
|
104 | { | |
|
105 | var contract = CreateEntity(); | |
|
96 | 106 | |
|
97 |
|
|
|
107 | int contract_amount = random_generator.Next(CONTRACT_MINIMUM, CONTRACT_MAXIMUM); | |
|
98 | 108 | |
|
99 | // AddComponent | |
|
100 | AddComponent(contract, new AreaComponent { squares = squares }); | |
|
101 | AddComponent(contract, new NameComponent { DisplayName = message.name }); | |
|
102 | AddComponent(contract, new SelectedComponent { selected = false }); | |
|
103 | AddComponent(contract, new ContractStatusComponent { status = ContractStatus.Proposed, date = this.simulation.DateTime }); | |
|
104 |
|
|
|
105 |
AddComponent(contract, new BudgetLineComponent |
|
|
106 | //Round to the nearest $5 | |
|
107 | amount = (decimal)((contract_amount/5)*5) }); | |
|
109 | // AddComponent | |
|
110 | AddComponent(contract, new AreaComponent { squares = squares }); | |
|
111 | AddComponent(contract, new NameComponent { DisplayName = message.name }); | |
|
112 | AddComponent(contract, new SelectedComponent { selected = false }); | |
|
113 | AddComponent(contract, new ContractStatusComponent { status = ContractStatus.Proposed, date = this.simulation.DateTime }); | |
|
114 | AddComponent(contract, new TreeDeltaComponent { deltaTrees = -1 }); | |
|
115 | AddComponent(contract, new BudgetLineComponent | |
|
116 | { | |
|
117 | category = "Contracts", | |
|
118 | //Round to the nearest $5 | |
|
119 | amount = (decimal)((contract_amount / 5) * 5) | |
|
120 | }); | |
|
121 | } | |
|
108 | 122 | |
|
109 | 123 | } |
|
110 | 124 | } |
@@ -7,6 +7,8 | |||
|
7 | 7 | { |
|
8 | 8 | public Vector2[] squares; |
|
9 | 9 | public string name; |
|
10 |
|
|
|
10 | public int max_squares; | |
|
11 | ||
|
12 | public int? min_squares; | |
|
11 | 13 | } |
|
12 | 14 | } |
@@ -194,6 +194,9 | |||
|
194 | 194 | </Reference> |
|
195 | 195 | <Reference Include="System.Data.DataSetExtensions" /> |
|
196 | 196 | <Reference Include="System.Xml" /> |
|
197 | <Reference Include="JM.LinqFaster"> | |
|
198 | <HintPath>..\packages\JM.LinqFaster.1.1.2\lib\net461\JM.LinqFaster.dll</HintPath> | |
|
199 | </Reference> | |
|
197 | 200 | </ItemGroup> |
|
198 | 201 | <ItemGroup> |
|
199 | 202 | <Folder Include="Utils\" /> |
@@ -2,8 +2,9 | |||
|
2 | 2 | <packages> |
|
3 | 3 | <package id="Collections.Pooled" version="1.0.82" targetFramework="net461" /> |
|
4 | 4 | <package id="ImGui.NET" version="1.78.0" targetFramework="net472" /> |
|
5 |
<package id=" |
|
|
6 |
<package id=" |
|
|
5 | <package id="JM.LinqFaster" version="1.1.2" targetFramework="net461" /> | |
|
6 | <package id="morelinq" version="3.3.2" targetFramework="net461" /> | |
|
7 | <package id="Newtonsoft.Json" version="13.0.1" targetFramework="net461" /> | |
|
7 | 8 | <package id="System.Buffers" version="4.5.1" targetFramework="net461" /> |
|
8 | 9 | <package id="System.Memory" version="4.5.4" targetFramework="net461" /> |
|
9 | 10 | <package id="System.Numerics.Vectors" version="4.5.0" targetFramework="net461" /> |
@@ -11,7 +12,7 | |||
|
11 | 12 | <package id="System.Security.AccessControl" version="5.0.0" targetFramework="net461" /> |
|
12 | 13 | <package id="System.Security.Permissions" version="5.0.0" targetFramework="net461" /> |
|
13 | 14 | <package id="System.Security.Principal.Windows" version="5.0.0" targetFramework="net461" /> |
|
14 |
<package id="System.ValueTuple" version="4. |
|
|
15 | <package id="System.ValueTuple" version="4.5.0" targetFramework="net461" /> | |
|
15 | 16 | <package id="Tracery.Net" version="1.0.0" targetFramework="net461" /> |
|
16 |
<package id="YamlDotNet" version=" |
|
|
17 | <package id="YamlDotNet" version="11.1.1" targetFramework="net461" /> | |
|
17 | 18 | </packages> No newline at end of file |
You need to be logged in to leave comments.
Login now