Ticket #4003: t4003-part1.patch
File t4003-part1.patch, 9.4 KB (added by , 8 years ago) |
---|
-
binaries/data/mods/public/simulation/ai/common-api/entity.js
103 103 return this.get("Identity/Civ"); 104 104 }, 105 105 106 cost: function() {106 "cost": function(productionQueue) { 107 107 if (!this.get("Cost")) 108 108 return undefined; 109 109 110 110 let ret = {}; 111 let typeCost; 111 112 for (let type in this.get("Cost/Resources")) 112 ret[type] = +this.get("Cost/Resources/" + type); 113 { 114 typeCost = +this.get("Cost/Resources/" + type); 115 if (productionQueue) 116 typeCost *= productionQueue.techCostMultiplier(type); 117 ret[type] = typeCost; 118 } 113 119 return ret; 114 120 }, 115 121 116 costSum: function() { 117 if (!this.get("Cost")) 122 "costSum": function(productionQueue) { 123 let cost = this.cost(productionQueue); 124 if (!cost) 118 125 return undefined; 119 120 126 let ret = 0; 121 for (let type in this.get("Cost/Resources"))122 ret += +this.get("Cost/Resources/" + type);127 for (let type in cost) 128 ret += cost[type]; 123 129 return ret; 124 130 }, 125 131 132 "techCostMultiplier": function(type) { 133 return +(this.get("ProductionQueue/TechCostMultiplier/"+type) || 1); 134 }, 135 126 136 /** 127 137 * Returns the radius of a circle surrounding this entity's 128 138 * obstruction shape, or undefined if no obstruction. … … 462 472 return this.get("BuildRestrictions/Category"); 463 473 }, 464 474 465 buildTime: function() { 466 return +this.get("Cost/BuildTime"); 475 "buildTime": function(productionQueue) { 476 let time = +this.get("Cost/BuildTime"); 477 if (productionQueue) 478 time *= productionQueue.techCostMultiplier("time"); 479 return time; 467 480 }, 468 481 469 482 buildDistance: function() { -
binaries/data/mods/public/simulation/ai/common-api/gamestate.js
693 693 }; 694 694 695 695 /** 696 * Find buildings that are capable of training that template.696 * Return true if we have a building able to train that template 697 697 */ 698 m.GameState.prototype.hasTrainer = function(template) 699 { 700 let civ = this.playerData.civ; 701 for (let ent of this.getOwnTrainingFacilities().values()) 702 { 703 let trainable = ent.trainableEntities(civ); 704 if (trainable && trainable.indexOf(template) !== -1) 705 return true; 706 } 707 return false; 708 }; 709 710 /** 711 * Find buildings able to train that template. 712 */ 698 713 m.GameState.prototype.findTrainers = function(template) 699 714 { 700 715 let civ = this.playerData.civ; 701 716 return this.getOwnTrainingFacilities().filter(function(ent) { 702 717 let trainable = ent.trainableEntities(civ); 703 return trainable && trainable.indexOf(template) != -1;718 return trainable && trainable.indexOf(template) !== -1; 704 719 }); 705 720 }; 706 721 … … 718 733 return undefined; 719 734 }; 720 735 721 /** Return true if one of thebuildings is capable of researching the given tech */736 /** Return true if one of our buildings is capable of researching the given tech */ 722 737 m.GameState.prototype.hasResearchers = function(templateName, noRequirementCheck) 723 738 { 724 739 // let's check we can research the tech. … … 752 767 { 753 768 // let's check we can research the tech. 754 769 if (!this.canResearch(templateName, noRequirementCheck)) 755 return [];770 return undefined; 756 771 757 772 let self = this; 758 773 let civ = this.playerData.civ; -
binaries/data/mods/public/simulation/ai/common-api/technology.js
65 65 return this._pairedWith; 66 66 }; 67 67 68 m.Technology.prototype.cost = function( )68 m.Technology.prototype.cost = function(productionQueue) 69 69 { 70 70 if (!this._template.cost) 71 71 return undefined; 72 return this._template.cost; 72 let cost = {}; 73 for (let type in this._template.cost) 74 { 75 cost[type] = +this._template.cost[type]; 76 if (productionQueue) 77 cost[type] *= productionQueue.techCostMultiplier(type); 78 } 79 return cost; 73 80 }; 74 81 82 m.Technology.prototype.costSum = function(productionQueue) 83 { 84 let cost = this.cost(productionQueue); 85 if (!cost) 86 return undefined; 87 let ret = 0; 88 for (let type in cost) 89 ret += cost[type]; 90 return ret; 91 }; 92 75 93 // seconds 76 94 m.Technology.prototype.researchTime = function() 77 95 { -
binaries/data/mods/public/simulation/ai/petra/queueManager.js
355 355 let item = queue.getNext(); 356 356 if (this.accounts[name].canAfford(item.getCost()) && item.canStart(gameState)) 357 357 { 358 this.finishingTime = gameState.ai.elapsedTime; 359 this.accounts[name].subtract(item.getCost()); 360 queue.startNext(gameState); 361 queue.switched = 0; 358 // canStart may update the cost because of the costMultiplier so we must check it again 359 if (this.accounts[name].canAfford(item.getCost())) 360 { 361 this.finishingTime = gameState.ai.elapsedTime; 362 this.accounts[name].subtract(item.getCost()); 363 queue.startNext(gameState); 364 queue.switched = 0; 365 } 362 366 } 363 367 } 364 368 else if (!queue.hasQueuedUnits()) -
binaries/data/mods/public/simulation/ai/petra/queueplan.js
12 12 this.template = gameState.getTemplate(this.type); 13 13 if (!this.template) 14 14 { 15 API3.warn("Tried to add the inexisting template " + this.type + " to Petra. Please report this on the forums");15 API3.warn("Tried to add the inexisting template " + this.type + " to Petra."); 16 16 return false; 17 17 } 18 18 this.ID = gameState.ai.uniqueIDs.plans++; 19 19 this.cost = new API3.Resources(this.template.cost()); 20 20 this.number = 1; 21 22 21 this.category = ""; 23 22 24 23 return true; -
binaries/data/mods/public/simulation/ai/petra/queueplanResearch.js
9 9 if (this.template.researchTime === undefined) 10 10 return false; 11 11 12 // Refine the estimated cost 13 let researchers = this.getBestResearchers(gameState, true); 14 if (researchers) 15 this.cost = new API3.Resources(this.template.cost(researchers[0])); 16 12 17 this.category = "technology"; 13 18 14 19 this.rush = rush; … … 20 25 21 26 m.ResearchPlan.prototype.canStart = function(gameState) 22 27 { 23 // also checks canResearch 24 return gameState.hasResearchers(this.type); 28 this.researchers = this.getBestResearchers(gameState); 29 if (!this.researchers) 30 return false; 31 this.cost = new API3.Resources(this.template.cost(this.researchers[0])); 32 return true; 25 33 }; 26 34 35 m.ResearchPlan.prototype.getBestResearchers = function(gameState, noRequirementCheck = false) 36 { 37 let allResearchers = gameState.findResearchers(this.type, noRequirementCheck); 38 if (!allResearchers || !allResearchers.hasEntities()) 39 return undefined; 40 41 // Keep only researchers with smallest cost 42 let costMin = Math.min(); 43 let researchers; 44 for (let ent of allResearchers.values()) 45 { 46 let cost = this.template.costSum(ent); 47 if (cost === costMin) 48 researchers.push(ent); 49 else if (cost < costMin) 50 { 51 costMin = cost; 52 researchers = [ent]; 53 } 54 } 55 return researchers; 56 }; 57 27 58 m.ResearchPlan.prototype.isInvalid = function(gameState) 28 59 { 29 60 return gameState.isResearched(this.type) || gameState.isResearching(this.type); … … 31 62 32 63 m.ResearchPlan.prototype.start = function(gameState) 33 64 { 34 let trainers = gameState.findResearchers(this.type).toEntityArray(); 35 36 // Prefer training buildings with short queues 37 // (TODO: this should also account for units added to the queue by 38 // plans that have already been executed this turn) 39 if (trainers.length) 40 { 41 trainers.sort((a, b) => a.trainingQueueTime() - b.trainingQueueTime()); 42 // drop anything in the queue if we rush it. 43 if (this.rush) 44 trainers[0].stopAllProduction(0.45); 45 trainers[0].research(this.type); 46 } 65 // Prefer researcher with shortest queues (no need to serialize this.researchers 66 // as the functions canStart and start are always called on the same turn) 67 this.researchers.sort((a, b) => a.trainingQueueTime() - b.trainingQueueTime()); 68 // Drop anything in the queue if we rush it. 69 if (this.rush) 70 this.researchers[0].stopAllProduction(0.45); 71 this.researchers[0].research(this.type); 47 72 this.onStart(gameState); 48 73 }; 49 74 -
binaries/data/mods/public/simulation/ai/petra/queueplanTraining.js
110 110 this.number += amount; 111 111 }; 112 112 113 / / Find the promoted types corresponding to this.type113 /** Find the promoted types corresponding to this.type */ 114 114 m.TrainingPlan.prototype.promotedTypes = function(gameState) 115 115 { 116 116 let types = [];