Matrix Games Forums

Forums  Register  Login  Photo Gallery  Member List  Search  Calendars  FAQ 

My Profile  Inbox  Address Book  My Subscription  My Forums  Log Out

Help with fuel script please

 
View related threads: (in this forum | in all forums)

Logged in as: Guest
Users viewing this topic: none
  Printable Version
All Forums >> [New Releases from Matrix Games] >> Command: Modern Operations series >> Mods and Scenarios >> Lua Legion >> Help with fuel script please Page: [1]
Login
Message << Older Topic   Newer Topic >>
Help with fuel script please - 2/12/2020 4:36:00 PM   
Randomizer


Posts: 1473
Joined: 6/28/2008
Status: offline
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

Post #: 1
RE: Help with fuel script please - 2/12/2020 7:06:01 PM   
Randomizer


Posts: 1473
Joined: 6/28/2008
Status: offline
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

(in reply to Randomizer)
Post #: 2
RE: Help with fuel script please - 2/13/2020 8:28:20 PM   
Randomizer


Posts: 1473
Joined: 6/28/2008
Status: offline
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

(in reply to Randomizer)
Post #: 3
RE: Help with fuel script please - 2/13/2020 11:29:38 PM   
stilesw


Posts: 1497
Joined: 6/26/2014
From: Hansville, WA, USA
Status: offline
quote:

Nobody?

Christopher,

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


< Message edited by stilesw -- 2/13/2020 11:38:01 PM >


_____________________________

“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)

(in reply to Randomizer)
Post #: 4
RE: Help with fuel script please - 2/14/2020 2:55:00 AM   
Whicker

 

Posts: 664
Joined: 6/20/2018
Status: offline
quote:

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.

(in reply to stilesw)
Post #: 5
RE: Help with fuel script please - 2/14/2020 3:41:05 PM   
Randomizer


Posts: 1473
Joined: 6/28/2008
Status: offline
@ 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.
quote:

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.

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.

Thank you again.

-C

(in reply to Whicker)
Post #: 6
RE: Help with fuel script please - 2/14/2020 4:22:27 PM   
stilesw


Posts: 1497
Joined: 6/26/2014
From: Hansville, WA, USA
Status: offline
quote:


Many thanks, gentlemen your input is appreciated and will be noted when the scenario is finished.

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.

We all have our skills and best not to ask me to fix a car!

-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)

(in reply to Randomizer)
Post #: 7
RE: Help with fuel script please - 2/14/2020 5:09:07 PM   
Whicker

 

Posts: 664
Joined: 6/20/2018
Status: offline
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`.

(in reply to stilesw)
Post #: 8
RE: Help with fuel script please - 2/14/2020 6:16:08 PM   
Randomizer


Posts: 1473
Joined: 6/28/2008
Status: offline
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

(in reply to Whicker)
Post #: 9
RE: Help with fuel script please - 2/14/2020 11:36:37 PM   
michaelm75au


Posts: 13500
Joined: 5/5/2001
From: Melbourne, Australia
Status: offline
Now you have that you could create a function to call
quote:

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

(in reply to Randomizer)
Post #: 10
RE: Help with fuel script please - 2/14/2020 11:40:20 PM   
Randomizer


Posts: 1473
Joined: 6/28/2008
Status: offline
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

(in reply to michaelm75au)
Post #: 11
RE: Help with fuel script please - 8/5/2020 5:55:19 PM   
orca

 

Posts: 501
Joined: 11/6/2013
Status: offline
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

(in reply to Randomizer)
Post #: 12
RE: Help with fuel script please - 8/5/2020 6:55:07 PM   
stilesw


Posts: 1497
Joined: 6/26/2014
From: Hansville, WA, USA
Status: offline
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

_____________________________

“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)

(in reply to orca)
Post #: 13
RE: Help with fuel script please - 8/6/2020 8:49:02 AM   
michaelm75au


Posts: 13500
Joined: 5/5/2001
From: Melbourne, Australia
Status: offline
http://commandlua.github.io/#table_Fuel
Fuel types are listed

_____________________________

Michael

(in reply to stilesw)
Post #: 14
RE: Help with fuel script please - 8/26/2020 3:43:13 PM   
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




(in reply to michaelm75au)
Post #: 15
RE: Help with fuel script please - 8/26/2020 11:09:55 PM   
michaelm75au


Posts: 13500
Joined: 5/5/2001
From: Melbourne, Australia
Status: offline
The fuel types are there - you need to click on it to expand the list.




Attachment (1)

_____________________________

Michael

(in reply to jkgarner)
Post #: 16
Page:   [1]
All Forums >> [New Releases from Matrix Games] >> Command: Modern Operations series >> Mods and Scenarios >> Lua Legion >> Help with fuel script please Page: [1]
Jump to:





New Messages No New Messages
Hot Topic w/ New Messages Hot Topic w/o New Messages
Locked w/ New Messages Locked w/o New Messages
 Post New Thread
 Reply to Message
 Post New Poll
 Submit Vote
 Delete My Own Post
 Delete My Own Thread
 Rate Posts


Forum Software © ASPPlayground.NET Advanced Edition 2.4.5 ANSI

0.750