Forum

> > CS2D > Scripts > 4 directions sort
Forums overviewCS2D overview Scripts overviewLog in to reply

English 4 directions sort

4 replies
To the start Previous 1 Next To the start

old 4 directions sort

EnderCrypt
User Off Offline

Quote
can anyone make a function that puts x,y in a table (sorted, where closest is lowest index)
depending on dir

example if the direction is 15 and current x and y is 10
then
four_dir[1] = {x=10,y=9}
four_dir[2] = {x=11,y=10}
four_dir[3] = {x=9,y=10}
four_dir[4] = {x=10,y=11}

its hard to explain, but i hope you understand understand...

old Re: 4 directions sort

Flacko
User Off Offline

Quote
What do you mean with direction?

Anyways, I can try:
1
2
3
4
5
6
7
8
9
10
11
function sort_pos(x,y)
	local t = {}
	for k,v in pairs(player(0,"tableliving")) do
		table.insert(t,{player(v,"x"), player(v,"y")})
	end
	table.sort(t,
		function(a,b)
		return math.sqrt(((a[1]-x)^2+(a[2]-y)^2) < ((b[1]-x)^2+(b[2]-y)^2))
	)
	return t
end

old Re: 4 directions sort

EnderCrypt
User Off Offline

Quote
thx, gonna try it tomorow
Hm could...
You see the parameters should be
X,y,direction
Then it should give the table, from x,y in direction sorted, can you fix that? Thx
edited 1×, last 23.06.11 12:28:46 am

old Re: 4 directions sort

Flacko
User Off Offline

Quote
Sorry, it seems I can't understand a shit of what you're saying.

And you still haven't explained me what you mean by direction.

old Re: 4 directions sort

EnderCrypt
User Off Offline

Quote
Iys har to explain but im gonna try, even due im on ipod touch... Stiupit...

Ok so player is on a tile x y, theres a total of 8 tilrs around, now skip the tiles diagonnally , now we got 4 tiles

Lets say player moves in a direction, he would walk on 1 of these tiles(in most case)

The tile is easy to find, but lets say player moves in a direction, however the tile is already occupyed, wich of the oyher 3 tiles would then be closest to turn towards, third should be opposite to this, and fourth opposite to first tile

Get it?


I mean if the direction is 15, closest direction(/90)
Is up
Second closest would be right
Third closest would be left and fourt is down

ok listen, lets say im on tile 10x, and 15y
having 15 angle (rot)
first tile infron of me would be the 10x, 14y
second closest would be 11x 15 y
third closest would be 9x 15 y
and the fourth closest would be 10x 16 y

edit:
solved it myself

heres the code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
function four_dir (dir)
dir_fixed = round(dir/90)*90
x_add = {}
y_add = {}

x_add[1] = round(math.cos(math.rad((dir_fixed) - 90)))
y_add[1] = round(math.sin(math.rad((dir_fixed) - 90)))
if dir < dir_fixed then
dir_fixed = math.floor(dir/90)*90
else
dir_fixed = math.ceil(dir/90)*90
end
x_add[2] = round(math.cos(math.rad((dir_fixed) - 90)))
y_add[2] = round(math.sin(math.rad((dir_fixed) - 90)))

x_add[3] = -x_add[2]
y_add[3] = -y_add[2]

x_add[4] = -x_add[1]
y_add[4] = -y_add[1]
end
edited 2×, last 23.06.11 04:13:58 pm
To the start Previous 1 Next To the start
Log in to reply Scripts overviewCS2D overviewForums overview