File size: 4,328 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 |
-- Sample transitional terrains declaration. Note that the transitional has greater id than the 2 that use it.
--- Returns the index of the terrain giving its name as input.
-- @cstyle int GetTerrainTextureIndex(string nameTerrain).
-- @param nameTerrain string. the name of the terrain.
-- @return int. index of the terrain (nil if invalid terrain name is given).
if FirstLoad then
TerrainTextures = {}
TerrainNameToIdx = {}
end
--- Returns the index of the terrain giving its name as input.
-- @param nameTerrain string. the name of the terrain.
-- @return int. index of the terrain (nil if invalid terrain name is given).
function GetTerrainTextureIndex(nameTerrain)
return TerrainNameToIdx[nameTerrain]
end
---
--- Returns the terrain texture preview image for the given terrain name.
---
--- @param nameTerrain string The name of the terrain.
--- @return boolean|userdata The terrain texture preview image, or false if the terrain name is invalid.
function GetTerrainTexturePreview(nameTerrain)
local idx = GetTerrainTextureIndex(nameTerrain)
return idx and TerrainTextures[idx] and GetTerrainImage(TerrainTextures[idx].basecolor) or false
end
---
--- Returns a combo box containing all the terrain names.
---
--- @return table A table containing the terrain names.
function GetTerrainNamesCombo()
return PresetsCombo("TerrainObj", false, "")
end
----
if FirstLoad then
suspendReasons = {}
end
---
--- Suspends terrain invalidations for the given reason.
---
--- If there are no other reasons to suspend terrain invalidations and the map is not empty, this function will suspend terrain invalidation.
--- The given reason is stored in the `suspendReasons` table, and terrain invalidation will remain suspended until `ResumeTerrainInvalidations` is called with the same reason.
---
--- @param reason string|boolean The reason for suspending terrain invalidations, or `false` if no specific reason is given.
function SuspendTerrainInvalidations(reason)
reason = reason or false
if next(suspendReasons) == nil and GetMap() ~= "" then
terrain.SuspendInvalidation()
end
suspendReasons[reason] = true
end
---
--- Resumes terrain invalidations that were previously suspended.
---
--- If there are no other reasons to suspend terrain invalidations and the map is not empty, this function will resume terrain invalidation.
--- The given reason is removed from the `suspendReasons` table, and terrain invalidation will resume if there are no other reasons to suspend it.
---
--- @param reason string|boolean The reason for suspending terrain invalidations, or `false` if no specific reason was given.
--- @param reload boolean If true, forces a reload of the terrain textures.
function ResumeTerrainInvalidations(reason, reload)
reason = reason or false
suspendReasons[reason] = nil
if next(suspendReasons) == nil and GetMap() ~= "" then
if reload then
hr.TR_ForceReloadNoTextures = 1
end
terrain.ResumeInvalidation()
end
end
----
if FirstLoad then
activeThread = false
end
---
--- Schedules a terrain reload to occur in 3 seconds.
---
--- If there is no active thread for reloading the terrain, this function will create a new real-time thread that will sleep for 2.8 seconds and then force a reload of the terrain textures.
---
--- @function ScheduleReloadTerrain
--- @return nil
function ScheduleReloadTerrain()
if not IsValidThread(activeThread) then
print("The terrain will be reloaded in 3 sec.")
activeThread = CreateRealTimeThread(function()
Sleep(2800)
hr.TR_ForceReloadTextures = true
activeThread = false
end)
end
end
--[==[
local step = const.TypeTileSize
local map = box(0, 0, terrain.GetMapWidth() - 1, terrain.GetMapHeight() - 1)
local typestats = {}
for j = map:miny(), map:maxy(), step do
for i = map:minx(), map:maxx(), step do
local type = terrain.GetTerrainType(point(i,j))
if type then
typestats[type] = typestats[type] and typestats[type] + 1 or 1
end
end
end
print("------------------------------------------------------------")
print("Terrain test:")
for type, texture in sorted_pairs(TerrainTextures) do
local count = typestats[type] or 0
print("Texture " .. texture.id .. " used " .. count .. " times.")
end
]==] |