Home   Help Search Login Register  

Author Topic: loki's scripts and code library... WiP  (Read 3114 times)

0 Members and 1 Guest are viewing this topic.

Offline loki72

  • Former Staff
  • ****
    • Loki's Nightmare
loki's scripts and code library... WiP
« on: 27 Jun 2008, 00:34:37 »
greetings,

i hope this is ok here....

this is a continuous WiP

also, i would like to extend an invitation for any and all to add small scripts that you have found useful to help build this into a library of scripts... ya know.. for us noob toilet cleaners... :cool2:

ArmA - Scripting General
-------------------------------------------------------------------

(counting loops)
http://www.ofpec.com/forum/index.php?topic=31460.0

how does one count the times a loop has been fired off? and when it has looped 12x.. it moves on?

timeskip used as example
sqs format
Code: [Select]
; start the time acc

_x=0
#loop
~.04
_x = _x +1
skiptime .0625
? _x >= 195 : goto "next"
goto "loop"

#next
;script continues from here

2 sqf formats:

Code: [Select]
_x = 0;
while {_x < 12 } do
{
    //do whatever you think is necessary but don't forget the following line
    _x = _x + 1;
};

or

Code: [Select]
for [{_x= 0},{_x <12},{_x = _x + 1}] do
{
    //again do whatever you like...and nothing more:D
};

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

(keeping track of player)
http://www.ofpec.com/forum/index.php?topic=31602.0

this script is used to know if the player has entered or exited a vehicle. the typeOf vehicle is placed in a hint.

Code: [Select]
[] spawn
{
  private["_unit", "_veh"];


  while {true} do
  {

     while {(alive player)} do
     {
        _unit = player;
        if (vehicle _unit != _unit) then
        {
      _veh = vehicle player;
      hint  format ["greetings... you're in a %1.", typeOf _veh];
      waitUntil {vehicle player == player};
      hint  format ["greetings... you just dismounted from a %1.", typeOf _veh];               

        };
        Sleep 1;
     };

  };
};

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

    
(basics of teleport and onMapSingleClick)
http://www.ofpec.com/forum/index.php?topic=31544.0

if i am in one spot, and i want to hit 'M' and click on the map to teleport to another area.. how would i?

add an addaction;

Code: [Select]
player addAction ["gotoA","scripts\tele.sqf"];
the onMapSingleClick command goes inside the action script "tele.sqf" and a title text asking for map click.

Code: [Select]
// tele.sqf
titleText["Select Map Position", "PLAIN"];
onMapSingleClick "vehicle player setPos _pos; onMapSingleClick '';true;";

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -


(Selecting a random target)
http://www.ofpec.com/forum/index.php?topic=31500.0

if you want a simulated madman in a city killing randoms closest to him or something similar...

madman.sqs
Code: [Select]
? !(local server): exit

_shooter = _this select 0

_shooter addMagazine "8Rnd_9x18_Makarov"
_shooter addWeapon "Makarov"


#killingSpree

_targetList = nearestObjects [_shooter, ["man"], 150]
_target = _targetList select 1

_shooter doTarget _target
_shooter doFire _target
~1

? !(alive _target): _target = nil
~1
goto "killingSpree"

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

(basics of para-drop)
http://www.ofpec.com/forum/index.php?topic=31222.0

looking for a simple script for placing a parachute on vehicles

drop_car.sqf
Code: [Select]
// drop_car.sqf
// [position_desired]execVM"drop_car.sqf"
// Example:
// [[getPos player select 0,getPos player select 1, 100]] execVM "drop_car.sqf"

   _pos  = _this select 0;
   _myvehicle = "UAZ" createVehicle _pos;
   _myvehicle setPos _pos;

   // Change dist between chute and car at will
   _dist = -2;

   _mychute = "ParachuteWest" createVehicle getPos _myvehicle;
   _mychute setPos getPos _myvehicle;

   while {(getPos _myvehicle select 2) > 0} do
   {
      _myvehicle setPos (_mychute modelToWorld [0,0,_dist]);
      _myvehicle SetVectorUp (vectorUp _mychute);
      sleep 0.01;
   };

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -


(basic score script)
http://www.ofpec.com/forum/index.php?topic=29657.0

I'm trying to make my own ranking system and I want ArmA to automatically give anyone who is a West Pilot points so they can fly immediately.

init.sqf
Code: [Select]

  _unit = player;
   if (player isKindOf "SoldierWPilot") then
   {
      if (score _unit < rank1)  exitWith
      {
         player addscore 1000;
         hint "Pilot points awarded";
      };
   };

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

    
(add horizontal velocity... turbo)
http://www.ofpec.com/forum/index.php?topic=31627.0

Suppose I want to add turbo to a flying aircraft with the SetVelocity command...

add an addaction;

Code: [Select]
player addAction ["faster","scripts\turbo.sqf"];
turbo.sqf
Code: [Select]
// velocity is in m/s
_vel = 30;
_dir = 180;
while {true} do
{
   _vehicle setDir _dir;
   _vehicle setVelocity [sin(_dir)*_vel, cos(_dir)*_vel, 0];
   Sleep 0.005;
};

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

(instant calvary)

how to i create some guys and a couple of manned vehicles when needed?

init.sqf
Code: [Select]
vehicle player addAction ["calvary","scripts\calvary.sqs"];
calvary.sqs
Code: [Select]
if (!isServer) exitWith{};

veh1 = createVehicle ["M1Abrams", position player, [], 15, "NONE"];
~0.1
"SoldierWAT"createUnit [position veh1,group player,"this moveindriver veh1"];
"SoldierWAT"createUnit [position veh1,group player,"this moveingunner veh1"];



veh3 = createVehicle ["vulcan", position player, [], 15, "NONE"];
~0.1
"SoldierWAT"createUnit [position veh3,group player,"this moveindriver veh3"];
"SoldierWAT"createUnit [position veh3,group player,"this moveingunner veh3"];


"SoldierWSaboteurMarksman"createUnit [position player,group player];
"SoldierWMedic"createUnit [position player,group player];
"SoldierWMG"createUnit [position player,group player];
"SoldierWAT"createUnit [position player,group player];
"SoldierWAT"createUnit [position player,group player];
"SoldierWSaboteurMarksman"createUnit [position player,group player];
"SoldierWMedic"createUnit [position player,group player];
"SoldierWMG"createUnit [position player,group player];
"SoldierWAT"createUnit [position player,group player];
"SoldierWAT"createUnit [position player,group player];

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -






ArmA - Scripting Advanced
-------------------------------------------------------------------







ArmA - Scripting Multiplayer
-------------------------------------------------------------------

(setting time on a dedicated server for JiP)
http://www.ofpec.com/forum/index.php?topic=31490.0

how do i set the global time after the time has been altered by script for all JiP's?

init.sqf
Code: [Select]
// init.sqf
wantsinfo = objNull;
currentdate = 0;
currentovercast = -1;
currentfog = -1;

if (isServer) then
{
   currentovercast = random 0.5;
   0 setOverCast currentovercast;

   [] spawn
   {
      while {true} do
      {
         waitUntil {!isNull wantsinfo};
         wantsinfo = objNull;
         currentdate = daytime;         
         publicVariable "currentdate";
         currentovercast = overcast;
         publicVariable "currentovercast";
         currentfog = fog;         
         publicVariable "currentfog";
      };
   };
}
else
{
   waitUntil {alive player};
   wantsinfo = player;   
   publicVariable "wantsinfo";

   waitUntil {currentdate > 0};
   skipTime (currentdate - daytime);
   waitUntil {currentovercast != -1};
   0 setOverCast currentovercast;
   Sleep 1;
   waitUntil {currentfog != -1};
   0 setFog currentfog;
};

-------------------------------------------------------------------



« Last Edit: 27 Jun 2008, 01:01:29 by loki72 »

Offline Rommel92

  • Members
  • *
Re: loki's scripts and code library... WiP
« Reply #1 on: 27 Jun 2008, 03:52:32 »
Locality Check:
Code: [Select]
if (isServer) then
{
//SERVER

if (!(isNull player)) then
{
//SERVER-CLIENT
};
}
else
{
//CLIENT - JIP
waitUntil {!(isNull player)};
};

Group Eject
Code: [Select]
{unAssignVehicle _x; _x action ["EJECT", vehicle _x];} forEach units GroupVar;
Group Get%
Quote
Code: (GetUp) [Select]
{_x setUnitPos "UP"} forEach units GroupVar;
Code: (GetCrouch) [Select]
{_x setUnitPos "MIDDLE"} forEach units GroupVar;
Code: (GetDown) [Select]
{_x setUnitPos "DOWN"} forEach units GroupVar;

fPara.sqf (Basic MapClick Paradrop Script)
Code: [Select]
_para = _this select 1;
_shift = _this select 2;
_var = _this select 3;
_pos = _this select 4;

switch (_var) do
{
case 0:
{
titleCut ["Shift - Click your DropZone on the Map.","PLAIN DOWN",3];
onMapSingleClick "[[],Player,_shift,1,_pos] execVM 'common\fPara.sqf'; true;";
};
case 1:
{
if (_shift) then
{
onMapSingleClick "";
_chute = "ParachuteC" createVehicle _pos;
_chute setPos [_pos select 0, _pos select 1,(_pos select 2) + 160];
_para moveInDriver _chute;

WaitUntil {getPos _para select 2 < 4};
_para setVelocity [0,0,0];
};
};
};

fMake.sqf (Group Spawner. Returns Group Name.)
Code: [Select]
/*
fMake.sqf
daniel350_at_bigpond_dot_com

_group = [WEST,["SoldierWB","SoldierWG"], getPos Player, [], "FORM"] execVM "fMake.sqf"
*/

//////-----------------------------------------------------------------------------//////

private ["_side", "_pool", "_position", "_markers, "_special", "_group"];

//////-----------------------------------------------------------------------------//////

_side = _this select 0;
_pool = _this select 1;
_position = _this select 2;
_markers = _this select 3;
_special = _this select 4;

//////-----------------------------------------------------------------------------//////

_group = createGroup _side;

//////-----------------------------------------------------------------------------//////

{_group createUnit [_x, _position, _markers, 0, _special]} forEach _pool;

//////-----------------------------------------------------------------------------//////

_group

fTime_skip.sqf (Skips time w/o cloud skip (loss of weather ingame))
Code: [Select]
/*
 Ensure time is already sync'd with server if MP, and JIP.
 [player, Time skipped per / second]

 [player, 0.015] execVM fTime_skip.sqf"
*/
private ["_skip"];
_skip = _this select 1;

0 setOverCast 0; //Stops cloud jumping during the script.

while {!(isNull player)} do
{
skipTime _skip;
sleep 1;
};

Private (how It Works)
Quote
Code: [Select]
_fish = 10;
if (true) then
{
private ["_var"];
_var = 5;
player GlobalChat format ["%1", _var];
};
player GlobalChat format ["%1", _var];
PRINTS (inOrder): 5, 10

Code: [Select]
if (true) then
{
_var = 5;
player GlobalChat format ["%1", _var];
};
player GlobalChat format ["%1", _var];
PRINTS (inOrder): 5, <NULL>

Code: [Select]
private ["_var"];

if (true) then
{
_var = 5;
player GlobalChat format ["%1", _var];
};
player GlobalChat format ["%1", _var];
PRINTS (inOrder): 5, 5

fW_armaments.sqf (respawning + resupplying)
Code: [Select]
private ["_Veh", "_Type", "_Pos", "_Dir"]

_Veh = _this select 0;
_Type = typeOf _veh;
_Pos = getPos _veh;
_Dir = getDir _veh;

while {True} do
{
If (!Alive _veh) then
{
sleep 30;
_Veh = _Type createVehicle _Pos;
_Veh setPos _Pos;
_Veh setDir _Dir;
};

clearMagazineCargo _Veh;
clearWeaponCargo _Veh;

{_veh addMagazineCargo _x} forEach
[
["30Rnd_556x45_Stanag", 200],
["15Rnd_9x19_M9", 200],
["200Rnd_556x45_M249", 120],
["100Rnd_762x51_M240", 60],
["M136", 100],
["JAVELIN", 10],

["SmokeShell", 20],
["SmokeShellRed", 20],
["SmokeShellGreen", 20],
["HandGrenadeTimed", 20],
["PipeBomb", 20]
];
{_veh addWeaponCargo _x} forEach
[
["M16A2", 20],
["M16A2GL", 20],
["M16A4", 20],
["M16A4_ACG", 20],
["M16A4_ACG_GL", 20],
["M4", 20],
["M4GL", 20],
["M4AIM", 20],
["M4A1", 20],
["M4A1GL", 20],
["M249", 20],
["M240", 10],
["M24", 5],
["JAVELIN", 5],
["M136", 20],
["M9", 20],

["Binocular", 20],
["NVGoggles", 20]
];
sleep 300;
};
« Last Edit: 27 Jun 2008, 04:53:07 by Rommel92 »

Offline loki72

  • Former Staff
  • ****
    • Loki's Nightmare
Re: loki's scripts and code library... WiP
« Reply #2 on: 27 Jun 2008, 04:25:27 »
 :D
whoot! thx rommel

maybe when we get enough.. this can become a sticky thread.

« Last Edit: 27 Jun 2008, 04:28:47 by loki72 »

Offline Rommel92

  • Members
  • *
Re: loki's scripts and code library... WiP
« Reply #3 on: 27 Jun 2008, 05:02:38 »
Perhaps index your initial post (like below), then edit your second post and put your scripts in the same format as mine above, means we can get this looking a bit more formidable, and easier to browse. Perhaps a limit of 4 scripts per post (so you can index it via the posts and so users can find the wanted scripts easier). Might also want to grab permission from the mods first whether this is allowed or not?

Its a little bit less of a resources thread (which is what the Editors depot is for) but more dedicated to simple scripts which don't need example missions and are in much larger quantity and accessibility.


Scripts:
- Locality Check

- Group Eject
- Group [GetUp, GetCrouch, GetDown]

- fMake (Group Spawner. Returns Group Name.)
- fTime_skip (Skips time w/o cloud skip (loss of weather ingame))

- fW_armaments (respawning + resupplying weaponcrate WEST)

Examples:
- Private (how It Works)
« Last Edit: 27 Jun 2008, 05:05:34 by Rommel92 »

Offline Trexian

  • Members
  • *
Re: loki's scripts and code library... WiP
« Reply #4 on: 27 Jun 2008, 14:46:50 »
This is VERY helpful.  Kinda like a code snippet catalog. :D   :good:
Sic semper tyrannosauro.

Offline Trexian

  • Members
  • *
Re: loki's scripts and code library... WiP
« Reply #5 on: 20 Mar 2009, 01:21:53 »
Here's one I just spent several hours researching.  I'm mixing in some keywords like vector, bearing, heading, trigonometry, geometry, things like that.

If you have 2 positions, you can figure out the relative direction by using atan2 or lookat.  I've found lookAt to be not as helpful, especially when you don't want to place an AI thing and make it do stuff.

So, after much searching and a little testing, here's my understanding of atan2, using a picture instead of a thousand words.


It works this way, in pseudo code:
(target x - origin x) atan2 (target y - origin y) = direction from origin to target

That seems a bit backwards, but I think that's what my tests showed.

The two different numbers in the pic show the use of mod (represented sometimes with the % sign).

The code that gives the correct heading to the barrel from my position is (I already used createVehicle to drop the barrel in a random direction 20m from me):
Code: [Select]
_barrelPos = getPos _barrel;
_pos = getPos player;
_vectMod = ((((_barrelPos select 0) - (_pos select 0)) atan2 ((_barrelPos select 1) - (_pos select 1))) + 360) % 360;

In various posts here, I've seen people use or not use the mod part of the equation.  Just know that "west" of 360/180 will be a negative value showing how far "west" of 360/180 the heading is.
Sic semper tyrannosauro.

Offline Spooner

  • Members
  • *
  • Mostly useless
    • Community Base Addons
Re: loki's scripts and code library... WiP
« Reply #6 on: 21 Mar 2009, 14:20:39 »
Normal mathematical angles are measured from -180 to +180 degrees, which is what you get from atan2, but azimuth is measured from 0 to 360:
Code: [Select]
_azimuth = (_angle + 360) mod 360;
[Arma 2] CBA: Community Base Addons
[Arma 1] SPON Core (including links to my other scripts)

Offline Sparticus76

  • Members
  • *
Re: loki's scripts and code library... WiP
« Reply #7 on: 22 Mar 2009, 04:18:25 »
To get a unit to fire streight ahead without firing in the air

Code: [Select]
_firer action ["useWeapon", _firer, _firer, 0]

Because this one fires in the air for some reason

Code: [Select]
_firer dofire primaryweapon _firer