Index: binaries/data/mods/public/globalscripts/ModificationTemplates.js =================================================================== --- binaries/data/mods/public/globalscripts/ModificationTemplates.js +++ binaries/data/mods/public/globalscripts/ModificationTemplates.js @@ -43,3 +43,156 @@ global.AuraTemplates = new ModificationTemplates("simulation/data/auras/"); global.TechnologyTemplates = new ModificationTemplates("simulation/data/technologies/"); } + +/** + * Derives modifications (to be applied to entities) from a given aura/technology. + * + * @param {Object} techTemplate - The aura/technology template to derive the modifications from. + * @return {Object} - An object containing the relevant modifications. + */ +function DeriveModificationsFromTech(techTemplate) +{ + if (!techTemplate.modifications) + return {}; + + let techMods = {}; + let techAffects = []; + if (techTemplate.affects && techTemplate.affects.length) + techAffects = techTemplate.affects.map(affected => affected.split(/\s+/)); + else + techAffects.push([]); + + for (let mod of techTemplate.modifications) + { + let affects = techAffects.slice(); + if (mod.affects) + { + let specAffects = mod.affects.split(/\s+/); + for (let a in affects) + affects[a] = affects[a].concat(specAffects); + } + + let newModifier = { "affects": affects }; + for (let idx in mod) + if (idx !== "value" && idx !== "affects") + newModifier[idx] = mod[idx]; + + if (!techMods[mod.value]) + techMods[mod.value] = []; + techMods[mod.value].push(newModifier); + } + return techMods; +} + +/** + * Derives modifications (to be applied to entities) from a provided array + * of aura/technology template data. + * + * @param {Object[]} techsDataArray + * @return {Object} - The combined relevant modifications of all the technologies. + */ +function DeriveModificationsFromTechnologies(techsDataArray) +{ + let derivedModifiers = {}; + for (let technology of techsDataArray) + { + if (!technology.reqs) + continue; + + let modifiers = DeriveModificationsFromTech(technology); + for (let modPath in modifiers) + { + if (!derivedModifiers[modPath]) + derivedModifiers[modPath] = []; + derivedModifiers[modPath] = derivedModifiers[modPath].concat(modifiers[modPath]); + } + } + return derivedModifiers; +} + +/** + * Common definition of the XML schema for in-template modifications. + */ +const ModificationSchema = +"" + + "" + + "" + + "tokens" + + "" + + "" + + "" + + "" + + "" + + "tokens" + + "" + + "" + + "" + + "" + + "" + + "" + + "" + + "" + + "" + + "" + + "" + + "" + + "" + + "" + +""; + +const ModificationsSchema = +"" + + "" + + "" + + "" + + ModificationSchema + + "" + + "" + +""; + +/** + * Derives a single modification (to be applied to entities) from a given XML template. + * + * @param {Object} techTemplate - The XML template node to derive the modification from. + * @return {Object} containing the relevant modification. + */ +function DeriveModificationFromXMLTemplate(template) +{ + let effect = {}; + if (template.Add) + effect.add = +template.Add; + if (template.Multiply) + effect.multiply = +template.Multiply; + if (template.Replace) + effect.replace = template.Replace; + effect.affects = template.Affects ? template.Affects._string.split(/\s/) : []; + + let ret = {}; + template.Paths._string.split(/\s/).forEach(path => { + ret[path] = [effect]; + }); + + return ret; +} + +/** + * Derives all modifications (to be applied to entities) from a given XML template. + * + * @param {Object} techTemplate - The XML template node to derive the modifications from. + * @return {Object} containing the combined modifications. + */ +function DeriveModificationsFromXMLTemplate(template) +{ + let ret = {}; + for (let name in template) + { + let modification = DeriveModificationFromXMLTemplate(template[name]); + for (let path in modification) + { + if (!ret[path]) + ret[path] = []; + ret[path] = ret[path].concat(modification[path]); + } + } + return ret; +} Index: binaries/data/mods/public/globalscripts/Technologies.js =================================================================== --- binaries/data/mods/public/globalscripts/Technologies.js +++ binaries/data/mods/public/globalscripts/Technologies.js @@ -40,74 +40,6 @@ } /** - * Derives modifications (to be applied to entities) from a given technology. - * - * @param {Object} techTemplate - The technology template to derive the modifications from. - * @return {Object} containing the relevant modifications. - */ -function DeriveModificationsFromTech(techTemplate) -{ - if (!techTemplate.modifications) - return {}; - - let techMods = {}; - let techAffects = []; - if (techTemplate.affects && techTemplate.affects.length) - for (let affected of techTemplate.affects) - techAffects.push(affected.split(/\s+/)); - else - techAffects.push([]); - - for (let mod of techTemplate.modifications) - { - let affects = techAffects.slice(); - if (mod.affects) - { - let specAffects = mod.affects.split(/\s+/); - for (let a in affects) - affects[a] = affects[a].concat(specAffects); - } - - let newModifier = { "affects": affects }; - for (let idx in mod) - if (idx !== "value" && idx !== "affects") - newModifier[idx] = mod[idx]; - - if (!techMods[mod.value]) - techMods[mod.value] = []; - techMods[mod.value].push(newModifier); - } - return techMods; -} - -/** - * Derives modifications (to be applied to entities) from a provided array - * of technology template data. - * - * @param {array} techsDataArray - * @return {object} containing the combined relevant modifications of all - * the technologies. - */ -function DeriveModificationsFromTechnologies(techsDataArray) -{ - let derivedModifiers = {}; - for (let technology of techsDataArray) - { - if (!technology.reqs) - continue; - - let modifiers = DeriveModificationsFromTech(technology); - for (let modPath in modifiers) - { - if (!derivedModifiers[modPath]) - derivedModifiers[modPath] = []; - derivedModifiers[modPath] = derivedModifiers[modPath].concat(modifiers[modPath]); - } - } - return derivedModifiers; -} - -/** * Returns whether the given modification applies to the entity containing the given class list */ function DoesModificationApply(modification, classes) Index: binaries/data/mods/public/globalscripts/Templates.js =================================================================== --- binaries/data/mods/public/globalscripts/Templates.js +++ binaries/data/mods/public/globalscripts/Templates.js @@ -180,7 +180,9 @@ effects.Damage[damageType] = getEntityValue(path + "/Damage/" + damageType); } - // TODO: status effects + if (temp.GiveStatus) + effects.GiveStatus = temp.GiveStatus; + return effects; }; Index: binaries/data/mods/public/gui/common/tooltips.js =================================================================== --- binaries/data/mods/public/gui/common/tooltips.js +++ binaries/data/mods/public/gui/common/tooltips.js @@ -377,13 +377,28 @@ if (template.Duration) durationString = sprintf(translate(", %(durName)s: %(duration)s"), { "durName": headerFont(translate("Duration")), - "duration": getSecondsString((template.TimeElapsed ? +template.Duration - template.TimeElapsed : +template.Duration) / 1000), + "duration": getSecondsString((template._timeElapsed ? +template.Duration - template._timeElapsed : +template.Duration) / 1000), }); - return sprintf(translate("%(statusName)s: %(effects)s, %(rate)s%(durationString)s"), { + let intervalString = ""; + if (template.Interval) + intervalString = sprintf(translate(", %(interval)s"), { + "interval": attackRateDetails(+template.Interval) + }); + + let tooltipString = ""; + if (template.Tooltip) + tooltipString = translate(template.Tooltip); + + let attackEffectsString = ""; + if (template.Damage || template.Capture) + attackEffectsString = attackEffectsDetails(template); + + return sprintf(translate("%(statusName)s: %(tooltip)s%(effects)s%(rate)s%(durationString)s"), { "statusName": headerFont(translateWithContext("status effect", name)), - "effects": attackEffectsDetails(template), - "rate": attackRateDetails(+template.Interval), + "tooltip": tooltipString, + "effects": attackEffectsString, + "rate": intervalString, "durationString": durationString }); } Index: binaries/data/mods/public/simulation/components/StatusEffectsReceiver.js =================================================================== --- binaries/data/mods/public/simulation/components/StatusEffectsReceiver.js +++ binaries/data/mods/public/simulation/components/StatusEffectsReceiver.js @@ -1,16 +1,44 @@ function StatusEffectsReceiver() {} +/** + * Initialises the status effects. + */ StatusEffectsReceiver.prototype.Init = function() { this.activeStatusEffects = {}; }; +/** + * Which status effects are active on this entity. + * + * @return {Object} - An object containing the status effects which currently affect the entity. + */ StatusEffectsReceiver.prototype.GetActiveStatuses = function() { return this.activeStatusEffects; }; -// Called by attacking effects. +/** + * Used to set status effects on an entity, used only from Transform.js. + * Do not use this to set new a new status effect on an entity, for no timer is set! + * + * @param {Object} data - The status effects to set on this entity. + */ +StatusEffectsReceiver.prototype.SetActiveStatuses = function(data) +{ + this.activeStatusEffects = data; +}; + +/** + * Called by Attacking effects. Adds status effects for each entry in the effectData. + * + * @param {Object} effectData - An object containing the status effects to give to the entity. + * @param {number} attacker - The entity ID of the attacker. + * @param {number} attackerOwner - The player ID of the attacker. + * @param {number} bonusMultiplier - A value to multiply the damage with (not implemented yet for SE). + * + * @return {Object} - The names of the status effects which were processed. + */ StatusEffectsReceiver.prototype.GiveStatus = function(effectData, attacker, attackerOwner, bonusMultiplier) { for (let effect in effectData) @@ -21,49 +49,100 @@ return { "inflictedStatuses": Object.keys(effectData) }; }; +/** + * Adds a status effect to the entity. + * + * @param {string} statusName - The name of the status effect. + * @param {object} data - The various effects and timings. + */ StatusEffectsReceiver.prototype.AddStatus = function(statusName, data) { if (this.activeStatusEffects[statusName]) + { + // TODO: implement different behaviour when receiving the same status multiple times. + // For now, these are ignored. return; + } this.activeStatusEffects[statusName] = {}; let status = this.activeStatusEffects[statusName]; Object.assign(status, data); - status.Interval = +data.Interval; - status.TimeElapsed = 0; - status.FirstTime = true; + + if (status.Modifiers) + { + let modifications = DeriveModificationsFromXMLTemplate(status.Modifiers); + let cmpModifiersManager = Engine.QueryInterface(SYSTEM_ENTITY, IID_ModifiersManager); + cmpModifiersManager.AddModifiers(statusName, modifications, this.entity); + } + + // With neither an interval nor a duration, there is no point in starting a timer. + if (!status.Duration && !status.Interval) + return; + + // We want an interval to update the GUI to show how much time of the status effect + // is left even if the status effect itself has no interval. + if (!status.Interval) + status._interval = 1000; + + status._timeElapsed = 0; + status._firstTime = true; let cmpTimer = Engine.QueryInterface(SYSTEM_ENTITY, IID_Timer); - status.Timer = cmpTimer.SetInterval(this.entity, IID_StatusEffectsReceiver, "ExecuteEffect", 0, +status.Interval, statusName); + status._timer = cmpTimer.SetInterval(this.entity, IID_StatusEffectsReceiver, "ExecuteEffect", 0, +(status.Interval || status._interval), statusName); }; +/** + * Removes a status effect from the entity. + * + * @param {string} statusName - The status effect to be removed. + */ StatusEffectsReceiver.prototype.RemoveStatus = function(statusName) { - if (!this.activeStatusEffects[statusName]) + let statusEffect = this.activeStatusEffects[statusName]; + if (!statusEffect) return; - let cmpTimer = Engine.QueryInterface(SYSTEM_ENTITY, IID_Timer); - cmpTimer.CancelTimer(this.activeStatusEffects[statusName].Timer); + if (statusEffect.Modifiers) + { + let cmpModifiersManager = Engine.QueryInterface(SYSTEM_ENTITY, IID_ModifiersManager); + cmpModifiersManager.RemoveAllModifiers(statusName, this.entity); + } + + if (statusEffect._timer) + { + let cmpTimer = Engine.QueryInterface(SYSTEM_ENTITY, IID_Timer); + cmpTimer.CancelTimer(statusEffect._timer); + } delete this.activeStatusEffects[statusName]; }; +/** + * Called by the timers. Executes a status effect. + * + * @param {string} statusName - The name of the status effect to be executed. + * @param {number} lateness - The delay between the calling of the function and the actual execution (turn time?). + */ StatusEffectsReceiver.prototype.ExecuteEffect = function(statusName, lateness) { let status = this.activeStatusEffects[statusName]; if (!status) return; - if (status.FirstTime) + if (status.Damage || status.Capture) + Attacking.HandleAttackEffects(statusName, status, this.entity, INVALID_ENTITY, INVALID_PLAYER); + + if (!status.Duration) + return; + + if (status._firstTime) { - status.FirstTime = false; - status.TimeElapsed += lateness; + status._firstTime = false; + status._timeElapsed += lateness; } else - status.TimeElapsed += status.Interval + lateness; - - Attacking.HandleAttackEffects(statusName, status, this.entity, -1, -1); + status._timeElapsed += +(status.Interval || status._interval) + lateness; - if (status.Duration && status.TimeElapsed >= +status.Duration) + if (status._timeElapsed >= +status.Duration) this.RemoveStatus(statusName); }; Index: binaries/data/mods/public/simulation/components/Timer.js =================================================================== --- binaries/data/mods/public/simulation/components/Timer.js +++ binaries/data/mods/public/simulation/components/Timer.js @@ -20,6 +20,35 @@ }; /** + * Gives the timers belonging to a specific entity. + * + * @param {number} entity - The entity ID of the entity to return the timers for. + * + * @return {Object[]} - An array of the timers which the entity has. + */ +Timer.prototype.GetTimers = function(entity) +{ + let timers = {}; + for (let [id, timer] of this.timers) + if (timer.entity == entity) + timers[id] = timer; + return timers; +}; + +/** + * Transfers a timer from an entity to a new entity, e.g. when promoting. + * + * @param {number} timerID - The timer ID to be transfered. + * @param {number} newEnt - The entity ID of the entity to which the timer ought to be transferred. + */ +Timer.prototype.TransferTimer = function(timerID, newEnt) +{ + for (let [id, timer] of this.timers) + if (id == timerID) + timer.entity = newEnt; +}; + +/** * Returns the duration of the latest turn in milliseconds. */ Timer.prototype.GetLatestTurnLength = function() Index: binaries/data/mods/public/simulation/components/tests/test_Pack.js =================================================================== --- binaries/data/mods/public/simulation/components/tests/test_Pack.js +++ binaries/data/mods/public/simulation/components/tests/test_Pack.js @@ -11,6 +11,7 @@ Engine.LoadComponentScript("interfaces/Player.js"); Engine.LoadComponentScript("interfaces/Promotion.js"); Engine.LoadComponentScript("interfaces/ResourceGatherer.js"); +Engine.LoadComponentScript("interfaces/StatusEffectsReceiver.js"); Engine.LoadComponentScript("interfaces/Timer.js"); Engine.LoadComponentScript("interfaces/UnitAI.js"); Engine.LoadComponentScript("Pack.js"); @@ -39,7 +40,8 @@ AddMock(SYSTEM_ENTITY, IID_Timer, { "CancelTimer": id => { timerActivated = false; return; }, - "SetInterval": (ent, iid, funcname, time, repeattime, data) => { timerActivated = true; return 7; } + "SetInterval": (ent, iid, funcname, time, repeattime, data) => { timerActivated = true; return 7; }, + "GetTimers": () => [] }); Engine.AddEntity = function(template) { Index: binaries/data/mods/public/simulation/components/tests/test_Timer.js =================================================================== --- binaries/data/mods/public/simulation/components/tests/test_Timer.js +++ binaries/data/mods/public/simulation/components/tests/test_Timer.js @@ -78,3 +78,10 @@ cmpTimer.OnUpdate({ "turnLength": 3.0 }); TS_ASSERT_UNEVAL_EQUALS(fired, [["s",2500]]); + +fired = []; + +var t = cmpTimer.SetTimeout(10, IID_Test, "Callback", 1000, "t"); +cmpTimer.TransferTimer(t, 20); +cmpTimer.OnUpdate({ "turnLength": 1.0 }); +TS_ASSERT_UNEVAL_EQUALS(fired, [["t",0]]); Index: binaries/data/mods/public/simulation/helpers/Attacking.js =================================================================== --- binaries/data/mods/public/simulation/helpers/Attacking.js +++ binaries/data/mods/public/simulation/helpers/Attacking.js @@ -3,50 +3,69 @@ */ function Attacking() {} +const DirectEffectsSchema = + "" + + "" + + "" + + "" + + // Armour requires Foundation to not be a damage type. + "Foundation" + + "" + + "" + + "" + + "" + + "" + + "" + + "" + + ""; + +const StatusEffectsSchema = + "" + + "" + + "" + + "" + + "" + + "" + + "" + + "" + + "" + + "" + + "" + + "" + + "" + + "" + + "" + + "" + + "" + + "" + + "" + + DirectEffectsSchema + + "" + + "" + + "" + + "" + + "" + + ModificationsSchema + + "" + + "" + + "" + + "" + + ""; + /** * Builds a RelaxRNG schema of possible attack effects. * See globalscripts/AttackEffects.js for possible elements. * Attacks may also have a "Bonuses" element. * - * @return {string} - RelaxNG schema string + * @return {string} - RelaxNG schema string. */ -const DamageSchema = "" + - "" + - "" + - "" + - // Armour requires Foundation to not be a damage type. - "Foundation" + - "" + - "" + - "" + - ""; - Attacking.prototype.BuildAttackEffectsSchema = function() { return "" + "" + "" + - "" + - DamageSchema + - "" + - "" + - "" + - "" + - "" + - "" + - "" + - "" + - "" + - "" + - "" + - "" + - "" + - "" + - "" + DamageSchema + "" + - "" + - "" + - "" + - "" + + DirectEffectsSchema + + StatusEffectsSchema + "" + "" + "" + @@ -250,13 +269,16 @@ Object.assign(targetState, cmpReceiver[receiver.method](attackData[effectType], attacker, attackerOwner, bonusMultiplier)); } + if (targetState.killed) + this.TargetKilled(attacker, target, attackerOwner); + + if (attacker == INVALID_ENTITY) + return; + let cmpPromotion = Engine.QueryInterface(attacker, IID_Promotion); if (cmpPromotion && targetState.xp) cmpPromotion.IncreaseXp(targetState.xp); - if (targetState.killed) - this.TargetKilled(attacker, target, attackerOwner); - Engine.PostMessage(target, MT_Attacked, { "type": attackType, "target": target, Index: binaries/data/mods/public/simulation/helpers/Transform.js =================================================================== --- binaries/data/mods/public/simulation/helpers/Transform.js +++ binaries/data/mods/public/simulation/helpers/Transform.js @@ -109,6 +109,12 @@ } } + let cmpStatusEffectsReceiver = Engine.QueryInterface(oldEnt, IID_StatusEffectsReceiver); + let cmpNewStatusEffectsReceiver = Engine.QueryInterface(newEnt, IID_StatusEffectsReceiver); + if (cmpStatusEffectsReceiver && cmpNewStatusEffectsReceiver) + cmpNewStatusEffectsReceiver.SetActiveStatuses(cmpStatusEffectsReceiver.GetActiveStatuses()); + + TransferTimers(oldEnt, newEnt); TransferGarrisonedUnits(oldEnt, newEnt); Engine.PostMessage(oldEnt, MT_EntityRenamed, { "entity": oldEnt, "newentity": newEnt }); @@ -256,6 +262,21 @@ } } +/** + * Transfers timers from oldEnt to newEnt. + * + * @param {number} oldEnt - The entity ID of the entity to transfer the timer from. + * @param {number} newEnt - The entity ID of the entity to transfer the timer to. + */ +function TransferTimers(oldEnt, newEnt) +{ + let cmpTimer = Engine.QueryInterface(SYSTEM_ENTITY, IID_Timer); + let timers = cmpTimer.GetTimers(oldEnt); + for (let timer in timers) + if (Engine.QueryInterface(newEnt, timers[timer].iid)) + cmpTimer.TransferTimer(timer, newEnt); +} + Engine.RegisterGlobal("ChangeEntityTemplate", ChangeEntityTemplate); Engine.RegisterGlobal("CanGarrisonedChangeTemplate", CanGarrisonedChangeTemplate); Engine.RegisterGlobal("ObstructionsBlockingTemplateChange", ObstructionsBlockingTemplateChange); Index: binaries/data/mods/public/simulation/templates/template_unit_infantry_ranged_archer.xml =================================================================== --- binaries/data/mods/public/simulation/templates/template_unit_infantry_ranged_archer.xml +++ binaries/data/mods/public/simulation/templates/template_unit_infantry_ranged_archer.xml @@ -11,6 +11,45 @@ 6.0 0 + + + Some bogus bonus tooltip describing the effect of the modifiers. This speeds up the entity but halves the health and resource gathering speed. + 15000 + + + UnitMotion/WalkSpeed + Unit + 20 + + + Health/Max ResourceGatherer/BaseSpeed + Unit Structure + 0.5 + + + + + 10000 + 1000 + + 1 + + + + Some bogus bonus tooltip describing the effect of the modifiers. This increases the armour permanently. + 1000 + + 1 + + + + Armour/Hack Armour/Pierce Armour/Crush + Unit + 20 + + + + 72.0 0.0 600