Ticket #531: PopLimit.diff
| File PopLimit.diff, 6.5 KB (added by evans, 22 months ago) |
|---|
-
binaries/data/mods/public/simulation/components/TrainingQueue.js
19 19 TrainingQueue.prototype.Init = function() 20 20 { 21 21 this.nextID = 1; 22 22 this.popReserve = 0; 23 23 this.queue = []; 24 24 // Queue items are: 25 25 // { … … 46 46 return string.split(/\s+/); 47 47 }; 48 48 49 TrainingQueue.prototype.GetPlayer = function() 50 { 51 var cmpPlayerMan = Engine.QueryInterface(SYSTEM_ENTITY, IID_PlayerManager); 52 var cmpOwnership = Engine.QueryInterface(this.entity, IID_Ownership); 53 var playerEnt = cmpPlayerMan.GetPlayerByID(cmpOwnership.GetOwner()); 54 return Engine.QueryInterface(playerEnt, IID_Player); 55 }; 56 57 TrainingQueue.prototype.ReservePopulationSlots = function(num) 58 { 59 this.popReserve += num; 60 }; 61 62 TrainingQueue.prototype.UnReservePopulationSlots = function(num) 63 { 64 this.popReserve -= Math.min(num, this.popReserve); 65 }; 66 49 67 TrainingQueue.prototype.AddBatch = function(player, templateName, count) 50 68 { 51 69 // TODO: there should probably be a limit on the number of queued batches … … 70 88 else 71 89 costs[r] = 0; 72 90 } 91 var population; 92 if(template.Cost.Population) 93 { 94 population = template.Cost.Population * count; 95 } 96 else 97 { 98 population = 0; 99 } 73 100 74 // Find the player 75 var cmpPlayerMan = Engine.QueryInterface(SYSTEM_ENTITY, IID_PlayerManager); 76 var playerEnt = cmpPlayerMan.GetPlayerByID(player); 77 var cmpPlayer = Engine.QueryInterface(playerEnt, IID_Player); 101 var cmpPlayer = this.GetPlayer(); 78 102 79 103 if (!cmpPlayer.TrySubtractResources(costs)) 80 104 { … … 82 106 return; 83 107 } 84 108 109 85 110 this.queue.push({ 86 111 "id": this.nextID++, 87 112 "template": templateName, 88 113 "count": count, 89 114 "resources": costs, 115 "population": population, 116 "trainingStarted": false, 90 117 "timeTotal": time*1000, 91 118 "timeRemaining": time*1000, 92 119 }); … … 109 136 110 137 // Now we've found the item to remove 111 138 112 // Find the player 113 var cmpPlayerMan = Engine.QueryInterface(SYSTEM_ENTITY, IID_PlayerManager); 114 var playerEnt = cmpPlayerMan.GetPlayerByID(player); 115 var cmpPlayer = Engine.QueryInterface(playerEnt, IID_Player); 139 var cmpPlayer = this.GetPlayer(); 116 140 117 141 // Refund the resource cost for this batch 118 142 cmpPlayer.AddResources(item.resources); 119 143 144 //Remove reserved population slots 145 if (item.trainingStarted) 146 { 147 //The batch's training has started. It will have 148 //population slots reserved for it 149 cmpPlayer.UnReservePopulationSlots(item.population); 150 this.UnReservePopulationSlots(item.population); 151 } 152 120 153 // Remove from the queue 121 154 // (We don't need to remove the timer - it'll expire if it discovers the queue is empty) 122 155 this.queue.splice(i, 1); 123 156 return; 124 157 } 125 } 158 }; 126 159 127 160 TrainingQueue.prototype.GetQueue = function() 128 161 { … … 139 172 return out; 140 173 }; 141 174 175 TrainingQueue.prototype.OnOwnershipChanged = function(msg) 176 { 177 var cmpPlayerMan = Engine.QueryInterface(SYSTEM_ENTITY, IID_PlayerManager); 178 if (msg.from != -1) 179 { 180 //if msg.from==-1, Building has just been created, 181 //so there will be no need to remove reserved slots 182 var fromPlayerEnt = cmpPlayerMan.GetPlayerByID(msg.from); 183 var cmpFromPlayer = Engine.QueryInterface(fromPlayerEnt, IID_Player); 184 //Remove reserved slots from the original owner 185 cmpFromPlayer.UnReservePopulationSlots(this.popReserve); 186 } 187 if (msg.to != -1) 188 { 189 //if msg.to==-1, Building has been destroyed, 190 //so there will be no need to reserve slots 191 var toPlayerEnt = cmpPlayerMan.GetPlayerByID(msg.to); 192 var cmpToPlayer = Engine.QueryInterface(toPlayerEnt, IID_Player); 193 //Reserve slots in new owner 194 cmpToPlayer.ReservePopulationSlots(this.popReserve); 195 } 196 197 }; 198 142 199 TrainingQueue.prototype.OnDestroy = function() 143 200 { 144 201 // If the building is destroyed while it's got a large training queue, 145 202 // you lose all the resources invested in that queue. That'll teach you 146 203 // to be so reckless with your buildings. 147 148 204 if (this.timer) 149 205 { 150 206 var cmpTimer = Engine.QueryInterface(SYSTEM_ENTITY, IID_Timer); … … 163 219 for (var i = 0; i < count; ++i) 164 220 { 165 221 var ent = Engine.AddEntity(templateName); 166 167 222 var pos = cmpFootprint.PickSpawnPoint(ent); 168 223 if (pos.y < 0) 169 224 { … … 198 253 // until we've used up all the time (so that we work accurately 199 254 // with items that take fractions of a second) 200 255 var time = g_ProgressInterval; 201 256 var cmpPlayer = this.GetPlayer(); 257 202 258 time *= 10; // XXX: this is a hack to make testing easier 203 204 259 while (time > 0 && this.queue.length) 205 260 { 206 261 var item = this.queue[0]; 207 if ( item.timeRemaining > time)262 if (!item.trainingStarted) 208 263 { 264 //Batch's training hasn't started yet 265 var popSlotsAvail = cmpPlayer.GetPopulationLimit() - cmpPlayer.GetPopulationCount(); 266 if (popSlotsAvail >= item.population) 267 { 268 //Slots available; 269 //Reserve those slots 270 this.ReservePopulationSlots(item.population); 271 cmpPlayer.ReservePopulationSlots(item.population); 272 //Start Training 273 item.timeRemaining -= time; 274 item.trainingStarted = true; 275 } 276 break; 277 } 278 else if (item.timeRemaining > time) 279 { 209 280 item.timeRemaining -= time; 210 281 break; 211 282 } … … 213 284 // This item is finished now 214 285 time -= item.timeRemaining; 215 286 this.SpawnUnits(item.template, item.count); 287 //Unit has been created. Remove reserved spots 288 cmpPlayer.UnReservePopulationSlots(item.population); 289 this.UnReservePopulationSlots(item.population); 216 290 this.queue.shift(); 217 291 } 218 292 -
binaries/data/mods/public/simulation/components/Player.js
10 10 this.civ = "gaia"; 11 11 this.colour = { "r": 0.0, "g": 0.0, "b": 0.0, "a": 1.0 }; 12 12 this.popCount = 0; 13 this.popLimit = 50; 13 this.popLimit = 0; 14 this.popReserve = 0; 14 15 this.resourceCount = { 15 16 "food": 2000, 16 17 "wood": 1500, … … 54 55 return this.colour; 55 56 }; 56 57 58 Player.prototype.ReservePopulationSlots = function(num) 59 { 60 this.popReserve += num; 61 }; 62 63 Player.prototype.UnReservePopulationSlots = function(num) 64 { 65 this.popReserve -= Math.min(num, this.popReserve); 66 } 67 57 68 Player.prototype.GetPopulationCount = function() 58 69 { 59 return this.popCount ;70 return this.popCount + this.popReserve; 60 71 }; 61 72 62 73 Player.prototype.GetPopulationLimit = function()
