Help with fuel script please
Moderators: angster, RoryAndersonCDT, michaelm75au, MOD_Command
- Randomizer
- Posts: 1521
- Joined: Sat Jun 28, 2008 8:31 pm
Help with fuel script please
I am trying to script an even that refuels all of the neutral fishing boats once each day for a multi-day scenario.
Tried the script below but it only changed the first entry, dhow "Nitram Krim"
local u = ScenEdit_SetUnit({name='Nitram Krim', guid='mj68dm-0hltfr138ctdj', name='Nazrim Krim', guid='mj68dm-0hltfr138ctd7', name='Abdulllah', guid='mj68dm-0hltfr138ctd1', name='Jag Rhadna', guid='mj68dm-0hltfr138ct7g', name='Aperradee Narrhee', guid='mj68dm-0hltfr138ctdd'})
local fuel = u.fuel
fuel[3001].current = 200
u.fuel = fuel
Tried to enclose all entries in curly brackets as below but got an error message that I cannot make sense of:
local u = ScenEdit_SetUnit({name='Nitram Krim', guid='mj68dm-0hltfr138ctdj'}, {name='Nazrim Krim', guid='mj68dm-0hltfr138ctd7'}, {name='Abdulllah', guid='mj68dm-0hltfr138ctd1'}, {name='Jag Rhadna', guid='mj68dm-0hltfr138ct7g'}, (name='Aperradee Narrhee', guid='mj68dm-0hltfr138ctdd'})
local fuel = u.fuel
fuel[3001].current = 200
u.fuel = fuel
ERROR: [string "Console"]:1: ')' expected near '='
and
local u = ScenEdit_SetUnit({{name='Nitram Krim', guid='mj68dm-0hltfr138ctdj'}, {name='Nazrim Krim', guid='mj68dm-0hltfr138ctd7'}, {name='Abdulllah', guid='mj68dm-0hltfr138ctd1'}, {name='Jag Rhadna', guid='mj68dm-0hltfr138ct7g'}, (name='Aperradee Narrhee', guid='mj68dm-0hltfr138ctdd'}})
local fuel = u.fuel
fuel[3001].current = 200
u.fuel = fuel
ERROR: [string "Console"]:1: ')' expected near '='
I could script each vessel individually but that's clunky and does not solve for the other requirement of the script; that should any vessel be destroyed, the script will still fire for the remainder without delivering an error message. I think I may be close but am more likely just deluded.
Thanks in advance for any help. I would rather not post the scenario here as it is not yet ready for its public debut but will send it via PM if it helps with the assistance process.
-C
Tried the script below but it only changed the first entry, dhow "Nitram Krim"
local u = ScenEdit_SetUnit({name='Nitram Krim', guid='mj68dm-0hltfr138ctdj', name='Nazrim Krim', guid='mj68dm-0hltfr138ctd7', name='Abdulllah', guid='mj68dm-0hltfr138ctd1', name='Jag Rhadna', guid='mj68dm-0hltfr138ct7g', name='Aperradee Narrhee', guid='mj68dm-0hltfr138ctdd'})
local fuel = u.fuel
fuel[3001].current = 200
u.fuel = fuel
Tried to enclose all entries in curly brackets as below but got an error message that I cannot make sense of:
local u = ScenEdit_SetUnit({name='Nitram Krim', guid='mj68dm-0hltfr138ctdj'}, {name='Nazrim Krim', guid='mj68dm-0hltfr138ctd7'}, {name='Abdulllah', guid='mj68dm-0hltfr138ctd1'}, {name='Jag Rhadna', guid='mj68dm-0hltfr138ct7g'}, (name='Aperradee Narrhee', guid='mj68dm-0hltfr138ctdd'})
local fuel = u.fuel
fuel[3001].current = 200
u.fuel = fuel
ERROR: [string "Console"]:1: ')' expected near '='
and
local u = ScenEdit_SetUnit({{name='Nitram Krim', guid='mj68dm-0hltfr138ctdj'}, {name='Nazrim Krim', guid='mj68dm-0hltfr138ctd7'}, {name='Abdulllah', guid='mj68dm-0hltfr138ctd1'}, {name='Jag Rhadna', guid='mj68dm-0hltfr138ct7g'}, (name='Aperradee Narrhee', guid='mj68dm-0hltfr138ctdd'}})
local fuel = u.fuel
fuel[3001].current = 200
u.fuel = fuel
ERROR: [string "Console"]:1: ')' expected near '='
I could script each vessel individually but that's clunky and does not solve for the other requirement of the script; that should any vessel be destroyed, the script will still fire for the remainder without delivering an error message. I think I may be close but am more likely just deluded.
Thanks in advance for any help. I would rather not post the scenario here as it is not yet ready for its public debut but will send it via PM if it helps with the assistance process.
-C
- Randomizer
- Posts: 1521
- Joined: Sat Jun 28, 2008 8:31 pm
RE: Help with fuel script please
New data, I have a script that works for all so what needs to be added so that it runs correctly and does not produce an error if one or more of the units is destroyed.
Thanks in advance for any or all help.
The script...
local u = ScenEdit_SetUnit({side='Bengali Civilian', name='Aperradee Narrhee', guid='mj68dm-0hltfr138ctdd'})
local fuel = u.fuel
fuel[3001].current = 400
u.fuel = fuel
local u = ScenEdit_SetUnit({side='Bengali Civilian', name='Jag Rhadna', guid='mj68dm-0hltfr138ct7g'})
local fuel = u.fuel
fuel[3001].current = 400
u.fuel = fuel
local u = ScenEdit_SetUnit({side='Bengali Civilian', name='Abdulllah', guid='mj68dm-0hltfr138ctd1'})
local fuel = u.fuel
fuel[3001].current = 400
u.fuel = fuel
local u = ScenEdit_SetUnit({side='Bengali Civilian', name='Nazrim Krim', guid='mj68dm-0hltfr138ctd7'})
local fuel = u.fuel
fuel[3001].current = 400
u.fuel = fuel
local u = ScenEdit_SetUnit({side='Bengali Civilian', name='Nitram Krim', guid='mj68dm-0hltfr138ctdj'})
local fuel = u.fuel
fuel[3001].current = 400
u.fuel = fuel
-C
Thanks in advance for any or all help.
The script...
local u = ScenEdit_SetUnit({side='Bengali Civilian', name='Aperradee Narrhee', guid='mj68dm-0hltfr138ctdd'})
local fuel = u.fuel
fuel[3001].current = 400
u.fuel = fuel
local u = ScenEdit_SetUnit({side='Bengali Civilian', name='Jag Rhadna', guid='mj68dm-0hltfr138ct7g'})
local fuel = u.fuel
fuel[3001].current = 400
u.fuel = fuel
local u = ScenEdit_SetUnit({side='Bengali Civilian', name='Abdulllah', guid='mj68dm-0hltfr138ctd1'})
local fuel = u.fuel
fuel[3001].current = 400
u.fuel = fuel
local u = ScenEdit_SetUnit({side='Bengali Civilian', name='Nazrim Krim', guid='mj68dm-0hltfr138ctd7'})
local fuel = u.fuel
fuel[3001].current = 400
u.fuel = fuel
local u = ScenEdit_SetUnit({side='Bengali Civilian', name='Nitram Krim', guid='mj68dm-0hltfr138ctdj'})
local fuel = u.fuel
fuel[3001].current = 400
u.fuel = fuel
-C
- Randomizer
- Posts: 1521
- Joined: Sat Jun 28, 2008 8:31 pm
RE: Help with fuel script please
Nobody?
Tried the script below but it returns this error: ERROR: [string "Console"]:1: ')' expected near '='
And the script:
function ScenEdit_SetUnit(side='Bengali Civilian')
local unit = ScenEdit_GetUnit({side='Bengali Civilian',name='UnitName'})
if unit ~= nil then local fuel = u.fuel
end
end
local u = ScenEdit_SetUnit({side='Bengali Civilian', name='Aperradee Narrhee', guid='mj68dm-0hltfr138ctdd'})
local fuel = u.fuel
fuel[3001].current = 200
u.fuel = fuel
local u = ScenEdit_SetUnit({side='Bengali Civilian', name='Jag Rhadna', guid='mj68dm-0hltfr138ct7g'})
local fuel = u.fuel
fuel[3001].current = 200
u.fuel = fuel
local u = ScenEdit_SetUnit({side='Bengali Civilian', name='Abdulllah', guid='mj68dm-0hltfr138ctd1'})
local fuel = u.fuel
fuel[3001].current = 200
u.fuel = fuel
local u = ScenEdit_SetUnit({side='Bengali Civilian', name='Nazrim Krim', guid='mj68dm-0hltfr138ctd7'})
local fuel = u.fuel
fuel[3001].current = 200
u.fuel = fuel
local u = ScenEdit_SetUnit({side='Bengali Civilian', name='Nitram Krim', guid='mj68dm-0hltfr138ctdj'})
local fuel = u.fuel
fuel[3001].current = 200
u.fuel = fuel
Tried the script below but it returns this error: ERROR: [string "Console"]:1: ')' expected near '='
And the script:
function ScenEdit_SetUnit(side='Bengali Civilian')
local unit = ScenEdit_GetUnit({side='Bengali Civilian',name='UnitName'})
if unit ~= nil then local fuel = u.fuel
end
end
local u = ScenEdit_SetUnit({side='Bengali Civilian', name='Aperradee Narrhee', guid='mj68dm-0hltfr138ctdd'})
local fuel = u.fuel
fuel[3001].current = 200
u.fuel = fuel
local u = ScenEdit_SetUnit({side='Bengali Civilian', name='Jag Rhadna', guid='mj68dm-0hltfr138ct7g'})
local fuel = u.fuel
fuel[3001].current = 200
u.fuel = fuel
local u = ScenEdit_SetUnit({side='Bengali Civilian', name='Abdulllah', guid='mj68dm-0hltfr138ctd1'})
local fuel = u.fuel
fuel[3001].current = 200
u.fuel = fuel
local u = ScenEdit_SetUnit({side='Bengali Civilian', name='Nazrim Krim', guid='mj68dm-0hltfr138ctd7'})
local fuel = u.fuel
fuel[3001].current = 200
u.fuel = fuel
local u = ScenEdit_SetUnit({side='Bengali Civilian', name='Nitram Krim', guid='mj68dm-0hltfr138ctdj'})
local fuel = u.fuel
fuel[3001].current = 200
u.fuel = fuel
RE: Help with fuel script please
Christopher,Nobody?
FWIW, this Lua code worked for me to keep an aircraft (AF-1) in the air without having it land or be refueled in the air. I've also done it with fishing vessels which ran out of fuel too quickly but unfortunately can't find the LUA I used for that. But, the ships were based on the one for that plane.
I ran a check every 15 minutes and if it met the parameters below refueled the plane.
One problem may be that the local u = ScenEdit_SetUnit({side='Bengali Civilian', name='Aperradee Narrhee', guid='mj68dm-0hltfr138ctdd'}) does not need (or want) both the name & guid fields, either one will suffice.
-WS
u = ScenEdit_GetUnit({side='NORAD',name='Air Force One #1'})
local newfuel = u.fuel
if newfuel[2001].current < 22000 then
newfuel[2001].current = newfuel[2001].max
u.fuel = newfuel
end
“There is no limit to what a man can do so long as he does not care a straw who gets the credit for it.”
Charles Edward Montague, English novelist and essayist
~Disenchantment, ch. 15 (1922)
Charles Edward Montague, English novelist and essayist
~Disenchantment, ch. 15 (1922)
RE: Help with fuel script please
function ScenEdit_SetUnit(side='Bengali Civilian')
why does it start with function? this is already a function. The first 5 lines you have look incorrect to me.
You probably should be doing ScenEdit_GetUnit not SetUnit. You get the unit and set it to a variable so you can access stuff. SetUnit is more to change something about it (fuel can be changed directly though).
---------------
local u = ScenEdit_GetUnit({side='Bengali Civilian', name='Aperradee Narrhee', guid='mj68dm-0hltfr138ctdd'})
if u ~= nil then
local fuel = u.fuel
fuel[3001].current = 200 --or fuel[3001].max for full tank
u.fuel = fuel
end
---------------
If I remember correctly that should be enough for one unit. The nil check may or may not be needed. In the console it will throw an error, but it may work in the game ok. You can use a print statement on each line with no nil check and then put Tool_EmulateNoConsole(true) at the top and you will get what would actually happen in the game.
- Randomizer
- Posts: 1521
- Joined: Sat Jun 28, 2008 8:31 pm
RE: Help with fuel script please
@ Stylesw and Whicker,
Many thanks, gentlemen your input is appreciated and will be noted when the scenario is finished.
The function thing I tried because there was a script written by someone that prevented error messages when Lua attempted to allocate aircraft that had been destroyed to a mission. It seemed reasonable that the same exclusion principals might apply to fueling ships which has been sunk. As can be seen, my ignorance of machine language is very deep indeed.
Thank you again.
-C
Many thanks, gentlemen your input is appreciated and will be noted when the scenario is finished.
The function thing I tried because there was a script written by someone that prevented error messages when Lua attempted to allocate aircraft that had been destroyed to a mission. It seemed reasonable that the same exclusion principals might apply to fueling ships which has been sunk. As can be seen, my ignorance of machine language is very deep indeed.
These instructions are a perfect example of my profound Lua ignorance. Although clear and concise, to me the reads as if it was written in Sanskrit and will take a lot of time, effort and experimenting to parse into something usable for game purposes. You might assess that insofar as Lua scripting CMO scenarios goes I am getting in over my head and I could not really disagree.If I remember correctly that should be enough for one unit. The nil check may or may not be needed. In the console it will throw an error, but it may work in the game ok. You can use a print statement on each line with no nil check and then put Tool_EmulateNoConsole(true) at the top and you will get what would actually happen in the game.
Thank you again.
-C
RE: Help with fuel script please
Glad to help. Even having been a professional programmer, LUA with CMANO/CMO was a new scripting language for me that often requires frequent references to its documentation.Many thanks, gentlemen your input is appreciated and will be noted when the scenario is finished.
We all have our skills and best not to ask me to fix a car[:D]!
-WS
“There is no limit to what a man can do so long as he does not care a straw who gets the credit for it.”
Charles Edward Montague, English novelist and essayist
~Disenchantment, ch. 15 (1922)
Charles Edward Montague, English novelist and essayist
~Disenchantment, ch. 15 (1922)
RE: Help with fuel script please
just keep trying, you learn a little bit each day.. main thing is to be curious and persistent. Did you see the lua vids I made on youtube? there are several, tried to go over the basics. Search YT for `CMANO Lua`.
- Randomizer
- Posts: 1521
- Joined: Sat Jun 28, 2008 8:31 pm
RE: Help with fuel script please
Just ran and tested and it works! A refueling event will trigger every six-game hours and the small boats that will crowd the targets area automatically top up their tanks. Tested both with and without losses and deletions with no error messages.
For anyone else, here's the script. The null value is necessary for the script to continue to run past any ship that is sunk. Obviously edits to side, name, fuel type and quantity are necessary to use the script in other scenarios. Looks like it can be expanded as necessary.
u = ScenEdit_GetUnit({side='Bengali Civilian',name='Aperradee Narrhee'})
if u ~= nil then
local newfuel = u.fuel
if newfuel[3001].current < 300 then
newfuel[3001].current = newfuel[3001].max
u.fuel = newfuel
end
end
u = ScenEdit_GetUnit({side='Bengali Civilian',name='Jag Rhadna'})
if u ~= nil then
local newfuel = u.fuel
if newfuel[3001].current < 300 then
newfuel[3001].current = newfuel[3001].max
u.fuel = newfuel
end
end
u = ScenEdit_GetUnit({side='Bengali Civilian',name='Abdullah'})
if u ~= nil then
local newfuel = u.fuel
if newfuel[3001].current < 300 then
newfuel[3001].current = newfuel[3001].max
u.fuel = newfuel
end
end
u = ScenEdit_GetUnit({side='Bengali Civilian',name='Nazrim Krim'})
if u ~= nil then
local newfuel = u.fuel
if newfuel[3001].current < 300 then
newfuel[3001].current = newfuel[3001].max
u.fuel = newfuel
end
end
u = ScenEdit_GetUnit({side='Bengali Civilian',name='Nitram Krim'})
if u ~= nil then
local newfuel = u.fuel
if newfuel[3001].current < 300 then
newfuel[3001].current = newfuel[3001].max
u.fuel = newfuel
end
end
Again, Thank You Both!
-C
For anyone else, here's the script. The null value is necessary for the script to continue to run past any ship that is sunk. Obviously edits to side, name, fuel type and quantity are necessary to use the script in other scenarios. Looks like it can be expanded as necessary.
u = ScenEdit_GetUnit({side='Bengali Civilian',name='Aperradee Narrhee'})
if u ~= nil then
local newfuel = u.fuel
if newfuel[3001].current < 300 then
newfuel[3001].current = newfuel[3001].max
u.fuel = newfuel
end
end
u = ScenEdit_GetUnit({side='Bengali Civilian',name='Jag Rhadna'})
if u ~= nil then
local newfuel = u.fuel
if newfuel[3001].current < 300 then
newfuel[3001].current = newfuel[3001].max
u.fuel = newfuel
end
end
u = ScenEdit_GetUnit({side='Bengali Civilian',name='Abdullah'})
if u ~= nil then
local newfuel = u.fuel
if newfuel[3001].current < 300 then
newfuel[3001].current = newfuel[3001].max
u.fuel = newfuel
end
end
u = ScenEdit_GetUnit({side='Bengali Civilian',name='Nazrim Krim'})
if u ~= nil then
local newfuel = u.fuel
if newfuel[3001].current < 300 then
newfuel[3001].current = newfuel[3001].max
u.fuel = newfuel
end
end
u = ScenEdit_GetUnit({side='Bengali Civilian',name='Nitram Krim'})
if u ~= nil then
local newfuel = u.fuel
if newfuel[3001].current < 300 then
newfuel[3001].current = newfuel[3001].max
u.fuel = newfuel
end
end
Again, Thank You Both!
-C
- michaelm75au
- Posts: 12457
- Joined: Sat May 05, 2001 8:00 am
- Location: Melbourne, Australia
RE: Help with fuel script please
Now you have that you could create a function to call
You could expand the function to handle more than just the type of fuel '3001', or create other functions for the other types of fuel.
function Fillup3001( side, name )
{
u = ScenEdit_GetUnit({side=side,name=name})
if u ~= nil then
local newfuel = u.fuel
if newfuel[3001].current < 300 then
newfuel[3001].current = newfuel[3001].max
u.fuel = newfuel
end
end
}
Fillup3001('Bengali Civilian','Aperradee Narrhee')
Fillup3001('Bengali Civilian','Jag Rhadna')
Fillup3001('Bengali Civilian','Abdullah')
Fillup3001('Bengali Civilian','Nazrim Krim')
Fillup3001('Bengali Civilian','Nitram Krim')
You could expand the function to handle more than just the type of fuel '3001', or create other functions for the other types of fuel.
Michael
- Randomizer
- Posts: 1521
- Joined: Sat Jun 28, 2008 8:31 pm
RE: Help with fuel script please
I suspected that there may be a more elegant solution but I have a script that works so will stick with it, being as how my last sortie into functions ended badly. However, have copied your function script to my Lua notes so it may appear in some other scenario.
Thanks.
-C
Thanks.
-C
RE: Help with fuel script please
I used this script and it it was very helpful. I can't get it to work for China Type 055 (#2834) because the fuel type is different. It is gas not diesel. Does anyone have a list of the codes for the fuel types? Specifically gas as used by the Type 055?
Thanks
Thanks
RE: Help with fuel script please
[quote] Does anyone have a list of the codes for the fuel types? Specifically gas as used by the Type 055? /quote]
Try this located at https://commandlua.github.io/#table_Fuel
Description = Code
NoFuel = 1001
AviationFuel = 2001
DieselFuel = 3001
OilFuel = 3002
GasFuel = 3003
Battery = 4001
AirIndepedent = 4002
RocketFuel = 5001
TorpedoFuel = 5002
WeaponCoast = 5003
-WS
Try this located at https://commandlua.github.io/#table_Fuel
Description = Code
NoFuel = 1001
AviationFuel = 2001
DieselFuel = 3001
OilFuel = 3002
GasFuel = 3003
Battery = 4001
AirIndepedent = 4002
RocketFuel = 5001
TorpedoFuel = 5002
WeaponCoast = 5003
-WS
“There is no limit to what a man can do so long as he does not care a straw who gets the credit for it.”
Charles Edward Montague, English novelist and essayist
~Disenchantment, ch. 15 (1922)
Charles Edward Montague, English novelist and essayist
~Disenchantment, ch. 15 (1922)
- michaelm75au
- Posts: 12457
- Joined: Sat May 05, 2001 8:00 am
- Location: Melbourne, Australia
RE: Help with fuel script please
http://commandlua.github.io/#table_Fuel
Fuel types are listed
Fuel types are listed
Michael
RE: Help with fuel script please
@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
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.
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.
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
@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
Code: Select all
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 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.
Code: Select all
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.
Code: Select all
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
- michaelm75au
- Posts: 12457
- Joined: Sat May 05, 2001 8:00 am
- Location: Melbourne, Australia
RE: Help with fuel script please
The fuel types are there - you need to click on it to expand the list.


- Attachments
-
- fuellist1.jpg (125.24 KiB) Viewed 508 times
Michael

