SRL Archive - SRL Suggestions

A Couple Functions


NCDS

2010-09-06 16:03:57

First thing,


{*******************************************************************************
procedure DeleteValueInVarArray(var Arr: TVariantArray; ValuePosition: Integer);
By: EvilChicken!
Description: Deletes value with ValuePosition in "Arr" array.
*******************************************************************************}
procedure DeleteValueInVarArray(var Arr: TVariantArray; ValuePosition: Integer);
var
ArrLen, I: Integer;
begin
ArrLen := High(Arr);
for I := ValuePosition to ArrLen - 1 do
Swap(Arr[I], Arr[I + 1]);
SetArrayLength(Arr, ArrLen);
end;
The function is still EC!'s, all I did was change the data type. With this function however, the others may not really even be necessary? This function is tested and proven to work in the following function.

Second thing,


{*******************************************************************************
function RandomizeArray(Objs: TVariantArray): TVariantArray;
By: NCDS & Coh3n
Description: Randomly arranges array 'Objs' and results the new array
*******************************************************************************}
function RandomizeArray(Objs: TVariantArray): TVariantArray;
var
i, o: Integer;
begin
SetLength(Result, Length(Objs));

while (Length(Objs) <> 0) do
for i := 0 to High(Result) do
begin
o := Random(Length(Objs));

Result[i] := Objs[o];

DeleteValueInVarArray(Objs, o);
end;
end;
This is something me and Coh3n were working on for MSI and figured it might work nice in SRL as well.


It was tested with:

procedure DeleteValueInVarArray(var Arr: array of variant; ValuePosition: Integer);
var
ArrLen, I: Integer;
begin
ArrLen := High(Arr);
for I := ValuePosition to ArrLen - 1 do
Swap(Arr[I], Arr[I + 1]);
SetArrayLength(Arr, ArrLen);
end;

function RandomizeArray(Objs: TVariantArray): TVariantArray;
var
i, o: Integer;
begin
SetLength(Result, Length(Objs));

while (Length(Objs) <> 0) do
for i := 0 to High(Result) do
begin
o := Random(Length(Objs));
Writeln(Objs[o]);

Result[i] := Objs[o];

DeleteValueInVarArray(Objs, o);
Writeln('Length = ' + IntToStr(Length(Objs)));
end;

for i := 0 to High(Result) do
Writeln(Result[i]);
end;

var
arr: TVariantArray;

begin
ClearDebug;
arr := ['sh', 'ho', 'k', 'l', 'y'];
RandomizeArray(arr);
end.

Output:

'ho'
Length = 4
'y'
Length = 3
'l'
Length = 2
'k'
Length = 1
'sh'
Length = 0
'ho'
'y'
'l'
'k'
'sh'
Successfully executed.

Thoughts?

EvilChicken!

2010-09-06 16:41:30

procedure RandomizeArray(var Objs: TVariantArray);
var
I, Hi: Integer;
begin
Hi := High(Objs);
for I := 0 to Hi do
Swap(Objs[I], Objs[Random(Hi + 1)]);
end;

Not tested, but should work?

Oh, and I made four separate functions for deleting elements from arrays because I had some trouble with variants in SCAR. This was long ago, though – I can't even remember what the problem was. Maybe just a bug in SCAR for all I know.


Nice to see a new post in this subforum, by the way.

NCDS

2010-09-06 16:48:35

From: EvilChicken!
procedure RandomizeArray(var Objs: TVariantArray);
var
I, Hi: Integer;
begin
Hi := High(Objs);
for I := 0 to Hi do
Swap(Objs[I], Objs[Random(Hi + 1)]);
end;

Not tested, but should work?

Oh, and I made four separate functions for deleting elements from arrays because I had some trouble with variants in SCAR. This was long ago, though – I can't even remember what the problem was. Maybe just a bug in SCAR for all I know.


Nice to see a new post in this subforum, by the way.


Yeah I was wondering why you hadn't just used a variant to being with :p

And if yours works then go for it! :) Like I mentioned above, the one I posted was made to accommodate MSI's needs.

EvilChicken!

2010-09-06 17:05:45

I really have to get going to bed now; could you test it and post whether it works?
Would be really nice of you, as I won't be stopping by my house tomorrow for more than five minutes tomorrow, barely enough time for me to commit this, let alone do everything else I'm supposed to do.. *wink*

/lazydev || /busydev

NCDS

2010-09-06 17:26:26

From: EvilChicken!
I really have to get going to bed now; could you test it and post whether it works?
Would be really nice of you, as I won't be stopping by my house tomorrow for more than five minutes tomorrow, barely enough time for me to commit this, let alone do everything else I'm supposed to do.. *wink*

/lazydev || /busydev



procedure RandomizeArray(var Objs: TVariantArray);
var
I, Hi: Integer;
begin
Hi := High(Objs);
for I := 0 to Hi do
Swap(Objs[I], Objs[Random(Hi + 1)]);

for i := 0 to High(Objs) do
Writeln(Objs[i]);
end;

var
arr: TVariantArray;

begin
ClearDebug;
arr := ['sh', 'ho', 'k', 'l', 'y'];
RandomizeArray(arr);
end.

'ho'
'sh'
'y'
'k'
'l'
Successfully executed.
:)

Naum

2010-09-06 17:42:03

Err, is there any conventional use for randomized arrays in scripts? I haven't seen any individual script, so far, which has had the need to use a procedure like that.
I feel it just won't be used, there's already loads of stuff like that in SRL. Not trying to discredit anyone, but I honestly don't see a use for it in SRL, sorry.

Appreciation for putting stuff forward tho bro :)

NCDS

2010-09-06 17:46:16

From: Naum
Err, is there any conventional use for randomized arrays in scripts? I haven't seen any individual scripts so far which have had the need to use a procedure like that.
I feel it just won't be used, there's already loads of stuff like that in SRL. Not trying to discredit anyone, but I honestly don't see a use for it in SRL, sorry.

Appreciation for putting stuff forward tho bro :)


I wasn't too sure about it either to be honest.

We're using it to randomly choose which type of tree to chop/ore to mine/etc. in MSI and just figured it couldn't hurt to post it here as well.

TRiLeZ

2010-09-06 17:56:03

From: NCDS
I wasn't too sure about it either to be honest.

We're using it to randomly choose which type of tree to chop/ore to mine/etc. in MSI and just figured it couldn't hurt to post it here as well.


You could just pick a random object in the array to do that.

I agree with Naum. SRL doesn't need it, I've never seen a script need something like that, and it won't really be needed in the future.

Good try at improving SRL though. :)

NCDS

2010-09-06 17:59:19

From: TRiLeZ
You could just pick a random object in the array to do that.

I agree with Naum. SRL doesn't need it, I've never seen a script need something like that, and it won't really be needed in the future.

Good try at improving SRL though. :)


But then that requires more looping if it fails to find that tree/rock/etc. That's why we're just re-sorting the array.

EvilChicken!

2010-09-07 06:09:40

I've made a couple of these for people who've requested it, mainly over MSN.
It is potentially useful, and is used by several scripters for various purposes.

I will commit this whenever I get home, unless someone does it for me, .. or I forget it.

marpis

2010-09-07 11:29:47

The bad side of Variant is that

var
TIA: TIntegerArray;

procedure Something(Arr: TVariantArray);
begin
end;

begin
Something(TIA);
end.

will cause type mismatch :(

NCDS

2010-09-07 14:41:53

From: marpis
The bad side of Variant is that

var
TIA: TIntegerArray;

procedure Something(Arr: TVariantArray);
begin
end;

begin
Something(TIA);
end.

will cause type mismatch :(


based off MSI_VarToIntArray();

program new;

function MSI_IntToVarArray(Arr: TIntegerArray): TVariantArray;
var
i: Integer;
begin
SetLength(Result, Length(Arr));

for i := 0 to High(Arr) do
Result[i] := Arr[i];
end;

To show it work's:

program new;

function MSI_IntToVarArray(Arr: TIntegerArray): TVariantArray;
var
i: Integer;
begin
SetLength(Result, Length(Arr));

for i := 0 to High(Arr) do
Result[i] := Arr[i];

for i := 0 to High(Result) do
Writeln(IntToStr(Result[i]));
end;

var
Ar: TIntegerArray;
begin
Ar := [1, 2, 3, 4, 5];
MSI_IntToVarArray(Ar);
end.

Output:

Compiled succesfully in 15 ms.
1
2
3
4
5
Successfully executed.

Edit:
My bad, I thought I was replying to an MSI thread I guess for some reason :duh:

Wanted

2010-09-07 15:40:48

Variant will causes problems with booleans.

Heavenguard

2010-09-07 15:54:56

I actually could of really used this when I was making my essence miner. I had several angle integers [360, 224, 45, etc], and I needed a way to get an array that would randomize this for a loop. Unfortunately I had problems with InIntArray, and I could of really used the Randomize Array as shown above.

Heres a snippet of what I was doing a before

{Things look a bit weird due to the fact i changed / deleted a lot of code and comments, so please disregard any things that look error pronish, I dropped this script shortly after a couple functions from this.}
case (Random(4)) of
0: corners := [0, 1, 2, 3];
1: corners := [1, 3, 0, 2];
2: corners := [2, 0, 1, 3];
3: corners := [3, 2, 1, 0];
end;

{Sets up the Four Corners [ Srad, Erad, MinR, MaxR, MinLength, MaxLength, MinFusionCount, MaxFusionCount]}
FourCorners[corners[0]] := [1, 90, 1, 75]; // Top Right
FourCorners[corners[1]] := [270, 360, 1, 75]; // Top Left
FourCorners[corners[2]] := [180, 270, 1, 75]; // Bottom Left
FourCorners[corners[3]] := [90, 180, 1, 75]; // Bottom Right
for a := 0 to High(FourCorners) do

I could have used a nice randomize array to get random angles to twist and such. Im sorry I cant really explain what I was doing here as i dropped the project a good number of lines in. But I know that Randomize Array would serve a good purpose in SRL for someone at somepoint in time who doesn't have the right logic to write things [on there own] for that moment.

Wizzup?

2010-09-07 17:27:24

From: IceFire908
Variant will causes problems with booleans.


What problems? AFAIK they are just 0 or -1 when converted to integer.

Wanted

2010-09-07 17:36:39

From: Wizzup?
What problems? AFAIK they are just 0 or -1 when converted to integer.


No.

var
B: Boolean;
V: Variant;

begin
B := False;
WriteLn(Integer(B));
B := True;
WriteLn(Integer(B));
V := False;
WriteLn(Integer(V));
V := True;
WriteLn(Integer(V));
V := False;
B := V;
WriteLn(Integer(B));
V := True;
B := V;
WriteLn(Integer(B));
end.

Successfully compiled (32 ms)
0
1
0
-1
0
255
Successfully executed

NCDS

2010-09-07 17:44:20

From: IceFire908
No.

var
B: Boolean;
V: Variant;

begin
B := False;
WriteLn(Integer(B));
B := True;
WriteLn(Integer(B));
V := False;
WriteLn(Integer(V));
V := True;
WriteLn(Integer(V));
V := False;
B := V;
WriteLn(Integer(B));
V := True;
B := V;
WriteLn(Integer(B));
end.

Successfully compiled (32 ms)
0
1
0
-1
0
255
Successfully executed

Surely we could handle the exception of a variant that equals 255 or -1 and set it to 1?

Wanted

2010-09-07 18:11:29

The truth of this threads is held within several facts, the first of these facts being that something of this nature belongs internally within scar/simba's array functions, that or inside of a plugin, for speed efficiency. A lot of functions like this are not useful at all if they aren't optimized.

If we were to have this type of function in SRL then should have every single variation of the concept, for booleans, tpointarrays, t2dpointarrays, extended, variant, integer, strings, integer arrays, string arrays, t2d integer arrays. etc the list goes on quite a bit, then you have every variations of those types such as DeleteInArray, ReArrangeAndShortenArray, Randomize Array, Invert, Split. etc.. this allows a nearly unlimited amount of different combinations.

It is simply not practical for us to include every function that might have potential usage, but rather a necessity for it to belong there, which there is none.

Correcting the variant->boolean and reverse conversion would not be worth the effort nor end result.

NCDS

2010-09-07 18:19:18

From: IceFire908
The truth of this threads is held within several facts, the first of these facts being that something of this nature belongs internally within scar/simba's array functions, that or inside of a plugin, for speed efficiency. A lot of functions like this are not useful at all if they aren't optimized.

If we were to have this type of function in SRL then should have every single variation of the concept, for booleans, tpointarrays, t2dpointarrays, extended, variant, integer, strings, integer arrays, string arrays, t2d integer arrays. etc the list goes on quite a bit, then you have every variations of those types such as DeleteInArray, ReArrangeAndShortenArray, Randomize Array, Invert, Split. etc.. this allows a nearly unlimited amount of different combinations.

It is simply not practical for us to include every function that might have potential usage, but rather a necessity for it to belong there, which there is none.

Correcting the variant->boolean and reverse conversion would not be worth the effort nor end result.


Currently in SRL's Math.scar:

{*******************************************************************************
procedure DeleteValueInStrArray(var Arr: TStringArray; ValuePosition: Integer);
By: EvilChicken!
Description: Deletes value with ValuePosition in "Arr" string array.
*******************************************************************************}
procedure DeleteValueInStrArray(var Arr: TStringArray; ValuePosition: Integer);
var
ArrLen, I: Integer;
begin
ArrLen := High(Arr);
for I := ValuePosition to ArrLen - 1 do
Swap(Arr[I], Arr[I + 1]);
SetArrayLength(Arr, ArrLen);
end;

{*******************************************************************************
procedure DeleteValueInIntArray(var Arr: TIntegerArray; ValuePosition: Integer);
By: EvilChicken!
Description: Deletes value with ValuePosition in "Arr" Integer array.
*******************************************************************************}
procedure DeleteValueInIntArray(var Arr: TIntegerArray; ValuePosition: Integer);
var
ArrLen, I: Integer;
begin
ArrLen := High(Arr);
for I := ValuePosition to ArrLen - 1 do
Swap(Arr[I], Arr[I + 1]);
SetArrayLength(Arr, ArrLen);
end;

{*******************************************************************************
procedure DeleteValueInFloatArray(var Arr: TExtendedArray; ValuePosition: Integer);
By: EvilChicken!
Description: Deletes value with ValuePosition in "Arr" Float array.
*******************************************************************************}
procedure DeleteValueInFloatArray(var Arr: TExtendedArray; ValuePosition: Integer);
var
ArrLen, I: Integer;
begin
ArrLen := High(Arr);
for I := ValuePosition to ArrLen - 1 do
Swap(Arr[I], Arr[I + 1]);
SetArrayLength(Arr, ArrLen);
end;

{*******************************************************************************
procedure DeleteValueInBoolArray(var Arr: TBooleanArray; ValuePosition: Integer);
By: EvilChicken!
Description: Deletes value with ValuePosition in "Arr" Boolean array.
*******************************************************************************}
procedure DeleteValueInBoolArray(var Arr: array of boolean; ValuePosition: Integer);
var
ArrLen, I: Integer;
begin
ArrLen := High(Arr);
for I := ValuePosition to ArrLen - 1 do
Swap(Arr[I], Arr[I + 1]);
SetArrayLength(Arr, ArrLen);
end;
That's why I posted the Variant version was to hopefully replace these.

As for the RandomizeArray, that was just a thought. I was making it for MSI, Coh3n said it couldn't hurt to post it here, so I made it a accept a variant, tested it, then posted.

If it is decided to be useful, cool. Otherwise, I'm not too concerned about it.

I don't see anything wrong with the DeleteValue proc though.

Wanted

2010-09-07 18:25:15

AFAIK variant only covers Integer, Extended, String, and Boolean. You would never be able to delete all those existing functions and only use variant, unfortunately.

NCDS

2010-09-07 18:34:46

From: IceFire908
AFAIK variant only covers Integer, Extended, String, and Boolean. You would never be able to delete all those existing functions and only use variant, unfortunately.


Which would mean variant would cover all those procedures, no?

I apologize if I'm missing something.

Wanted

2010-09-07 18:39:33

From: NCDS
Which would mean variant would cover all those procedures, no?

I apologize if I'm missing something.


In order to use that function everyone would have to convert their array types to variant arrays.

NCDS

2010-09-07 18:49:52

From: IceFire908
In order to use that function everyone would have to convert their array types to variant arrays.


:duh: not sure why that didn't occur to me.

Well then, do with them what you choose, doesn't much matter to me.