Line data Source code
1 : function SkirmishReplacer() {} 2 : 3 0 : SkirmishReplacer.prototype.Schema = 4 : "<optional>" + 5 : "<element name='general' a:help='The general element replaces {civ} with the civ code.'>" + 6 : "<interleave>" + 7 : "<text/>" + 8 : "</interleave>" + 9 : "</element>" + 10 : "</optional>"; 11 : 12 0 : SkirmishReplacer.prototype.Init = function() 13 : { 14 : }; 15 : 16 0 : SkirmishReplacer.prototype.Serialize = null; // We have no dynamic state to save 17 : 18 : function getReplacementEntities(civ) 19 : { 20 0 : return Engine.ReadJSONFile("simulation/data/civs/" + civ + ".json").SkirmishReplacements; 21 : } 22 : 23 0 : SkirmishReplacer.prototype.OnOwnershipChanged = function(msg) 24 : { 25 0 : if (msg.to == 0) 26 0 : warn("Skirmish map elements can only be owned by regular players. Please delete entity "+this.entity+" or change the ownership to a non-gaia player."); 27 : }; 28 : 29 0 : SkirmishReplacer.prototype.ReplaceEntities = function() 30 : { 31 0 : const civ = QueryOwnerInterface(this.entity, IID_Identity)?.GetCiv(); 32 0 : if (!civ) 33 0 : return; 34 : 35 0 : var replacementEntities = getReplacementEntities(civ); 36 : 37 0 : var cmpTemplateManager = Engine.QueryInterface(SYSTEM_ENTITY, IID_TemplateManager); 38 0 : var templateName = cmpTemplateManager.GetCurrentTemplateName(this.entity); 39 : 40 0 : let specialFilters = templateName.substr(0, templateName.lastIndexOf("|") + 1); 41 0 : templateName = removeFiltersFromTemplateName(templateName); 42 : 43 0 : if (templateName in replacementEntities) 44 0 : templateName = replacementEntities[templateName]; 45 0 : else if (this.template && "general" in this.template) 46 0 : templateName = this.template.general; 47 : else 48 0 : templateName = ""; 49 : 50 0 : if (!templateName || civ == "gaia") 51 : { 52 0 : Engine.DestroyEntity(this.entity); 53 0 : return; 54 : } 55 : 56 0 : templateName = specialFilters + templateName.replace(/\{civ\}/g, civ); 57 : 58 0 : var cmpCurPosition = Engine.QueryInterface(this.entity, IID_Position); 59 0 : var replacement = Engine.AddEntity(templateName); 60 0 : if (!replacement) 61 : { 62 0 : Engine.DestroyEntity(this.entity); 63 0 : return; 64 : } 65 0 : var cmpReplacementPosition = Engine.QueryInterface(replacement, IID_Position); 66 0 : var pos = cmpCurPosition.GetPosition2D(); 67 0 : cmpReplacementPosition.JumpTo(pos.x, pos.y); 68 0 : var rot = cmpCurPosition.GetRotation(); 69 0 : cmpReplacementPosition.SetYRotation(rot.y); 70 0 : var cmpCurOwnership = Engine.QueryInterface(this.entity, IID_Ownership); 71 0 : var cmpReplacementOwnership = Engine.QueryInterface(replacement, IID_Ownership); 72 0 : cmpReplacementOwnership.SetOwner(cmpCurOwnership.GetOwner()); 73 : 74 0 : let msg = { "entity": this.entity, "newentity": replacement }; 75 0 : Engine.PostMessage(this.entity, MT_EntityRenamed, msg); 76 0 : Engine.PostMessage(this.entity, MT_SkirmishReplacerReplaced, msg); 77 0 : Engine.DestroyEntity(this.entity); 78 : }; 79 : 80 : /** 81 : * Replace this entity with a civ-specific entity 82 : * Message is sent right before InitGame() is called, in InitGame.js 83 : * Replacement needs to happen early on real games to not confuse the AI 84 : */ 85 0 : SkirmishReplacer.prototype.OnSkirmishReplace = function(msg) 86 : { 87 0 : this.ReplaceEntities(); 88 : }; 89 : 90 : /** 91 : * Replace this entity with a civ-specific entity 92 : * This is needed for Atlas, when the entity isn't replaced before the game starts, 93 : * so it needs to be replaced on the first turn. 94 : */ 95 0 : SkirmishReplacer.prototype.OnUpdate = function(msg) 96 : { 97 0 : this.ReplaceEntities(); 98 : }; 99 : 100 0 : Engine.RegisterComponentType(IID_SkirmishReplacer, "SkirmishReplacer", SkirmishReplacer);