Merge branch 'fixes' into main/gingo-test
[ryzomcore.git] / ryzom / common / data_common / r2 / r2_environment.lua
blob3764c710d13dd4c713568dc33371df1517d27158
1 -- main tables of environement & general editor functions
3 ---------------
4 -- FUNCTIONS --
5 ---------------
7 ------------------------------------------------------------------------------------------------------------
8 -- Helper : eval a property in an instance of a class :
9 -- if the property is a value, then that value is returned
10 -- if the property is a function, then the 'this' parameter is passed to the function and the result is returned
11 -- if the property is nil, then the defualt value is returned
12 function r2:evalProp(prop, this, defaultValue)
13 if type(prop) == "function" then return prop(this) -- could have been this:prop(), too...
14 elseif prop ~= nil then return prop
15 else return defaultValue
16 end
17 end
19 -------------------------------------------------------------------------------------
20 -- get translation id for a property, followed by a boolean (false if not found)
21 function r2:getPropertyTranslationId(prop)
22 local translationId
23 local found = true
24 if prop.Translation ~= nil then
25 translationId = prop.Translation
26 elseif i18n.hasTranslation('uiR2EDProp_' .. prop.Name) then
27 translationId = 'uiR2EDProp_' .. prop.Name
28 elseif i18n.hasTranslation('uiR2ED' .. prop.Name) then
29 translationId = 'uiR2ED' .. prop.Name
30 else
31 translationId = prop.Name
32 found = false
33 end
34 return translationId, found
35 end
37 -------------------------------------------------------------------------------------
38 -- get translation id for a property, as an ucstring
39 function r2:getPropertyTranslation(prop)
40 local translationId = r2:getPropertyTranslationId(prop)
41 if (i18n.hasTranslation(translationId)) then
42 return i18n.get(translationId)
43 else
44 return ucstring(translationId)
45 end
46 end
49 ------------------------------------------------------------------------------------------------------------
50 -- return the left quota for the current scenario (called by C++)
51 function r2:getLeftQuota()
52 return r2.ScenarioWindow:getLeftQuota()
53 end
56 ------------------------------------------------------------------------------------------------------------
57 -- retrieve class description of an instance (in the r2.Classes table)
58 function r2:getClass(instance)
59 if instance == nil or instance.isNil then
60 debugInfo("Calling r2:getClass on nil instance")
61 debugInfo(debug.traceback())
62 return
63 end
64 if instance.Class == nil then
65 debugInfo("Calling r2:getClass on class with no 'Class' field")
66 debugInfo(debug.traceback())
67 return
68 end
69 return r2.Classes[instance.Class]
70 end
72 ------------------------------------------------------------------------------------------------------------
73 -- get parent instance of an object in the editor (that is an object with an InstanceId)
74 function r2:getParentInstance(object)
75 debugInfo(debug.traceback())
76 assert(nil) -- deprecated : use special member 'ParentInstance' instead
77 --if object == nil then return nil end
78 --local parent = object.Parent
79 --while parent ~= nil do
80 -- if parent.InstanceId ~= nil then
81 -- return parent
82 -- end
83 -- parent = parent.Parent
84 --end
85 --return nil
86 end
88 ------------------------------------------------------------------------------------------------------------
89 -- get max number of acts in a scenario
90 function r2:getMaxNumberOfAdditionnalActs()
91 return getDefine("r2ed_max_num_additionnal_acts")
92 end
94 ------------------------------------------------------------------------------------------------------------
95 -- Explore a tree of instance, each instance that has a sheet id is append to the list
96 function r2:exploreInstanceTree(obj, destTable)
97 if obj.InstanceId ~= nil then
98 table.insert(destTable, obj)
99 end
100 for k, v in specPairs(obj) do
101 if type(v) == "userdata" and v.Size ~= nil then
102 -- this is a sub array
103 r2:exploreInstanceTree(v, destTable)
109 -- test from a sheet id if an object is a bot object
110 function r2:isBotObject(sheetClient)
111 return getCharacterSheetSkel(sheetClient, false) == ""
112 or string.match(sheetClient, "object_[%w_]*%.creature") -- patch for bot objects (with skeletons -> wind turbine)
115 -- helper function for pasting : relocate the 'Position' field of 'dest' for proper pasting
116 function r2:relocatePos(dest)
117 local x, y, z = r2:findEmptyPlace(dest.Position.x, dest.Position.y)
118 if x ~= nil then
119 dest.Position.x = x
120 dest.Position.y = y
121 dest.Position.z = z
125 -- Get a new position were to paste an object
126 function r2:getPastePosition()
127 local x, y, z = r2:getUserEntityPosition()
128 local fx, fy = r2:getUserEntityFront()
129 x = x + fx * 4
130 y = y + fy * 4
131 x = x + 3 * math.random(-100, 100) / 100
132 y = y + 3 * math.random(-100, 100) / 100
133 local nx, ny = r2:findEmptyPlace(x, y)
134 if nx ~= nil then
135 return nx, ny, z
136 else
137 return r2:getUserEntityPosition() -- no empty place found, paste on user
138 end
142 ------------------------------------------------------------------------------------------------------------------
143 --------------------
144 -- INIT / GLOBALS --
145 --------------------
147 --debugInfo("Initializing main tables")
148 if r2 == nil then
149 r2 = {} -- for vianney's tests (is initialized by the caller otherwise)
152 ------------------
153 -- MISC GLOBALS --
154 ------------------
156 r2.ScratchUCStr = ucstring() -- scratch ucstring, useful to do call from utf8 without to create a new object
158 ---------------------
159 -- EDITION GLOBALS --
160 ---------------------
162 r2.maxId = {}
164 -- define in r2_features.lua
165 r2.Features={}
167 -- define in r2_basic_bricks.lua
168 r2.BasicBricks = {}
170 -- define in r2_palette.lua
171 r2.Palette = {}
173 -- definition of all R2 classes (contains both basic components and components of features)
174 r2.Classes = {}
176 -- current content of the clipboard after the selection has been copied
177 r2.ClipBoard = nil
178 r2.ClipBoardSrcInstanceId = nil
179 r2.ClipBoardDisplayName = nil
182 -----------------------
183 -- ANIMATION GLOBALS --
184 -----------------------
186 -- contains var related to animation
187 r2.AnimGlobals =
189 Acts = nil, -- set to nil until received by server, contains
190 -- the list of acts accessible at runtime by the animator
191 UserTriggers = nil, -- set to nil until received by server, contains
192 -- the list of triggers that an animator can fire at runtime
194 -------------------------------------------------------------------------------------
195 reset = function(this)
196 this.Acts = nil
197 this.UserTriggers = nil