Ticket #4003: t4003-part2.patch
File t4003-part2.patch, 5.8 KB (added by , 8 years ago) |
---|
-
binaries/data/mods/public/simulation/ai/petra/queueplanResearch.js
15 15 this.cost = new API3.Resources(this.template.cost(researchers[0])); 16 16 17 17 this.category = "technology"; 18 19 18 this.rush = rush; 20 19 21 20 return true; -
binaries/data/mods/public/simulation/ai/petra/queueplanTraining.js
9 9 return false; 10 10 } 11 11 12 // Refine the estimated cost and add pop cost 13 let trainers = this.getBestTrainers(gameState); 14 let trainer = trainers ? trainers[0] : undefined; 15 this.cost = new API3.Resources(this.template.cost(trainer), +this.template._template.Cost.Population); 16 12 17 this.category = "unit"; 13 this.cost = new API3.Resources(this.template.cost(), +this.template._template.Cost.Population);14 15 18 this.number = number; 16 19 this.maxMerge = maxMerge; 17 20 … … 22 25 23 26 m.TrainingPlan.prototype.canStart = function(gameState) 24 27 { 25 let trainers = gameState.findTrainers(this.type); 28 this.trainers = this.getBestTrainers(gameState); 29 if (!this.trainers) 30 return false; 31 this.cost = new API3.Resources(this.template.cost(this.trainers[0]), +this.template._template.Cost.Population); 32 return true; 33 }; 34 35 m.TrainingPlan.prototype.getBestTrainers = function(gameState) 36 { 37 if (this.metadata && this.metadata.trainer) 38 { 39 let trainer = gameState.getEntityById(this.metadata.trainer); 40 if (trainer) 41 return [trainer]; 42 } 43 44 let allTrainers = gameState.findTrainers(this.type); 26 45 if (this.metadata && this.metadata.sea) 27 trainers = trainers.filter(API3.Filters.byMetadata(PlayerID, "sea", this.metadata.sea)); 46 allTrainers = allTrainers.filter(API3.Filters.byMetadata(PlayerID, "sea", this.metadata.sea)); 47 if (this.metadata && this.metadata.base) 48 allTrainers = allTrainers.filter(API3.Filters.byMetadata(PlayerID, "base", this.metadata.base)); 49 if (!allTrainers || !allTrainers.hasEntities()) 50 return undefined; 28 51 29 return trainers.length !== 0; 52 // Keep only trainers with smallest cost 53 let costMin = Math.min(); 54 let trainers; 55 for (let ent of allTrainers.values()) 56 { 57 let cost = this.template.costSum(ent); 58 if (cost === costMin) 59 trainers.push(ent); 60 else if (cost < costMin) 61 { 62 costMin = cost; 63 trainers = [ent]; 64 } 65 } 66 return trainers; 30 67 }; 31 68 32 69 m.TrainingPlan.prototype.start = function(gameState) … … 37 74 for (let key in this.metadata) 38 75 if (key !== "trainer") 39 76 metadata[key] = this.metadata[key]; 40 let trainer = gameState.getEntityById(this.metadata.trainer); 41 if (trainer) 42 trainer.train(gameState.civ(), this.type, this.number, metadata, this.promotedTypes(gameState)); 43 this.onStart(gameState); 44 return; 77 this.metadata = metadata; 45 78 } 46 79 47 let trainersColl = gameState.findTrainers(this.type); 48 if (this.metadata && this.metadata.sea) 49 trainersColl = trainersColl.filter(API3.Filters.byMetadata(PlayerID, "sea", this.metadata.sea)); 50 else if (this.metadata && this.metadata.base) 51 trainersColl = trainersColl.filter(API3.Filters.byMetadata(PlayerID, "base", this.metadata.base)); 52 53 // Prefer training buildings with short queues 54 // (TODO: this should also account for units added to the queue by 55 // plans that have already been executed this turn) 56 if (trainersColl.length) 80 if (this.trainers.length > 1) 57 81 { 58 let trainers = trainersColl.toEntityArray();59 82 let wantedIndex; 60 83 if (this.metadata && this.metadata.index) 61 84 wantedIndex = this.metadata.index; 62 85 let workerUnit = this.metadata && this.metadata.role && this.metadata.role == "worker"; 63 86 let supportUnit = this.template.hasClass("Support"); 64 trainers.sort(function(a, b) { 87 this.trainers.sort(function(a, b) { 88 // Prefer training buildings with short queues 65 89 let aa = a.trainingQueueTime(); 66 90 let bb = b.trainingQueueTime(); 91 // Give priority to support units in the cc 67 92 if (a.hasClass("Civic") && !supportUnit) 68 93 aa += 10; 69 94 if (b.hasClass("Civic") && !supportUnit) 70 95 bb += 10; 96 // And support units should not be too near to dangerous place 71 97 if (supportUnit) 72 98 { 73 99 if (gameState.ai.HQ.isNearInvadingArmy(a.position())) … … 75 101 if (gameState.ai.HQ.isNearInvadingArmy(b.position())) 76 102 bb += 50; 77 103 } 104 // Give also priority to buildings with the right accessibility 78 105 let aBase = a.getMetadata(PlayerID, "base"); 79 106 let bBase = b.getMetadata(PlayerID, "base"); 80 107 if (wantedIndex) … … 84 111 if (!bBase || gameState.ai.HQ.getBaseByID(bBase).accessIndex != wantedIndex) 85 112 bb += 30; 86 113 } 87 // then, if worker, small preference for bases with less workers114 // Then, if workers, small preference for bases with less workers 88 115 if (workerUnit && aBase && bBase && aBase != bBase) 89 116 { 90 117 let apop = gameState.ai.HQ.getBaseByID(aBase).workers.length; … … 96 123 } 97 124 return aa - bb; 98 125 }); 99 if (this.metadata && this.metadata.base !== undefined && this.metadata.base === 0)100 this.metadata.base = trainers[0].getMetadata(PlayerID, "base");101 trainers[0].train(gameState.civ(), this.type, this.number, this.metadata, this.promotedTypes(gameState));102 126 } 103 else if (gameState.ai.Config.debug > 1) 104 warn(" no trainers for this queue " + this.type); 127 128 if (this.metadata && this.metadata.base !== undefined && this.metadata.base === 0) 129 this.metadata.base = this.trainers[0].getMetadata(PlayerID, "base"); 130 this.trainers[0].train(gameState.civ(), this.type, this.number, this.metadata, this.promotedTypes(gameState)); 131 105 132 this.onStart(gameState); 106 133 }; 107 134