Operation Flashpoint Dragon Rising OFDR Forums
Would you like to react to this message? Create an account in a few clicks or log in to continue.

custom LUA functions

4 posters

Go down

custom LUA functions Empty custom LUA functions

Post by TemplarGFX Thu 03 Dec 2015, 00:35

Here are a bunch of functions I use regularly that people may find helpful

Gets a new position using direction and distance values
Code:
--originalPosition = table containing x,y,z coordinates.  use OFP:getPosition
--distance = distance in units from the starting position
--direction = direction in degrees from the starting position
--returns table containing new x,y,z coordinates of position
function positionByDirection(originalPosition, distance, direction)
  origx = originalPosition[1]
  origz = originalPosition[3]
  newx = origx + math.cos(direction*3.14/180) * distance
  newz = origz + math.sin(direction*3.14/180) * distance
  newy = OFP:getTerrainHeight(newx, newz)
  return {newx, newy, newz}
end

gets the direction between two points
Code:
--radians = angle in radians to be converted to degrees (for use with atan2 function)
--returns angle in degrees
function radiansToDegrees(radians)
  return radians*(180/3.14)
end

--startPosition = table containing x,y,z position of start point
--endPosition = table containing x,y,z position of end point
--returns the angle in degrees from the start position to end position
function pointDirection(startPosition, endPosition)
  return radiansToDegrees(math.atan2(endPosition[3]-startPosition[3], endPosition[1]-startPosition[1]))
end

Gets the distance between two points
Code:
--startPosition = table containing x,y,z position of start point
--endPosition = table containing x,y,z position of end point
returns the distance in units between the two positions (like OFP:getDistance)
function pointDistance (startPosition, endPosition)
  local result_position = {endPosition[1] - startPosition[1], endPosition[2] - startPosition[2], endPosition[3] - startPosition[3]}
  return math.sqrt(result_position[1] * result_position[1] + result_position[2] * result_position[2] + result_position[3] * result_position[3])
end

Gets a random position around the initial position within specified distances
Code:
--startPosition = table containing x,y,z position of start point
--minDistance = minimum distance new position can be from start position
--maxDistance = maximum distance new position can be from start position
returns table containing new x,y,z position
function getRandomPosition(startPosition, minDistance, maxDistance)
  distance = math.random(mindDistance, maxDistance)
  direction = math.random(0, 359)
  return positionByDirection(startPosition, distance, direction)
end

Limits a value to specified min and max, with looping ability (for angles)
Code:
--inputValue = the value to limit
--minValue = minimum value input can be
--maxValue = maximum value input can be
--loop = if true, inputValue will be adjusted by maxValue until it fits inside min and max (useful for angle calculations in degrees or radians)
--loop = if false, returns minValue or maxValue if input outside of limit
--returns the limited value
function limitValue(inputValue, minValue, maxValue, loop)
  if loop == false then
    if inputValue < minValue then
     inputValue = minValue
   end
    if inputValue > maxValue then
     inputValue = maxValue
   end
   return inputValue
  else
    while inputValue > maxValue do
     inputValue = inputValue - maxValue
   end
    while inputValue < minValue do
     inputValue = inputValue + maxValue
   end
  end
end

checks to see if the terrain is blocking the line of sight between two entities
Code:
--viewingEntity = the entity that is doing the looking
--targetEntity = the entity that the viewer is trying to look at
--entityHeight = how tall the viewing entity is (1.5 for soldiers is good)
--stepSize = amount of distance in units to check for blocking terrain
--returns true if the terrain is not blocking the line of site or false if it is
function canSee(viewingEntity, targetEntity, entityHeight, stepSize)
  start_pos = OFP:getPosition(viewingEntity)
  start_height = OFP:getTerrainHeight(viewingEntity)
  end_pos = OFP:getPosition(targetEntity)
  direction = pointDirection(start_pos, end_pos)
  distance = OFP:getDistance(viewingEntity, targetEntity)
  steps = distance / stepSize
  can_see = true
  for i=1, steps do
    test_distance = stepSize * i
   if test_distance > distance then
     test_distance = distance
     i = steps + 1
   end
    new_position = positionByDirection(start_pos, direction, test_distance)
   if new_position[2] > start_height + entityHeight then
     can_see = false
   end
  end
  return can_see
end

attempts to merge units of one echelon into another if there is space
Code:
--echelonName = the echelon we want to merge with another
--tableOfEchelons = table containing names of echelons we want to try and merge with
--maxDistance = the maximum distance any of the merging echelons can be
--returns true if the members of echelonName have been attached to another echelon and false if not
function mergeEchelons(echelonName, tableOfEchelons, maxDistance)
  echelon_size = OFP:getEchelonSize(echelonName)
  members = {}
  for i=0, echelon_size do
    table.insert(members, OFP:getEchelonMember(echelonName, i))
  end
  chosen_echelon = ""
  for i=1, #tableOfEchelons do
    if tableOfEchelons[i] ~= echelonName then
     index_size = OFP:getEchelonSize(tableOfEchelons[i])
     if squad_max_size - index_size >= echelon_size and OFP:getDistance(echelonName, tableOfEchelons[i]) < maxDistance then
       chosen_echelon = tableOfEchelons[i]
       break
       end
   end
  end
  if chosen_echelon ~= "" then
    for i=1, #members do
     OFP:attach(members[i], chosen_echelon)
   end
    return true
  end
  return false
end

TemplarGFX
Veteran

Points : 159
Reputation : 18
Join date : 2013-02-20

Back to top Go down

custom LUA functions Empty Re: custom LUA functions

Post by TheGeneral Sun 06 Dec 2015, 19:14

oooo....! hello mate, thanks for the posting of some of your custom work.
TheGeneral
TheGeneral
Admin

Points : 780
Reputation : 35
Join date : 2012-12-15

http://maniacaldog.wix.com/keepdralive

Back to top Go down

custom LUA functions Empty Re: custom LUA functions

Post by TemplarGFX Sun 06 Dec 2015, 21:49

Hello MDog!

I'm back on OFDR for a while, just can't find anything to replace it.  ARMA just doesn't feel the same for me!

TemplarGFX
Veteran

Points : 159
Reputation : 18
Join date : 2013-02-20

Back to top Go down

custom LUA functions Empty Re: custom LUA functions

Post by TheGeneral Mon 07 Dec 2015, 09:10

I know what you mean dude. I do like the sim/immersion side in Arma though. Which I only came close to creating in DR with Tjdagger when we made nightshade.

I've just started a udemy course so I might end back to it as well. It's been a while.
TheGeneral
TheGeneral
Admin

Points : 780
Reputation : 35
Join date : 2012-12-15

http://maniacaldog.wix.com/keepdralive

Back to top Go down

custom LUA functions Empty Re: custom LUA functions

Post by dewi316 Tue 08 Dec 2015, 20:07

TemplarGFX wrote:

I'm back on OFDR for a while, just can't find anything to replace it.  ARMA just doesn't feel the same for me!

Excellent news there are a handfull of people who can really make this game sing, awaiting new orders Sir,  ssshhh whisper it Island War 3.   Twisted Evil


Last edited by dewi316 on Wed 09 Dec 2015, 21:43; edited 1 time in total
dewi316
dewi316
Veteran

Location : swansea
Points : 179
Reputation : 22
Join date : 2012-12-19

Back to top Go down

custom LUA functions Empty Re: custom LUA functions

Post by TheGeneral Tue 08 Dec 2015, 22:30

Island war 3 numdnuts!
TheGeneral
TheGeneral
Admin

Points : 780
Reputation : 35
Join date : 2012-12-15

http://maniacaldog.wix.com/keepdralive

Back to top Go down

custom LUA functions Empty Re: custom LUA functions

Post by tvig0r0us Thu 10 Dec 2015, 00:35

TemplarGFX wrote:Hello MDog!

I'm back on OFDR for a while, just can't find anything to replace it.  ARMA just doesn't feel the same for me!
I'd say you are in no way alone in that assertion. I'm frankly not sure that I'll ever find anything that replaces this game. Welcome back!

tvig
tvig0r0us
tvig0r0us
Admin

Location : Nky
Points : 266
Reputation : 29
Join date : 2013-10-16

http://www.moddb.com/members/tvig0r0us

Back to top Go down

custom LUA functions Empty Re: custom LUA functions

Post by TemplarGFX Thu 10 Dec 2015, 04:27

tvig0r0us wrote:
TemplarGFX wrote:Hello MDog!

I'm back on OFDR for a while, just can't find anything to replace it.  ARMA just doesn't feel the same for me!
I'd say you are in no way alone in that assertion. I'm frankly not sure that I'll ever find anything that replaces this game. Welcome back!

tvig


Im glad to see so many "old" names still around!


Im currently working on a little present for the community (no its not IW 3 LOL)!

It really annoyed me returning to OFDR this time around that the help file was so unhelpful, didn't contain any basic lua functions, and was missing alot of information.

So I came up with a way to integrate command and function help directly into the LUA Editor! So I have been going over EVERY command (both from OFP, and LUA core functions) putting concise and detailed help information into it!

I have completed the LUA string, IO, OS, table, math and core libraries and just on the final leg of fixing the help information for the OFP, debug and waypoint commands (which is going to take a few days at least LOL).

Im doing a "dump" of my knowledge into this on each command so that everyone has immediate access to a fully comprehensive help system (that's accurate!) that contains practically everything including EVERY OFP specific command! and all the LUA commands we use on a regular basis!

TemplarGFX
Veteran

Points : 159
Reputation : 18
Join date : 2013-02-20

Back to top Go down

custom LUA functions Empty Re: custom LUA functions

Post by TheGeneral Thu 10 Dec 2015, 08:08

https://youtu.be/-FucbvoFFy0
TheGeneral
TheGeneral
Admin

Points : 780
Reputation : 35
Join date : 2012-12-15

http://maniacaldog.wix.com/keepdralive

Back to top Go down

custom LUA functions Empty Re: custom LUA functions

Post by TemplarGFX Tue 15 Dec 2015, 03:20

Wow am I going overboard with this LOL

Any information missing from the ME that you can think of including?  Nothing too huge character-count wise!

So far I've got all the stuff with the AI commands, tables of all the predefined strings, some handy custom functions and a contextual and paged help system!

TemplarGFX
Veteran

Points : 159
Reputation : 18
Join date : 2013-02-20

Back to top Go down

custom LUA functions Empty Re: custom LUA functions

Post by TheGeneral Tue 15 Dec 2015, 10:21

**** me dude! You have been busy.
TheGeneral
TheGeneral
Admin

Points : 780
Reputation : 35
Join date : 2012-12-15

http://maniacaldog.wix.com/keepdralive

Back to top Go down

custom LUA functions Empty Re: custom LUA functions

Post by Sponsored content


Sponsored content


Back to top Go down

Back to top

- Similar topics

 
Permissions in this forum:
You cannot reply to topics in this forum