File size: 3,137 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
if not const.CaveTileSize then return end

DefineMapGrid("CaveGrid", const.CaveGridBits or 8, const.CaveTileSize, 64, "save_in_map")
MapVar("CavesOpened", false)

----- Open caves mode for Map Editor

if FirstLoad then
	EditorCaveBoxes = false
end

function UpdateEditorCaveBoxes()
	EditorCaveBoxes = editor.GetNonZeroInvalidationBoxes(CaveGrid)
	for idx, bx in ipairs(EditorCaveBoxes) do
		EditorCaveBoxes[idx] = bx * const.CaveTileSize
	end
end

function EditorSetCavesOpen(open)
	if not EditorCaveBoxes then
		UpdateEditorCaveBoxes()
	end
	
	CavesOpened = open
	Msg("EditorCavesOpen", open)
	if open then
		local grid = IsEditorActive() and CaveGrid or DiscoveredCavesGrid
		terrain.SetTerrainHolesBaseGridRect(grid, EditorCaveBoxes)
	else
		terrain.ClearTerrainHolesBaseGrid(EditorCaveBoxes)
	end
	
	local statusbar = GetDialog("XEditorStatusbar")
	if statusbar then
		statusbar:ActionsUpdated()
	end
end

-- Messages 

function OnMsg.LoadGame()
	-- Open caves based on the loaded map var
	if CavesOpened then
		EditorSetCavesOpen(true)
	end
end

function OnMsg.ChangeMapDone(map)
	if map ~= "" then
		EditorCaveBoxes = false
		EditorSetCavesOpen(false)
	end
end

function OnMsg.GameExitEditor()
	EditorSetCavesOpen(false)
end

----- Cave brush

local hash_color_multiplier = 10 -- for a more diverse color palette
local function RandCaveColor(idx)
	return RandColor(xxhash(idx * hash_color_multiplier))
end

DefineClass.XCaveBrush = {
	__parents = { "XMapGridAreaBrush" },
	
	GridName = "CaveGrid",
	
	ToolSection = "Terrain",
	ToolTitle = "Caves",
	Description = {
		"Defines the cave areas on the map.",
		"(<style GedHighlight>Ctrl-click</style> to select & lock areas)\n" ..
		"(<style GedHighlight>Shift-click</style> to select entire caves)\n" ..
		"(<style GedHighlight>Alt-click</style> to get cave value at cursor)"
	},
	ActionSortKey = "23",
	ActionIcon = "CommonAssets/UI/Editor/Tools/Caves.tga", 
	ActionShortcut = "C",
}

function XCaveBrush:GetGridPaletteItems()
	local white = "CommonAssets/System/white.dds"
	local items = {}
	local grid_values = editor.GetUniqueGridValues(_G[self.GridName], MapGridTileSize(self.GridName), const.MaxCaves)
	local max_val = 0
	
	table.insert(items, { text = "Blank", value = 0, image = white, color = RGB(0, 0, 0) })
	for _, val in ipairs(grid_values) do
		if val ~= 0 then
			table.insert(items, {
				text = string.format("Cave %d", val),
				value = val,
				image = white,
				color = RandCaveColor(val)
			})
			if max_val < val then
				max_val = val
			end
		end
	end
	
	table.insert(items, { text = "New Cave...", value = max_val + 1, image = white, color = RandCaveColor(max_val + 1) })
	return items
end

function XCaveBrush:GetPalette()
	local palette = { [0] = RGB(0, 0, 0) }
	for i = 1, 254 do
		palette[i] = RandCaveColor(i)
	end
	palette[255] = RGBA(255, 255, 255, 128)
	return palette
end

function OnMsg.OnMapGridChanged(name, bbox)
	local brush = XEditorGetCurrentTool()
	if name == "CaveGrid" and IsKindOf(brush, "XCaveBrush") then
		if CavesOpened then
			table.insert(EditorCaveBoxes, bbox)
			terrain.SetTerrainHolesBaseGridRect(CaveGrid, bbox)
		end
	end
end