jkgarner
Posts: 174
Joined: 4/30/2020 Status: offline
|
@michaelm75au : the fuel codes may have been listed there in an earlier version of the file, but they are not there now. (2020 Aug 25 10:15 AM CDT) @Randomizer : There are more 'elegant' solutions, we call them elegant because they are cleaner and easier to use. One of the aims of programmers should be to write code that is not only error-free and works correctly, but to write code that limits the number of errors the users of the code may introduce by using the code. The most elegant solutions do this be being very easy to understand and very easy to use. Part of this is checking the parameters passed to the functions... the function must work even if the parameters passed in are WRONG. ('cause sometimes they are) Part of this is cleanly handling error cases. Part of this is writing functions that are easy to understand, have a specific function or definable action, then making it handle that action fully. Allowing the user to specify parameters in more than 1 way, for example, can make the function more usable. As an example, the CommandLua API functions allow guid, or side and name, (or all three). They all work, and do the same thing. The error handling can be combined with functions that collapse allowable parameters into a single format... For example. I want a FillUp function that allows the user to specify fuel either as the code number or the fuel name (description). I must also make sure that the parameter is correct. This can be done in a single function: CheckFuelType function CheckFuelType(fType)
{
if type(fType) == 'string'
if fType == 'NoFuel' then return 1001 end
if fType == 'AviationFuel' then return 2001 end
if fType == 'DieselFuel' then return 3001 end
if fType == 'OilFuel' then return 3002 end
if fType == 'GasFuel' then return 3003 end
if fType == 'Battery' then return 4001 end
if fType == 'AirIndepedent' then return 4002 end
if fType == 'RocketFuel' then return 5001 end
if fType == 'TorpedoFuel' then return 5002 end
if fType == 'WeaponCoast' then return 5003 end
else if type(fType) == 'number'
if fType == 1001 then return fType end -- NoFuel
if fType == 2001 then return fType end -- AviationFuel
if fType == 3001 then return fType end -- DieselFuel
if fType == 3002 then return fType end -- OilFuel
if fType == 3003 then return fType end -- GasFuel
if fType == 4001 then return fType end -- Battery
if fType == 4002 then return fType end -- AirIndepedent
if fType == 5001 then return fType end -- RocketFuel
if fType == 5002 then return fType end -- TorpedoFuel
if fType == 5003 then return fType end -- WeaponCoast
end
end
return -1 --invalid fType parameter
} This function accepts either the description or the code, and will return the code ONLY if correct values are passed in. It will return -1 if the input value is in error. This function can then be used in the FillUP function to allow a user to specify any type of fuel for the unit. Some units (like bases) may have multiple fuel types. By allowing the user to specify the fuel type the function, which is simple to understand, can be used in more cases. function Fillup( side, name, fuelType, minVal or 1 )
{
fType = CheckFuelType(fuelType) --check for valid fuel type
if fType < 0 then return end --invalid fuel Type parameter
if type(minVal) ~= 'number' then minVal = 1 --check for minVal type, force to 1 if not a nubmer.
if minVal < 1 then minVal = 1 -- force it to 1 if less than 1
unit = ScenEdit_GetUnit({side=side,name=name}) --get the unit
if unit == nil then return end --invalid unit specification --unit = nil if invalid unit specified.
local newfuel = u.fuel --get the unit's fuel item
if newFuel[fType] == nil then return --unit does not use the type of fuel specified.
if minVal > newFuel[fType].max then return --bad minVal value (too large)
if newfuel[fType].current < minVal then -- if the unit's fuel value is less than the minVal...
newfuel[fType].current = newfuel[fType].max --refuel the unit!
u.fuel = newfuel --save the change to the unit.
end
} This function collapses fuel type from either code or description to code for the user. (It does not handle guid at this time) This function checks for all the error conditions that can happen in the situation. This function has definable action: to fill up fuel for a unit, when it is low. We can specify the unit, the fuel, and the level for triggering the re-fill. We can even ignore the low level, and it will define a minimal amount for us. USAGE: FillUp( side, name, fuelType, minVal) e.g. FullUp('USA', 'USS Boxer', 'DeiselFuel') --minVal automatically set to 1. The nice thing about elegant code is that it always works, and it always works well, and it is easily understood.. this leads me to want to re-use (instead of reinventing.) It also leads to fewer errors when building stuff and running stuff. I know this is completely unsolicited, but we should strive to improve ourselves in all ways... If we endeavor to write code, let us learn the best way to do it. Good Luck
|