Ticket #1353: 1353_InconsistentGathering.B.patch
File 1353_InconsistentGathering.B.patch, 5.6 KB (added by , 12 years ago) |
---|
-
binaries/data/mods/public/simulation/components/ResourceGatherer.js
187 187 /** 188 188 * Gather from the target entity. This should only be called after a successful range check, 189 189 * and if the target has a compatible ResourceSupply. 190 * It should be called at a rate of once per second.190 * Call interval will be determined by gather rate, so always gather 1 amount when called. 191 191 */ 192 192 ResourceGatherer.prototype.PerformGather = function(target) 193 193 { 194 var rate = this.GetTargetGatherRate(target);195 if (! rate)194 var cmpUnitAI = Engine.QueryInterface(this.entity, IID_UnitAI); 195 if (!cmpUnitAI || !cmpUnitAI.CanGather(target)) 196 196 return { "exhausted": true }; 197 197 198 var gatherAmount = 1; 199 198 200 var cmpResourceSupply = Engine.QueryInterface(target, IID_ResourceSupply); 199 201 var type = cmpResourceSupply.GetType(); 200 202 … … 205 207 // Find the maximum so we won't exceed our capacity 206 208 var maxGathered = this.GetCapacities()[type.generic] - this.carrying[type.generic]; 207 209 208 var status = cmpResourceSupply.TakeResources(Math.min( rate, maxGathered));210 var status = cmpResourceSupply.TakeResources(Math.min(gatherAmount, maxGathered)); 209 211 210 212 this.carrying[type.generic] += status.amount; 211 213 … … 244 246 245 247 var type = cmpResourceSupply.GetType(); 246 248 249 var rates = this.GetGatherRates(); 250 247 251 var rate; 248 if (type.specific && this.GetGatherRates()[type.generic+"."+type.specific])249 rate = this.GetGatherRates()[type.generic+"."+type.specific];252 if (type.specific && rates[type.generic+"."+type.specific]) 253 rate = rates[type.generic+"."+type.specific]; 250 254 else 251 rate = this.GetGatherRates()[type.generic];252 255 rate = rates[type.generic]; 256 253 257 return (rate || 0); 254 258 }; 255 259 -
binaries/data/mods/public/simulation/components/ResourceSupply.js
54 54 55 55 ResourceSupply.prototype.TakeResources = function(rate) 56 56 { 57 // Internally we handle fractional resource amounts (to be accurate 58 // over long periods of time), but want to return integers (so players 59 // have a nice simple integer amount of resources). So return the 60 // difference between rounded values: 57 // 'rate' should be a non-negative integer 61 58 62 59 var old = this.amount; 63 60 this.amount = Math.max(0, old - rate); 61 var change = old - this.amount; 64 62 65 // (use ceil instead of floor so that we continue returning non-zero values even if 0 < amount < 1)66 var change = Math.ceil(old) - Math.ceil(this.amount);67 68 63 // Remove entities that have been exhausted 69 64 if (this.amount == 0) 70 65 Engine.DestroyEntity(this.entity); 71 66 72 67 Engine.PostMessage(this.entity, MT_ResourceSupplyChanged, { "from": old, "to": this.amount }); 73 68 74 return { "amount": change, "exhausted": ( old== 0) };69 return { "amount": change, "exhausted": (this.amount == 0) }; 75 70 }; 76 71 77 72 ResourceSupply.prototype.GetType = function() -
binaries/data/mods/public/simulation/components/UnitAI.js
944 944 945 945 "GATHERING": { 946 946 "enter": function() { 947 this.StartTimer(1000, 1000); 947 var target = this.order.data.target; 948 949 // Calculate timing based on gather rates 950 // This allows the gather rate to control how often we gather, instead of how much. 951 var cmpResourceGatherer = Engine.QueryInterface(this.entity, IID_ResourceGatherer); 952 var rate = cmpResourceGatherer.GetTargetGatherRate(target); 948 953 954 if (!rate) 955 { 956 // No rate, give up on gathering 957 this.FinishOrder(); 958 return; 959 } 960 961 // Scale timing interval based on rate, and start timer 962 var offset = 1000; // Should be at least longer than repeat time. See attacking.enter. 963 var repeat = 1000 / rate; 964 this.StartTimer(offset, repeat); 965 949 966 // We want to start the gather animation as soon as possible, 950 967 // but only if we're actually at the target and it's still alive 951 968 // (else it'll look like we're chopping empty air). 952 969 // (If it's not alive, the Timer handler will deal with sending us 953 970 // off to a different target.) 954 if (this.CheckTargetRange(t his.order.data.target, IID_ResourceGatherer))971 if (this.CheckTargetRange(target, IID_ResourceGatherer)) 955 972 { 956 973 var typename = "gather_" + this.order.data.type.specific; 957 974 this.SelectAnimation(typename, false, 1.0, typename); … … 1970 1987 { 1971 1988 this.timer = undefined; 1972 1989 } 1973 else1974 {1975 var cmpTimer = Engine.QueryInterface(SYSTEM_ENTITY, IID_Timer);1976 this.timer = cmpTimer.SetTimeout(this.entity, IID_UnitAI, "TimerHandler", data.timerRepeat - lateness, data);1977 }1978 1990 1979 1991 UnitFsm.ProcessMessage(this, {"type": "Timer", "data": data, "lateness": lateness}); 1980 1992 }; … … 1992 2004 var data = { "timerRepeat": repeat }; 1993 2005 1994 2006 var cmpTimer = Engine.QueryInterface(SYSTEM_ENTITY, IID_Timer); 1995 this.timer = cmpTimer.SetTimeout(this.entity, IID_UnitAI, "TimerHandler", offset, data); 2007 if (repeat === undefined) 2008 this.timer = cmpTimer.SetTimeout(this.entity, IID_UnitAI, "TimerHandler", offset, data); 2009 else 2010 this.timer = cmpTimer.SetInterval(this.entity, IID_UnitAI, "TimerHandler", offset, repeat, data); 1996 2011 }; 1997 2012 1998 2013 /**