Home   Help Search Login Register  

Author Topic: Creating a mortar script (NOW solved- check out the function solution)  (Read 3216 times)

0 Members and 1 Guest are viewing this topic.

Offline LeeHunt

  • Former Staff
  • ****
  • John 21:25
I spent a couple hours looking through artillery scripts in the COMREF and trying on my own, but I can't figure out how to create a target out of thin air at a random location��  :dunno:.��  I could have missed something obvious, but it doesn't seem that easy.  Does anyone know how to set a gamelogic to a quasi-random spot within an area the player is aiming at, without using a mapclick command?��  (i'd like to have the player just set his direction, choose a mortar angle, and thump the shell goes off into the distance)

I am trying to write a mortar script (the insurgent's best friend, next to the satchel "IED" of course) where shells will land X distance from the player with some random dispersion in an area.��  The artillery scripts in the COMREF all have some sort of target designated, which is fine, but the only one with any randomness targets the nearest object and not a random piece of thin air on the map.��  So you can't "Camcreate" the shell at an array position, only at an object like a game logic I'm assuming.��  So there's my question again, how do i semi-randomly place game logics based on the player's position?��  Thanks!��  I appreciate any help!

Below is what I've come up with so far trying to piece together different scripts, but i'm confused with sin and cos (what grade did I get in geometry in school?)

_mortardirection = getdir player
_mortarposition = getpos player

_dist = 90 + (random 20)

_mposx = _mortarposition select 0
_mposy = _mortarposition select 1
_mposz = _mortarposition select 2

hint format ["Mortar angle set at 70 degrees, direction set at %1, press cancel to start over",_mortardirection]

~3

shell = "Bo_GBU12_LGB" camCreate [_mposx + ((sin _mortardirection) * _dist), _mposy + ((cos _mortardirection) * _dist), _mposz]

MortarshellsLeft = Mortarshellsleft - 1

exit

« Last Edit: 16 Apr 2007, 01:30:03 by Lee »

Offline Mr.Peanut

  • Former Staff
  • ****
  • urp!
More details would help.  Are you trying to let the player call in a mortar strike at an approximate location? Or do you want the player to specify an angle and direction and try and hit the target by trial and error?

Why not let player have a howitzer and do it all himself? And why do you not want a mapclick?

There is a great new command that spits out weapon direction, even for binoculars. This way the player could look throuhg binos and use an action menu to tell the mortar to start firing. The only trick then is translating where the player is looking into a map location. I wish there was a simple command for that.

More details please.
urp!

Offline bedges

  • Administrator
  • *****
    • OFPEC The Editing Center
slight amendment to your script. this will produce *something*, but whether it's what is required is another question. the shells will all fall up to 110 metres in front of the player....

Code: [Select]
_mortardirection = getdir player
_mortarposition = getpos player
_dist = 90 + (random 20)

_mposx = _mortarposition select 0
_mposy = _mortarposition select 1
_mposz = _mortarposition select 2

hint format ["Mortar angle set at 70 degrees, direction set at %1, press cancel to start over",_mortardirection]
~3

shell = "Bo_GBU12_LGB" camCreate [_mposx + _dist *(sin _mortardirection), _mposy + _dist*(cos _mortardirection), _mposz]
MortarshellsLeft = Mortarshellsleft - 1
exit

if what you want are shells appearing all round the player at random distances, you need to make the direction random, as well as the distance.

Code: [Select]
_mortardirection = (random 360)
_mortarposition = getpos player
_dist = 90 + (random 20)

_mposx = _mortarposition select 0
_mposy = _mortarposition select 1
_mposz = _mortarposition select 2

hint format ["Mortar angle set at 70 degrees, direction set at %1, press cancel to start over",_mortardirection]
~3

shell = "Bo_GBU12_LGB" camCreate [_mposx + _dist *(sin _mortardirection), _mposy + _dist*(cos _mortardirection), _mposz]
MortarshellsLeft = Mortarshellsleft - 1
exit

Offline LeeHunt

  • Former Staff
  • ****
  • John 21:25
Thanks gentlemen for responding.

To better answer your question Mr. Peanut, i am trying have the player use trial and error by only setting two variables for himself, his direction and the angle of the mortar [In this mission, the player is an insurgent who found a mortar and has no training.  The player just has to try it out and see what works, thus no map click or howitzer.] 

I tried that Bedges, couldn't get it work for some reason, did it work when you tried it?   Maybe I am missing something?  I got no explosion or anything.  So still not sure how to camcreate the shell or a game logic or something based on the player's direction and a random distance away...

Offline Mandoble

  • Former Staff
  • ****
    • Grunt ONE and MandoMissile suite
Had this in one test mission, may be useful for you.

Just create a mission, put a simple soldier as player and create two scripts: init.sqs and mortar_keys.sqf

init.sqs:
Code: [Select]
~1
mortar_angle = 25
(findDisplay 46) displaySetEventHandler ["KeyDown", "_res = _this execVM""mortar_keys.sqf"""]
exit

And now mortar_keys.sqf
Code: [Select]
/* mortar_keys.sqf
By Mandoble April 2007

Controls a "virtual mortar"
T to increase angle
Y to decrease angle
U to fire

Change _muzzlevel to desired muzzle velocity in m/s
*/

_key = _this select 1;

if (_key == 20) then
{
// T Pressed
   if (mortar_angle < 85) then {mortar_angle = mortar_angle + 2;};
   hint format["Elevation: %1",mortar_angle];
};

if (_key == 21) then
{
// Y key pressed
   if (mortar_angle > 5) then {mortar_angle = mortar_angle - 2;};
   hint format["Elevation: %1",mortar_angle];
};

if ((_key == 22) && (vehicle player == player)) then
{
// U key pressed, firing missile
   _muzzlevel = 80;

   _type = "Bo_GBU12_LGB";
// Other types you may use:
// Sh_122_HE
// Sh_105_HE
// Sh_125_HE


   _vdir = player weaponDirection (weapons player select 0);
   _dir = (_vdir select 0) atan2 (_vdir select 1);
   _posini = [(getPos player select 0)+sin(_dir)*2,(getPos player select 1)+cos(_dir)*2, 0.2];

   _dhr = cos(mortar_angle);
   _vdir = [sin(_dir)*_dhr, cos(_dir)*_dhr, sin(mortar_angle)];
   _vel = [(_vdir select 0)*_muzzlevel,(_vdir select 1)*_muzzlevel,(_vdir select 2)*_muzzlevel];
   _ur = cos(mortar_angle+90);
   _vup = [sin(_dir)*_ur,cos(_dir)*_ur,sin(mortar_angle+90)];

   _mortar = _type createVehicle _posini;
   _mortar setVectorDir _vdir;
   _mortar setVectorUp _vup;
   _mortar setVelocity _vel;
};

Forgot to say, press T or Y to adjust elevation, U to fire the mortar.
« Last Edit: 15 Apr 2007, 23:07:51 by Mandoble »

Offline LeeHunt

  • Former Staff
  • ****
  • John 21:25
Hahaha, absolutely fantastic!!!   :clap:  Thank you Mandoble!!!!!!  :good:

I give you many thanks and many credits in my mission (which you may enjoy, though its probably another couple months away, some 50 scripts already etc etc).   And to all reading this I suggest you give Mandoble's mortar it a try, lotta fun.  Feels a little bit like those water balloon sling shot cannons I made as a kid...


Offline LeeHunt

  • Former Staff
  • ****
  • John 21:25
Of course now i have a follow on question on using the mortar function you created Mandoble:

1) How do you know which keys on the keyboard correspond to which numbers?  I did not see anything in the COMREF on it etc. (would like to change it to page down, page up etc)

2) How can I set a limit on the ammo usage, within the function or in a script?

3) and thanks again  :D

Offline Mandoble

  • Former Staff
  • ****
    • Grunt ONE and MandoMissile suite
1) displaySetEventHandler and key codes

2) add a
Code: [Select]
mortar_left = 20 global variable to init.sqs, then change
Code: [Select]
if ((_key == 22) && (vehicle player == player)) then by
Code: [Select]
if ((_key == 22) && (vehicle player == player) && (mortar_left > 0)) then in mortar_keys.sqf
 and finally add
Code: [Select]
mortar_left = mortar_left - 1; just after
Code: [Select]
_mortar setVelocity _vel; also in mortar_keys.sqf.

3) not needed  :P

4) You missed that one: How to simulate a real mortar ratio-of-fire so that player cannot just spend all the shells in one second? That is, to simulate the re-load time. (not replying to this one on purpose  >:D )

Offline h-

  • OFPEC Site
  • Administrator
  • *****
  • Formerly HateR_Kint
    • OFPEC
Lee, do not double post.
Edit your post instead of replying to it.
Double posting is allowed only after a longer time has passed since the previous post, for example 3 days is long enough..
Project MCAR   ---   Northern Fronts   ---   Emitter 3Ditor
INFORMATIVE THREAD TITLES PLEASE. "PLEASE HELP" IS NOT ONE..
Chuck Norris can divide by zero.

Offline Mr.Peanut

  • Former Staff
  • ****
  • urp!
So what exactly is display 46? And is there a list of standard display units anywhere?
urp!

Offline Mandoble

  • Former Staff
  • ****
    • Grunt ONE and MandoMissile suite
46 is just main display. Anyother display should be created by you, but, probably, main MAP display has its own and unique ID.

Offline Mandoble

  • Former Staff
  • ****
    • Grunt ONE and MandoMissile suite
now ROF and rounds left are considered:

init.sqs
Code: [Select]
~1
mortar_angle = 45
mortar_left  = 20
last_shell_time = 0
(findDisplay 46) displaySetEventHandler ["KeyDown", "_res = _this execVM""mortar_keys.sqf"""]
exit

mortar_keys.sqf
Code: [Select]
/* mortar_keys.sqf
By Mandoble April 2007

Controls a "virtual mortar"
T to increase angle
Y to decrease angle
U to fire

Change _muzzlevel to desired muzzle velocity in m/s
*/

_key = _this select 1;

if (_key == 20) then
{
// T Pressed
   if (mortar_angle < 85) then {mortar_angle = mortar_angle + 2;};
   hint format["Elevation: %1",mortar_angle];
};

if (_key == 21) then
{
// Y key pressed
   if (mortar_angle > 5) then {mortar_angle = mortar_angle - 2;};
   hint format["Elevation: %1",mortar_angle];
};

if ((_key == 22) && (vehicle player == player)) then
{
// U key pressed, firing missile
   if (mortar_left > 0) then
   {
//    5 seconds to reload the mortar
      _reload_time = 5;

      if ((dayTime * 3600) > (last_shell_time + _reload_time)) then
      {
         _muzzlevel = 80;

         _type = "Bo_GBU12_LGB";
// Other types you may use:
// Sh_122_HE
// Sh_105_HE
// Sh_125_HE


         _vdir = player weaponDirection (weapons player select 0);
         _dir = (_vdir select 0) atan2 (_vdir select 1);
         _posini = [(getPos player select 0)+sin(_dir)*2,(getPos player select 1)+cos(_dir)*2, 0.2];

         _dhr = cos(mortar_angle);
         _vdir = [sin(_dir)*_dhr, cos(_dir)*_dhr, sin(mortar_angle)];
         _vel = [(_vdir select 0)*_muzzlevel,(_vdir select 1)*_muzzlevel,(_vdir select 2)*_muzzlevel];
         _ur = cos(mortar_angle+90);
         _vup = [sin(_dir)*_ur,cos(_dir)*_ur,sin(mortar_angle+90)];

         _mortar = _type createVehicle _posini;
         _mortar setVectorDir _vdir;
         _mortar setVectorUp _vup;
         _mortar setVelocity _vel;
         last_shell_time = (dayTime * 3600);
         mortar_left = mortar_left - 1;
         hint format["%1 mortar shells left", mortar_left];
      }
      else
      {
         cutText["Still reloading", "PLAIN DOWN"];
      };
   }
   else
   {
      cutText["You ran out of shells", "PLAIN DOWN"];
   };
};

There is an important factor to be considered here, ArmA shells (bombs or whatever else) have a life-time and once elapsed the shell is automatically deleted. Using real mortar muzzle velocities like 250 m/s and an angle of 45 degrees or more, you would reach real mortar ranges (several kilometers 4~12) but the shell may be automatically deleted before hitting the ground. Bo_GBU12_LGB is used there because it has a life-time of about 120 seconds, more than enough to reach 12 km ;)

Offline Trexian

  • Members
  • *
Is there some reference source for a USian... err English keyboard and the codes?  I've googled, but can't seem to find what I'm looking for.  The German list in the link from Mandoble is helpful, but I'm a bit concerned there may be discrepancies.  For example, in the list on the link it says "undefined" for + which seems odd.

Many thanks!
Sic semper tyrannosauro.

Offline Luke

  • Members
  • *
  • Thank God! The OFPEC Staff are here!
Trexian,

Check the "command" reference:D

particularly here

 :dunno: concerning the undefined "+"

Hope this helps!!

Luke
Pesky Human!!
Wort Wort Wort.

Offline Trexian

  • Members
  • *
:D

Thanks - and I think that list is taken from the biki which has basically the same info, based on a German keyboard. :)

I'm working on using another key.

Thanks though!  :good:
Sic semper tyrannosauro.