Changeset View
Standalone View
binaries/data/mods/public/simulation/components/ResourceGatherer.js
Show All 34 Lines | ||||||||||||||
{ | { | |||||||||||||
this.carrying = {}; // { generic type: integer amount currently carried } | this.carrying = {}; // { generic type: integer amount currently carried } | |||||||||||||
// (Note that this component supports carrying multiple types of resources, | // (Note that this component supports carrying multiple types of resources, | |||||||||||||
// each with an independent capacity, but the rest of the game currently | // each with an independent capacity, but the rest of the game currently | |||||||||||||
// ensures and assumes we'll only be carrying one type at once) | // ensures and assumes we'll only be carrying one type at once) | |||||||||||||
// The last exact type gathered, so we can render appropriate props | // The last exact type gathered, so we can render appropriate props | |||||||||||||
this.lastCarriedType = undefined; // { generic, specific } | this.lastCarriedType = undefined; // { generic, specific } | |||||||||||||
this.lastGathered = undefined; | ||||||||||||||
Silier: Assignment looks unnecessary, but ok | ||||||||||||||
Done Inline ActionsHow would you like me to code it ? it is similar to the line above. mammadori: How would you like me to code it ? it is similar to the line above. | ||||||||||||||
Done Inline ActionsYou don't need to assign it at all. Just assign it when using and delete when done with it. (Saves some serialisation data :) ) Freagarach: You don't need to assign it at all. Just assign it when using and delete when done with it. | ||||||||||||||
}; | }; | |||||||||||||
/** | /** | |||||||||||||
* Returns data about what resources the unit is currently carrying, | * Returns data about what resources the unit is currently carrying, | |||||||||||||
* in the form [ {"type":"wood", "amount":7, "max":10} ] | * in the form [ {"type":"wood", "amount":7, "max":10} ] | |||||||||||||
*/ | */ | |||||||||||||
ResourceGatherer.prototype.GetCarryingStatus = function() | ResourceGatherer.prototype.GetCarryingStatus = function() | |||||||||||||
{ | { | |||||||||||||
▲ Show 20 Lines • Show All 54 Lines • ▼ Show 20 Lines | ||||||||||||||
// Since this code is very performancecritical and applying technologies quite slow, cache it. | // Since this code is very performancecritical and applying technologies quite slow, cache it. | |||||||||||||
ResourceGatherer.prototype.RecalculateGatherRatesAndCapacities = function() | ResourceGatherer.prototype.RecalculateGatherRatesAndCapacities = function() | |||||||||||||
{ | { | |||||||||||||
this.baseSpeed = ApplyValueModificationsToEntity("ResourceGatherer/BaseSpeed", +this.template.BaseSpeed, this.entity); | this.baseSpeed = ApplyValueModificationsToEntity("ResourceGatherer/BaseSpeed", +this.template.BaseSpeed, this.entity); | |||||||||||||
this.rates = {}; | this.rates = {}; | |||||||||||||
for (let r in this.template.Rates) | for (let r in this.template.Rates) | |||||||||||||
{ | { | |||||||||||||
Done Inline ActionsI think check is not needed here, if variable is undefined you can just return it as well with the same result Silier: I think check is not needed here, if variable is undefined you can just return it as well with… | ||||||||||||||
let type = r.split("."); | let type = r.split("."); | |||||||||||||
if (type[0] != "treasure" && type.length > 1 && !Resources.GetResource(type[0]).subtypes[type[1]]) | if (type[0] != "treasure" && type.length > 1 && !Resources.GetResource(type[0]).subtypes[type[1]]) | |||||||||||||
Done Inline ActionsYou only use this within this file and the function does not add any check, so you could just use the member directly below. Freagarach: You only use this within this file and the function does not add any check, so you could just… | ||||||||||||||
{ | { | |||||||||||||
error("Resource subtype not found: " + type[0] + "." + type[1]); | error("Resource subtype not found: " + type[0] + "." + type[1]); | |||||||||||||
continue; | continue; | |||||||||||||
} | } | |||||||||||||
let rate = ApplyValueModificationsToEntity("ResourceGatherer/Rates/" + r, +this.template.Rates[r], this.entity); | let rate = ApplyValueModificationsToEntity("ResourceGatherer/Rates/" + r, +this.template.Rates[r], this.entity); | |||||||||||||
this.rates[r] = rate * this.baseSpeed; | this.rates[r] = rate * this.baseSpeed; | |||||||||||||
} | } | |||||||||||||
▲ Show 20 Lines • Show All 204 Lines • ▼ Show 20 Lines | ||||||||||||||
*/ | */ | |||||||||||||
ResourceGatherer.prototype.DropResources = function() | ResourceGatherer.prototype.DropResources = function() | |||||||||||||
{ | { | |||||||||||||
this.carrying = {}; | this.carrying = {}; | |||||||||||||
Engine.PostMessage(this.entity, MT_ResourceCarryingChanged, { "to": this.GetCarryingStatus() }); | Engine.PostMessage(this.entity, MT_ResourceCarryingChanged, { "to": this.GetCarryingStatus() }); | |||||||||||||
}; | }; | |||||||||||||
/** | ||||||||||||||
Done Inline Actions
Freagarach: | ||||||||||||||
Done Inline ActionsCan you (or someone else) come up with a more fitting name? AddToPlayerCounter or something? Freagarach: Can you (or someone else) come up with a more fitting name? `AddToPlayerCounter` or something? | ||||||||||||||
Done Inline ActionsCould be UpdatePlayerCount (gatherer is implied I guess) Stan: Could be UpdatePlayerCount (gatherer is implied I guess) | ||||||||||||||
* Handle the increment of Player's per resource gatherers counter | ||||||||||||||
Done Inline Actions
Freagarach: | ||||||||||||||
* returns true if counted | ||||||||||||||
Done Inline ActionsIs it needed to return something? Freagarach: Is it needed to return something? | ||||||||||||||
Done Inline Actions^ Freagarach: ^ | ||||||||||||||
Done Inline ActionsNot yet, it was intended as a feedback for UnitAI, I will remove it, it will be re-introducted as necessary. mammadori: Not yet, it was intended as a feedback for UnitAI, I will remove it, it will be re-introducted… | ||||||||||||||
*/ | ||||||||||||||
Done Inline ActionsQuery this right before it is used. Freagarach: Query this right before it is used. | ||||||||||||||
ResourceGatherer.prototype.PlayerCounterInc = function(type) | ||||||||||||||
{ | ||||||||||||||
let cmpPlayer = QueryOwnerInterface(this.entity, IID_Player); | ||||||||||||||
Done Inline ActionsNot sure if the warnings actually add anything? Freagarach: Not sure if the warnings actually add anything? | ||||||||||||||
Done Inline ActionsThe plan is to remove them as soon as I do not need them, still not clear how it is possible, a dead unit has no player? mammadori: The plan is to remove them as soon as I do not need them, still not clear how it is possible, a… | ||||||||||||||
Done Inline ActionsAh, that is what you meant with left debug code in then :) My bad :)
No, a dead entity has a player number of -1. I'm actually nt sure how one would end up with a playerless entity. Freagarach: Ah, that is what you meant with `left debug code in` then :) My bad :)
> a dead unit has no… | ||||||||||||||
Done Inline Actionsmove this before cmpPlayer Silier: move this before cmpPlayer | ||||||||||||||
if (!cmpPlayer) | ||||||||||||||
return false; | ||||||||||||||
if (this.lastGathered && this.lastGathered.generic == type.generic) | ||||||||||||||
Done Inline ActionsCould just use the member directly? (Since the get doesn't do anything.) Freagarach: Could just use the member directly? (Since the get doesn't do anything.) | ||||||||||||||
return false; | ||||||||||||||
this.lastGathered = type; | ||||||||||||||
Done Inline Actions= null is not needed Silier: `= null` is not needed | ||||||||||||||
let resCode = (type.generic == "treasure") ? type.specific : type.generic; | ||||||||||||||
cmpPlayer.AddResourceGatherer(resCode); | ||||||||||||||
return true; | ||||||||||||||
Done Inline ActionsWhy do you need this? Freagarach: Why do you need this? | ||||||||||||||
}; | ||||||||||||||
/** | ||||||||||||||
* Handle the decrement of Player's per resource gatherers counter | ||||||||||||||
* returns true if counted | ||||||||||||||
*/ | ||||||||||||||
Done Inline Actionsremove type variable and use this.lastGathered directly Silier: remove `type` variable and use `this.lastGathered` directly
also move check at the start of the… | ||||||||||||||
ResourceGatherer.prototype.PlayerCounterDec = function(playerid = null) | ||||||||||||||
{ | ||||||||||||||
let cmpPlayer = playerid ? | ||||||||||||||
Done Inline ActionsPlease don't use the members of another component directly like this. Freagarach: Please don't use the members of another component directly like this. | ||||||||||||||
Done Inline ActionsYou mean the ++ part? How should that be incremented properly? mammadori: You mean the ++ part? How should that be incremented properly?
| ||||||||||||||
Done Inline ActionscmpPlayer.AddGatherer(resCode) Freagarach: `cmpPlayer.AddGatherer(resCode)`
(And same comments apply on the decrement function of course.) | ||||||||||||||
Done Inline ActionsDon't need the assignment. Freagarach: Don't need the assignment. | ||||||||||||||
QueryPlayerIDInterface(playerid) : | ||||||||||||||
QueryOwnerInterface(this.entity, IID_Player); | ||||||||||||||
if (!cmpPlayer) | ||||||||||||||
return false; | ||||||||||||||
Done Inline Actions
(Or one could inline it using a ternary above.) Freagarach: (Or one could inline it using a ternary above.) | ||||||||||||||
let type = this.lastGathered; | ||||||||||||||
if (!type) | ||||||||||||||
return false; | ||||||||||||||
Done Inline ActionsDon't need to assign that to a variable, right? Freagarach: Don't need to assign that to a variable, right? | ||||||||||||||
Done Inline ActionsTo ease reading the following lines. I could use this.lastGathered instead of type if you prefere mammadori: To ease reading the following lines. I could use this.lastGathered instead of type if you… | ||||||||||||||
Done Inline ActionsNo strong opinion from me, just asking questions :) This can be just removed if ever I get to refactor treasures. Freagarach: No strong opinion from me, just asking questions :)
This can be just removed if ever I get to… | ||||||||||||||
Done Inline ActionsWould this be possible? Freagarach: Would this be possible? | ||||||||||||||
Done Inline ActionsWhat I mean that if we arrive here without type, something must be wrong, right? Freagarach: What I mean that if we arrive here without type, something must be wrong, right? | ||||||||||||||
Done Inline ActionsI'll remove this part too, it was necessary before the using (hopefully correct ) of UnitAI mammadori: I'll remove this part too, it was necessary before the using (hopefully correct ) of UnitAI | ||||||||||||||
let resCode = (type.generic == "treasure") ? type.specific : type.generic; | ||||||||||||||
cmpPlayer.RemoveResourceGatherer(resCode); | ||||||||||||||
this.lastGathered = undefined; | ||||||||||||||
return true; | ||||||||||||||
}; | ||||||||||||||
Done Inline Actionsdelete? Freagarach: `delete`? | ||||||||||||||
Done Inline ActionsNot sure about the delete though ^^ Freagarach: Not sure about the delete though ^^ | ||||||||||||||
// Since we cache gather rates, we need to make sure we update them when tech changes. | // Since we cache gather rates, we need to make sure we update them when tech changes. | |||||||||||||
// and when our owner change because owners can had different techs. | // and when our owner change because owners can had different techs. | |||||||||||||
ResourceGatherer.prototype.OnValueModification = function(msg) | ResourceGatherer.prototype.OnValueModification = function(msg) | |||||||||||||
{ | { | |||||||||||||
if (msg.component != "ResourceGatherer") | if (msg.component != "ResourceGatherer") | |||||||||||||
return; | return; | |||||||||||||
this.RecalculateGatherRatesAndCapacities(); | this.RecalculateGatherRatesAndCapacities(); | |||||||||||||
}; | }; | |||||||||||||
ResourceGatherer.prototype.OnOwnershipChanged = function(msg) | ResourceGatherer.prototype.OnOwnershipChanged = function(msg) | |||||||||||||
{ | { | |||||||||||||
if (msg.to == INVALID_PLAYER) | if (msg.to == INVALID_PLAYER) { | |||||||||||||
FreagarachUnsubmitted Done Inline Actions
Freagarach: | ||||||||||||||
this.PlayerCounterDec(msg.from); | ||||||||||||||
Done Inline ActionsIs this needed? (Both the comment and the msg.from check.) Freagarach: Is this needed? (Both the comment and the msg.from check.) | ||||||||||||||
Done Inline Actionsthe second check is definitely because I'm too chatty on IRC :P can be removed; wraitii: the second check is definitely because I'm too chatty on IRC :P can be removed; | ||||||||||||||
Done Inline Actions
Silier: | ||||||||||||||
return; | return; | |||||||||||||
} | ||||||||||||||
this.RecalculateGatherRatesAndCapacities(); | this.RecalculateGatherRatesAndCapacities(); | |||||||||||||
}; | }; | |||||||||||||
ResourceGatherer.prototype.OnGlobalInitGame = function(msg) | ResourceGatherer.prototype.OnGlobalInitGame = function(msg) | |||||||||||||
{ | { | |||||||||||||
this.RecalculateGatherRatesAndCapacities(); | this.RecalculateGatherRatesAndCapacities(); | |||||||||||||
}; | }; | |||||||||||||
ResourceGatherer.prototype.OnMultiplierChanged = function(msg) | ResourceGatherer.prototype.OnMultiplierChanged = function(msg) | |||||||||||||
{ | { | |||||||||||||
let cmpPlayer = QueryOwnerInterface(this.entity, IID_Player); | let cmpPlayer = QueryOwnerInterface(this.entity, IID_Player); | |||||||||||||
if (cmpPlayer && msg.player == cmpPlayer.GetPlayerID()) | if (cmpPlayer && msg.player == cmpPlayer.GetPlayerID()) | |||||||||||||
this.RecalculateGatherRatesAndCapacities(); | this.RecalculateGatherRatesAndCapacities(); | |||||||||||||
}; | }; | |||||||||||||
Engine.RegisterComponentType(IID_ResourceGatherer, "ResourceGatherer", ResourceGatherer); | Engine.RegisterComponentType(IID_ResourceGatherer, "ResourceGatherer", ResourceGatherer); |
Assignment looks unnecessary, but ok