File size: 20,982 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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
RecursiveCallMethods.OnModifiableValueChanged = "call"

DefineClass.Modifiable = {
	__parents = { "ContinuousEffectContainer" },
	
	-- Each modifiable property has modifiable = true in its property definition; it must not have Get<prop>/Set<prop> methods
	-- A base_<prop> member is autogenerated, it is initialized with the modifiable prop's default value
	
	-- Usage: To add/remove modifiers, create ObjectModifier/MultipleObjectsModifier Modifier objects.
	--   You must not directly call UpdateModifier, the Modifier objects above call it internally when needed.
	--   Modifier objects will apply the modifier when created, remove it when destroyed, and provide TurnOff/TurnOn methods.
	--   Important members you need to set when creating a Modifier object:
	--     a) prop                      - name of the property affected
	--     b) target(s)                 - specifies the object(s) to be modified
	--     c) add (optional)            - additive modifier (applied first)
	--     d) mul (optional)            - multiplicative modifier, base 1000
	--     e) subobject_path (optional) - path to subobject to modify; { key1, key2 } will affect obj[key1][key2]
	
	-- SetBase("property_name", value) function is provided
	-- It can be used to set the base_<prop> value, which is the value upon which modifications are applied.
	-- This recalculates the modifiable property.
	
	-- OnModifiableValueChanged(prop) callback is provided
	-- It is called whenever a modifiable property's value changed, either due to modifiers or the base changing.
	-- It is a part of the RecursiveCallMethods group, hence all of a classes' parents implementations will get called, including the classes' own implementation.
	-- (the parents' implementations are called first)
	
	-- The calculation performed by modifiers is provided by the Modifier class and can be overridden
	-- The default Modifier class uses parameters called mul and add and performs
	--	  value = MulDivRound(base_value, mul, 1000) + add
	-- with multiplicative modifications all multiplied together.

	-- For each property we keep a table with accumulated Modifier parameters as named members, and all contributing modifiers with their own ModCalc params in the array part
	-- e.g. for the default mod/add ModCalc:
	--   {
	--		mul = 4000,
	--		add = -700,
	--		{ id = "some_upgrade", mul = 2000, add = 0, },
	--		{ id = "some_tech", mul = 2000, add = -200, },
	--		{ id = "some_penalty", mul = 1000, add = -500, },
	--	 }
	
	-- PropModifiers have the following format (note that "mul" and "add" are not hardcoded, but inherited from Modifier):
	--   { id = <modification_id>, prop = <property_id>, display_name = <display_name>, mul = <multiplicative_modifier>, add = <additive_modifier> }
	--
	
	modifications = false,
}

local remove = table.remove
local ifind = table.ifind
local function ChangeValue(self, prop, value)
	local old_value = self[prop]
	if old_value ~= value then
		self[prop] = value
		-- go over posted messages and check if they are for the same object and property
		local prev_msg, index = ifind(PostMsgList, "OnModifiableValueChanged", self, prop)
		if prev_msg then
			old_value = prev_msg[4]
			remove(PostMsgList, index)
		end
		if old_value ~= value then
			PostMsg("OnModifiableValueChanged", self, prop, old_value, value)
		end
	end
end

function OnMsg.OnModifiableValueChanged(obj, prop, old_value, value)
	procall(obj.OnModifiableValueChanged, obj, prop, old_value, value)
end

function Modifiable:AddModifier(id, prop, ...)
	local modifier = Modifier:ModCreate(...)
	if not modifier then return end
	modifier.id = id or nil
	self:AddModifierObj(modifier, prop)
	return modifier
end

function Modifiable:AddModifierObj(modifier, prop)
	prop = prop or modifier.prop
	local modifications = self.modifications
	if not modifications then
		modifications = {}
		self.modifications = modifications
	end
	local modification_list = modifications[prop]
	if not modification_list then
		local prop_meta = self:GetPropertyMetadata(prop)
		if prop_meta then
			modification_list = Modifier:new{ min = prop_meta.min or nil, max = prop_meta.max or nil }
		else
			modification_list = Modifier:new()
		end
		modifications[prop] = modification_list
	end
	
	if modifier.id then
		table.remove_entry(modification_list, "id", modifier.id)
	end
	if modifier.container then
		table.remove_entry(modification_list, "container", modifier.container)
	end
	modification_list[#modification_list + 1] = modifier
	
	modification_list:ModAccumulate()
	return ChangeValue(self, prop, modification_list:ModApply(self["base_" .. prop]))
end

function Modifiable:RemoveModifier(id, prop)
	local modifications = self.modifications
	local modification_list = modifications and modifications[prop]
	if not modification_list or not table.remove_entry(modification_list, "id", id) then return end
	
	local value = self["base_" .. prop]
	if #modification_list > 0 then
		modification_list:ModAccumulate()
		value = modification_list:ModApply(value)
	else
		modifications[prop] = nil
	end
	return ChangeValue(self, prop, value)
end

function Modifiable:RemoveModifierObj(modifier, prop)
	prop = prop or modifier.prop
	local modifications = self.modifications
	local modification_list = modifications and modifications[prop or false]
	if not modification_list then return end
	if not table.remove_entry(modification_list, modifier) and (not modifier.container or not table.remove_entry(modification_list, "container", modifier.container)) then
		return
	end
	
	local value = self["base_" .. prop]
	if #modification_list > 0 then
		modification_list:ModAccumulate()
		value = modification_list:ModApply(value)
	else
		modifications[prop] = nil
	end
	return ChangeValue(self, prop, value)
end

function Modifiable:ChangedModifier(prop)
	local modifications = self.modifications
	local modification_list = modifications and modifications[prop or false]
	if not modification_list then return end

	modification_list:ModAccumulate()
	return ChangeValue(self, prop, modification_list:ModApply(self["base_" .. prop]))
end

function Modifiable:ModifyValue(value, prop) -- apply modifiers of a prop to a value
	local modifications = self.modifications
	local modification_list = modifications and modifications[prop]
	if modification_list then
		value = modification_list:ModApply(value)
	end
	return value
end

function Modifiable:GetBase(prop)
	return self["base_" .. prop]
end

function Modifiable:SetBase(prop, value, base_prop)
	base_prop = base_prop or "base_" .. prop
	local base_value = self[base_prop]
	if base_value == value then return end
	self[base_prop] = value
	local modifications = self.modifications
	local modification_list = modifications and modifications[prop]
	if modification_list then
		value = modification_list:ModApply(value)
	end
	return ChangeValue(self, prop, value)
end

function Modifiable:AddBase(prop, value)
	if value ~= 0 then
		self:SetBase(prop, value + self["base_" .. prop])
	end
end

function Modifiable:GetClassValue(prop)
	return getmetatable(self)[prop]
end

function Modifiable:RestoreBase(prop)
	local base_prop = "base_" .. prop
	if rawget(self, base_prop) == nil then return end
	self:SetBase(prop, nil, base_prop)
	return true
end

function Modifiable:RestoreModifiableValue(prop)
	local value = self:GetClassValue(prop)
	local modifications = self.modifications
	local modification_list = modifications and modifications[prop]
	if modification_list then
		value = modification_list:ModApply(value)
	end
	return ChangeValue(self, prop, value)
end

function Modifiable:GetPropertyModifierTexts(prop)
	local modifications = self.modifications
	if not modifications then return empty_table end
	local modification_list = modifications[prop]
	if not modification_list then return empty_table end

	local mod_texts = {}
	for _, mod in ipairs(modification_list) do
		if mod.display_text then
			mod_texts[#mod_texts + 1] = T{mod.display_text, mod}
		end
	end
	return mod_texts
end

function Modifiable:ModifierById(id, prop)
	local modifications = self.modifications
	if not modifications then return false end
	if prop then
		local modification_list = modifications[prop]
		return modification_list and table.find_value(modification_list, "id", id)
	else
		for _, modification_list in pairs(modifications) do
			local mod = table.find_value(modification_list, "id", id)
			if mod then
				return mod
			end
		end
	end
end

Modifiable.OnModifiableValueChanged = empty_func

function OnMsg.ClassesPostprocess()
	ClassDescendants("Modifiable", function (name, class)
		for _, prop_meta in ipairs(class.properties) do
			if prop_meta.modifiable and prop_meta.editor == "number" then
				local prop_id = prop_meta.id
				local value = rawget(class, prop_id)
				if value ~= nil then
					rawset(class, "base_" .. prop_id, value)
				end
			end
		end
	end)
end

if Platform.developer then
	function OnMsg.ClassesPreprocess(classdefs)
		for name, def in pairs(classdefs) do
			for _, meta in ipairs(def.properties) do
				if meta.modifiable then
					local prop = meta.id
					--print(name, prop)
					if def["Get" .. prop] or def["Set" .. prop] then
						printf("Class %s should not have Get/Set accessor functions for the modifiable property %s", name, prop)
					end
				end
			end
		end
	end
end


----- Modifier

DefineClass.Modifier = {
	__parents = { "PropertyObject" },
	display_text = "",
	id = "",
	prop = false,

	mul = 1000,
	add = 0,
	min = false,
	max = false,
	add_min = 0,
	add_max = 0,
}

function Modifier:ModCreate(mul, add, text, add_min, add_max, min, max)
	if (mul or 1000) == 1000 and (add or 0) == 0 and (add_min or 0) == 0 and (add_max or 0) == 0 then return end
	local modifier = self:new()
	modifier:ModSet(mul, add, text, add_min, add_max, min, max)
	return modifier
end

function Modifier:ModSet(mul, add, text, add_min, add_max, min, max)
	self.mul = mul ~= 1000 and mul or nil
	self.add = add ~= 0 and add or nil
	self.display_text = text ~= "" and text or nil
	self.add_min = add_min ~= 0 and add_min or nil
	self.add_max = add_max ~= 0 and add_max or nil
	self.min = min or nil
	self.max = max or nil
end

local MulDivRound = MulDivRound
local Clamp = Clamp
function Modifier:ModApply(value)
	value = MulDivRound(value + self.add, self.mul, 1000)
	local min, max = self.min, self.max
	min = min and (min + self.add_min)
	max = max and (max + self.add_max)
	return Clamp(value, min, max)
end

function Modifier:ModAccumulate(mod_list)
	local mul, add, add_min, add_max = 1000, 0, 0, 0
	for _, mod in ipairs(mod_list or self) do
		add = add + mod.add
		mul = MulDivRound(mul, mod.mul, 1000)
		add_min = add_min + mod.add_min
		add_max = add_max + mod.add_max
	end
	self.add = add ~= 0 and add or nil
	self.mul = mul ~= 1000 and mul or nil
	self.add_min = add_min ~= 0 and add_min or nil
	self.add_max = add_max ~= 0 and add_max or nil
end


----- ObjectModifier

DefineClass.ObjectModifier = {
	__parents = { "InitDone", "Modifier" },
	target = false,
	is_applied = false,
}

function ObjectModifier:Init()
	self:TurnOn()
end

function ObjectModifier:Done()
	self:TurnOff()
end

function ObjectModifier:ResolveObject(obj)
	return obj
end

function ObjectModifier:TurnOn()
	if self.is_applied then return end
	local target = self:ResolveObject(self.target)
	if target then target:AddModifierObj(self) end
	self.is_applied = true
end

function ObjectModifier:TurnOff()
	if not self.is_applied then return end
	local target = self:ResolveObject(self.target)
	if target then target:RemoveModifierObj(self) end
	self.is_applied = false
end

function ObjectModifier:Change(...)
	self:ModSet(...)
	if self.is_applied then
		local target = self:ResolveObject(self.target)
		if target then target:ChangedModifier(self.prop) end
	end
end

function ObjectModifier:IsApplied()
	return self.is_applied
end


----- MultipleObjectsModifier

DefineClass.MultipleObjectsModifier = {
	__parents = { "InitDone", "Modifier" },
	targets = false,
	is_applied = false,
}

function MultipleObjectsModifier:Init()
	self:TurnOn()
end

function MultipleObjectsModifier:Done()
	self:TurnOff()
end

function MultipleObjectsModifier:ResolveObject(obj)
	return obj
end

function MultipleObjectsModifier:TurnOn()
	if self.is_applied then return end
	for i, target in ipairs(self.targets or empty_table) do
		target = self:ResolveObject(target)
		if target then target:AddModifierObj(self) end
	end
	self.is_applied = true
end

function MultipleObjectsModifier:TurnOff()
	if not self.is_applied then return end
	for i, target in ipairs(self.targets or empty_table) do
		target = self:ResolveObject(target)
		if target then target:RemoveModifierObj(self) end
	end
	self.is_applied = false
end

function MultipleObjectsModifier:CleanInvalidTargets()
	table.validate(self.targets)
end

function MultipleObjectsModifier:CanDelete()
	for i, target in ipairs(self.targets or empty_table) do
		if IsValid(target) then
			return false
		end
	end
	return true
end

function MultipleObjectsModifier:Change(...)
	self:ModSet(...)
	if self.is_applied then
		for i, target in ipairs(self.targets or empty_table) do
			target = self:ResolveObject(target)
			if target then target:ChangedModifier(self.prop) end
		end
	end
end

function MultipleObjectsModifier:AddTarget(target)
	assert(not table.find(self.targets, target))
	table.insert(self.targets, target)
	if self.is_applied then
		target = self:ResolveObject(target)
		if target then target:AddModifierObj(self) end
	end
end

function MultipleObjectsModifier:RemoveTarget(target)
	local found = table.remove_entry(self.targets, target)
	if found and self.is_applied then
		target = self:ResolveObject(target)
		if target then target:RemoveModifierObj(self) end
	end
end

if FirstLoad then
	ModifiablePropsComboItems = {}
	ModifiablePropScale = {}
end

local function UpdateModifiablePropScales()
	local scale = {}
	ClassDescendants("Modifiable", function(name, classdef, scale)
		local class_props = classdef:GetProperties()
		for i = 1, #class_props do
			local prop = class_props[i]
			if prop.modifiable and prop.editor == "number" then
				local new_scale = prop.scale
				local existing_scale = scale[prop.id]
				if not existing_scale then
					scale[prop.id] = new_scale
				elseif existing_scale ~= new_scale then
					assert(false, "Modifiable property with different scale factors!")
				end
			end
		end
	end, scale)
	ModifiablePropsComboItems = table.keys(scale)
	table.sort(ModifiablePropsComboItems, CmpLower)
	ModifiablePropScale = scale
end

function OnMsg.ClassesBuilt()
	UpdateModifiablePropScales()
end

function OnMsg.BinAssetsLoaded()
	UpdateModifiablePropScales()
end

function ClassModifiablePropsCombo(obj)
	local existing, props = {}, {}
	local class_props = obj:GetProperties()
	for i = 1,#class_props do
		local prop = class_props[i]
		if prop.modifiable and prop.editor == "number" and not existing[prop.id] then
			existing[prop.id] = true
			props[#props + 1] = {value = prop.id, text = prop.name or prop.id}
		end
	end
	TSort(props, "text")
	return props
end

function ClassModifiablePropsNonTranslatableCombo(obj)
	if type(obj) == "string" then
		obj = g_Classes[obj]
	end
	if not obj then
		return ModifiablePropsComboItems
	end
	local props = {}
	for _, prop in ipairs(obj:GetProperties()) do
		if prop.modifiable and prop.editor == "number" then
			props[#props + 1] = prop.id
		end
	end
	table.sort(props)
	return props
end

function NestedObjectsCombo(obj)
	if type(obj) == "string" then
		obj = g_Classes[obj]
	end
	if not obj then return {} end
	local items = {}
	local props = obj:GetProperties()
	for _, prop in ipairs(props) do
		if prop.editor == "nested_obj" or prop.editor == "nested_list" then
			items[#items + 1] = prop.id
		end
	end
	table.sort(items)
	table.insert(items, 1, "")
	return items
end

DefineClass.ModifyProperty = {
	__parents = { "ContinuousEffect", "Modifier" },
	properties = {
		{ id = "Id", },
		{ id = "obj_class", name = "Object Class", help = "Apply to objects of this class only (optional)", 
			editor = "choice", default = false, items = function (self) return ClassDescendantsList("Modifiable") end, 
			no_edit = function(self) return not self.HasClassProp end, dont_save = function(self) return not self.HasClassProp end },
		{ id = "sub_object", name = "Sub-object", help = "Use to specify the sub-object to be modified (optional)", 
			editor = "string_list", default = false, 
			item_default = "", items = function (self) return self:HasMember("obj_class") and NestedObjectsCombo(self.obj_class) or {} end, arbitrary_value = false, max_items = -1, 
			no_edit = function(self) return not self.obj_class or not self.HasSubObjectProp end, dont_save = function(self) return not self.HasSubObjectProp end },
		{ id = "id", name = "Id", help = "Only the last modifier with this id will be active (optional)", 
			editor = "text", default = false,
			no_edit = function(self) return not self.HasIdProp end, dont_save = function(self) return not self.HasIdProp end },
		{ id = "prop", name = "Property", help = "Name of a numeric property to modify", 
			editor = "choice", default = false, items = function (self) return ClassModifiablePropsNonTranslatableCombo(self.obj_class) end, },
		{ id = "add", name = "Add", help = "Additive modifier, applied before Mul", 
			editor = "number", default = 0, scale = function (self) return self:GetModScale() end, },
		{ id = "mul", name = "Mul", help = "Multiplicative modifier", 
			editor = "number", default = 1000, min = 0, scale = 1000 },
		{ id = "add_min", name = "Change min", help = "Add to modified value min", 
			editor = "number", default = 0, scale = function (self) return self:GetModScale() end, },
		{ id = "add_max", name = "Change max", help = "Add to modified value max",
			editor = "number", default = 0, scale = function (self) return self:GetModScale() end, },
		{ id = "display_text", name = "Display Text", help = "Can be used to display in the UI this modifier",
			editor = "text", default = "", translate = true,
			no_edit = function(self) return self.HasDisplayTextProp end, dont_save = function(self) return self.HasDisplayTextProp end },
	},
	CreateInstance = false,
	RequiredObjClasses = {
		"Modifiable",
	},
	Documentation = "Applies a modifier to a property within the object parameter of this effect.",
	EditorNestedObjCategory = "Continuous Effects",
	EditorView = T(744151871819, "Modify <u(SubObjectEditorView)><u(prop)><AddEditorView><MulEditorView> <u(id)>"),
	HasClassProp = true,
	HasSubObjectProp = true,
	HasIdProp = true,
	HasDisplayTextProp = true,
}

function ModifyProperty:GetSubObjectEditorView()
	if next(self.sub_object or empty_table) then
		return table.concat(self.sub_object, ".") .. "."
	end
	return ""
end

function ModifyProperty:GetAddEditorView()
	if self.add == 0 then return "" end
	local scale = self:GetModScale()
	local text = (self.add < 0 and Untranslated(" -") or Untranslated(" +")) .. FormatAsFloat(abs(self.add), type(scale) == "number" and scale or const.Scale[scale] or 1, 3, true)
	return type(scale) == "string" and text .. Untranslated(scale) or text
end

function ModifyProperty:GetMulEditorView()
	if self.mul == 1000 then return "" end
	return Untranslated(" x") .. FormatAsFloat(self.mul, 1000, 3, true)
end

function ModifyProperty:GetModScale()
	return ModifiablePropScale[self.prop] or 1
end

function ModifyProperty:ResolveObject(obj)
	if self.obj_class and not obj:IsKindOf(self.obj_class) then return end
	for i, field in ipairs(self.sub_object or empty_table) do
		obj = type(obj) == "table" and rawget(obj, field)
	end
	return obj
end

function ModifyProperty:OnStart(obj, context)
	obj = self:ResolveObject(obj)
	if obj then
		obj:AddModifierObj(self)
	end
end
ModifyProperty.__exec = ModifyProperty.OnStart

function ModifyProperty:OnStop(obj, context)
	obj = self:ResolveObject(obj)
	if obj then
		obj:RemoveModifierObj(self)
	end
end

function ModifyProperty:GetError()
	if not self.prop then
		return "Missing property to modify"
	elseif self.add == 0 and self.mul == 1000 and self.add_min == 0 and self.add_max == 0 then
		return "Default values result in no modification"
	end
end


----- ModifiersPreset

DefineClass.ModifiersPreset = {
	__parents = { "Preset", },
	properties = {
		{ category = "Effect", id = "Modifiers", name = "Modifiers", no_edit = function(self) return not self.ModifiersClass end,
			editor = "nested_list", default = false, base_class = function(self) return self.ModifiersClass end, all_descendants = true, },
	},
	ModifiersClass = false,
	EditorMenubarName = false,
}

function ModifiersPreset:ApplyModifiers(obj)
	for _, modifier in ipairs(self.Modifiers or empty_table) do
		modifier:OnStart(obj, self)
	end
end

function ModifiersPreset:UnapplyModifiers(obj)
	for _, modifier in ipairs(self.Modifiers or empty_table) do
		modifier:OnStop(obj, self)
	end
end

function ModifiersPreset:PostLoad()
	for _, modifier in ipairs(self.Modifiers or empty_table) do
		modifier.container = self
	end
	Preset.PostLoad(self)
end