File size: 5,672 Bytes
b6a38d7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
DefineClass.DistToTag = {
	__parents = { "PropertyObject", },
	properties = {
		{ category = "General", id = "Tag",  name = "Tag",  editor = "preset_id", default = false, preset_class = "PrefabTag" },
		{ category = "General", id = "Dist", name = "Dist", editor = "number", default = 0, scale = "m" },
		{ category = "General", id = "Op",   name = "Op",   editor = "choice", default = '>', items = {'>', '<'} },
	},
	EditorView = Untranslated("<Tag> <Op> <dist(Dist)>"),
}

DefineClass.PrefabTag = {
	__parents = { "Preset", },
	properties = {
		{ category = "General", id = "Persistable",      name = "Persistable",           editor = "bool",        default = false, help = "POI prefabs with such tag will try to persist their location between map generations." },
		{ category = "General", id = "TagDist",          name = "Dist To Tags",          editor = "nested_list", default = empty_table, base_class = "DistToTag", help = "Defines the distances to the border of other POI with specified tags" },
		{ category = "General", id = "TagDistStats",     name = "All Dist Stats",        editor = "text",        default = "", lines = 1, max_lines = 20, dont_save = true, read_only = true },
		{ category = "General", id = "PrefabPOI",        name = "POI Types",             editor = "text",        default = "", lines = 1, max_lines = 20, dont_save = true, read_only = true },
		{ category = "General", id = "PrefabTypes",      name = "Prefab Types",          editor = "text",        default = "", lines = 1, max_lines = 20, dont_save = true, read_only = true },
		{ category = "General", id = "Prefabs",          name = "Prefabs",               editor = "text",        default = "", lines = 1, max_lines = 30, dont_save = true, read_only = true },
	},
	EditorMenubarName = "Prefab Tags",
	EditorIcon = "CommonAssets/UI/Icons/list.png",
	EditorMenubar = "Map.Generate",
	StoreAsTable = false,
	GlobalMap = "PrefabTags",
}

function PrefabTag:GetEditorViewPresetPrefix()
	return self.Persistable and "<color 255 255 0>" or ""
end

function PrefabTag:GetEditorViewPresetPostfix()
	return self.Persistable and "</color>" or ""
end

function PrefabTag:GetTagDistStats()
	local tag_to_tag_limits = GetPrefabTagsLimits(true)
	local stats = {}
	for tag, limits in sorted_pairs(tag_to_tag_limits[self.id]) do
		local min_dist, max_dist = limits[1] or min_int, limits[2] or max_int
		if min_dist and min_dist >= 0 then
			stats[#stats + 1] = string.format("%s > %d m", tag, min_dist / guim)
		end
		if max_dist and max_dist < max_int then
			stats[#stats + 1] = string.format("%s < %d m", tag, max_dist / guim)
		end
	end
	table.sort(stats)
	return table.concat(stats, "\n")
end

function PrefabTag:GetPrefabPOI()
	local tag = self.id
	local presets = {}
	ForEachPreset("PrefabPOI", function(preset, group, tag, presets)
		local tags = preset.Tags or empty_table
		if tags[tag] then
			presets[#presets + 1] = preset.id
		end
	end, tag, presets)
	table.sort(presets)
	return table.concat(presets, "\n")
end

function PrefabTag:GetPrefabTypes()
	local tag = self.id
	local presets = {}
	ForEachPreset("PrefabType", function(preset, group, tag, presets)
		local tags = preset.Tags or empty_table
		if tags[tag] then
			presets[#presets + 1] = preset.id
		end
	end, tag, presets)
	table.sort(presets)
	return table.concat(presets, "\n")
end

function PrefabTag:GetPrefabs()
	local tag = self.id
	local presets = {}
	local markers = PrefabMarkers
	for _, marker in ipairs(markers) do
		local tags = marker.tags or empty_table
		if tags[tag] then
			presets[#presets + 1] = markers[marker]
		end
	end
	table.sort(presets)
	return table.concat(presets, "\n")
end

function PrefabTag:GetError()
	local tag_to_tag_limits = GetPrefabTagsLimits()
	for tag, limits in sorted_pairs(tag_to_tag_limits[self.id]) do
		local min_dist, max_dist = limits[1] or min_int, limits[2] or max_int
		if min_dist >= max_dist then
			return "Invalid limitst"
		end
	end
end

----

function GetPrefabTagsLimits(mirror)
	local tag_to_tag_limits = {}
	for tag1, tag_info in pairs(PrefabTags) do
		local tag_limits
		for _, entry in ipairs(tag_info.TagDist) do
			if not tag_limits then
				tag_limits = tag_to_tag_limits[tag1]
				if not tag_limits then
					tag_limits = {}
					tag_to_tag_limits[tag1] = tag_limits
				end
			end
			local tag2 = entry.Tag
			local limits = tag_limits[tag2]
			if not limits then
				limits = {}
				tag_limits[tag2] = limits
				if mirror and tag1 ~= tag2 then
					table.set(tag_to_tag_limits, tag2, tag1, limits)
				end
			end
			local dist = entry.Dist
			local op = entry.Op
			if op == '>' then
				limits[1] = Max(limits[1] or min_int, dist)
			elseif op == '<' then
				limits[2] = Min(limits[2] or max_int, dist)
			end
		end
	end
	return tag_to_tag_limits
end

function GetPrefabTagsPersistable()
	local tags = {}
	for tag, tag_info in pairs(PrefabTags) do
		if tag_info.Persistable then
			tags[tag] = true
		end
	end
	return tags
end

----

function PrefabTagsCombo()
	local tags = {}
	ForEachPreset("PrefabTag", function(preset, group, tags)
		tags[#tags + 1] = preset.id
	end, tags)
	table.sort(tags)
	return tags
end

----

AppendClass.MapDataPreset = { properties = {
	{ category = "Random Map", id = "PersistedPrefabs", editor = "prop_table", default = empty_table, no_edit = true },
	{ category = "Random Map", id = "PersistedPrefabsPreview", name = "Persisted Prefabs", editor = "text", default = "", read_only = true, lines = 1, max_lines = 10 }
}}

function MapDataPreset:GetPersistedPrefabsPreview()
	local text = {}
	for _, entry in ipairs(self.PersistedPrefabs) do
		text[#text + 1] = table.concat(entry, ", ")
	end
	return table.concat(text, "\n")
end