Starsector API
Loading...
Searching...
No Matches
ProcgenUsedNames.java
Go to the documentation of this file.
1package com.fs.starfarer.api.impl.campaign.procgen;
2
3import java.util.Collection;
4import java.util.HashSet;
5import java.util.Set;
6
7import com.fs.starfarer.api.Global;
8import com.fs.starfarer.api.impl.campaign.procgen.MarkovNames.MarkovNameResult;
9import com.fs.starfarer.api.impl.campaign.procgen.StarSystemGenerator.LagrangePointType;
10import com.fs.starfarer.api.util.WeightedRandomPicker;
11
12public class ProcgenUsedNames {
13
14 public static class NamePick {
15 public NameGenData spec;
16 public String nameWithRomanSuffixIfAny;
17 public String secondaryWithRomanSuffixIfAny;
18 public NamePick(NameGenData spec, String nameWithRomanSuffixIfAny, String secondaryWithRomanSuffixIfAny) {
19 this.spec = spec;
20 this.nameWithRomanSuffixIfAny = nameWithRomanSuffixIfAny;
21 this.secondaryWithRomanSuffixIfAny = secondaryWithRomanSuffixIfAny;
22 }
23
24 }
25
26 public static final String KEY = "ProcgenUsedNames_key";
27 private Set<String> names = new HashSet<String>();
28
29 public static void notifyUsed(String name) {
30 getUsed().names.add(name);
31 }
32
33 public static boolean isUsed(String name) {
34 return getUsed().names.contains(name);
35 }
36
37 public static ProcgenUsedNames getUsed() { //for (String name : ((ProcgenUsedNames) test).names) System.out.println(name);
38 Object test = Global.getSector().getPersistentData().get(KEY);
39 if (test == null) {
40 test = new ProcgenUsedNames();
41 Global.getSector().getPersistentData().put(KEY, test);
42 }
43 return (ProcgenUsedNames) test;
44 }
45
46
47 public static NamePick pickName(String tag, String parent, LagrangePointType lagrangePoint) {
48 WeightedRandomPicker<NamePick> picker = new WeightedRandomPicker<NamePick>(StarSystemGenerator.random);
49
50
51 Collection<NameGenData> all = Global.getSettings().getAllSpecs(NameGenData.class);
52
53 // names for child of parent, if any
54 if (parent != null) {
55 for (NameGenData spec : all) {
56 if (isUsed(spec.getName())) continue;
57 if (!spec.hasTag(tag)) continue;
58 if (parent == null && spec.getName().contains("$parent")) continue;
59 if (spec.hasParent(parent)) {
60 picker.add(new NamePick(spec, spec.getName(), spec.getSecondary()), spec.getFrequency());
61 }
62 }
63 }
64
65 // if needed, add names w/o parent
66 if (picker.isEmpty()) {
67 for (NameGenData spec : all) {
68 if (isUsed(spec.getName())) continue;
69 if (!spec.hasTag(tag)) continue;
70 if (!spec.getParents().isEmpty()) continue;
71 if (parent == null && spec.getName().contains("$parent")) continue;
72 picker.add(new NamePick(spec, spec.getName(), spec.getSecondary()), spec.getFrequency());
73 }
74 }
75
76 // if there's nothing, try to create a name using markov chains
77 // before moving on to roman numerals
78 if (picker.isEmpty()) {
79 int attempts = 10;
80 for (int i = 0; i < attempts; i++) {
81 MarkovNameResult name = MarkovNames.generate(picker.getRandom());
82 if (name == null || name.name == null) continue;
83 if (isUsed(name.name)) continue;
84
85 NameGenData data = new NameGenData(name.name, null);
86 NamePick pick = new NamePick(data, name.name, null);
87 return pick;
88 }
89 }
90
91
92
93 // if still no names, we're out of names. start adding roman numerals.
94 // (or there's nothing at all for the tag, but that's a bug elsewhere)
95 if (picker.isEmpty()) {
96 OUTER: for (Object obj : all) {
97 NameGenData spec = (NameGenData) obj;
98 if (!spec.hasTag(tag)) continue;
99
100 String base = spec.getName();
101 for (int i = 2; i < 4000; i++) {
102 String name = base + " " + Global.getSettings().getRoman(i);
103 if (isUsed(name)) continue;
104 if (parent == null && spec.getName().contains("$parent")) continue;
105
106 String secondary = null;
107 if (spec.getSecondary() != null) {
108 secondary = spec.getSecondary() + " " + Global.getSettings().getRoman(i);
109 }
110 picker.add(new NamePick(spec, name, secondary), (4000f - i) * spec.getFrequency()); // lower numbers more likely to be picked
111 continue OUTER;
112 }
113 }
114 }
115
116 NamePick pick = picker.pick();
117
118 if (pick != null) {
119 pick.nameWithRomanSuffixIfAny = doTokenReplacement(pick.nameWithRomanSuffixIfAny, parent, lagrangePoint);
120 if (pick.secondaryWithRomanSuffixIfAny != null) {
121 pick.secondaryWithRomanSuffixIfAny = doTokenReplacement(pick.secondaryWithRomanSuffixIfAny, parent, lagrangePoint);
122 }
123
124 }
125
126 return pick;
127 }
128
129 public static String doTokenReplacement(String name, String parent, LagrangePointType lagrange) {
130 if (parent != null) {
131 name = name.replaceAll("\\$parent", parent);
132 name = name.replaceAll("s's", "s'");
133 }
134 if (lagrange != null) {
135 name = name.replaceAll("\\$L", lagrange.name());
136 } else {
137 name = name.replaceAll("\\$L ", "");
138 }
139 return name;
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
static SettingsAPI getSettings()
Definition Global.java:51
static SectorAPI getSector()
Definition Global.java:59
static MarkovNameResult generate(Random random)
static String doTokenReplacement(String name, String parent, LagrangePointType lagrange)
static NamePick pickName(String tag, String parent, LagrangePointType lagrangePoint)
< T > Collection< T > getAllSpecs(Class< T > c)