Page 1 of 2

Lighters pause in midpoints

Posted: Sun Oct 15, 2017 5:49 am
by HedgeHog builder
So when you left click; a beacon spawns, they are square and very small
When you right click; a spawner appears, spawners are circles

The lighters, which are the products of the spawners, will then go to the beacons.

However, if you place the beacons in a line like this;
The lighters will end up pausing in the middle.

Re: Lighters pause in midpoints

Posted: Mon Oct 16, 2017 1:08 pm
by Azhukar
I've changed the indentation to make your file more readable. It seems the code you posted does not behave as you describe, you probably posted a different version.

I recommend you read a guide on how to properly format your code to be readable and maintainable. Here's a start http://lua-users.org/wiki/LuaStyleGuide

Re: Lighters pause in midpoints

Posted: Thu Oct 19, 2017 7:01 am
by HedgeHog builder
It actually still does the pause in midpoints. I would like it to go from first beacon placed to second, then third.

Does anyone know how to do that?

Re: Lighters pause in midpoints

Posted: Thu Oct 19, 2017 11:44 am
by Azhukar
HedgeHog builder wrote:
Thu Oct 19, 2017 7:01 am
It actually still does the pause in midpoints. I would like it to go from first beacon placed to second, then third.

Does anyone know how to do that?
Sorry I missed the part where you needed to press the button "a" for the units to spawn. The reason your units stop in the middle of even number of spawners is because you're moving them once for each target, so when there's an equal amount of targets on each side their movements cancel out.

Here's a very crude example of how you could determine which target the units should go to next.

Re: Lighters pause in midpoints

Posted: Fri Oct 20, 2017 7:33 am
by HedgeHog builder

Code: Select all

		for i,v in ipairs(Opposing_Pens) do
			for k,Beacon in ipairs(Beacons) do
				if (v.target == nil) then
					v.target = k
				elseif (v.target ~= k) then
					break
				end
So line by line:

For each index and value of opposing pens do;
for each index and value of beacons do;
if the target of opposing pens is non existent; then target is set to index of a beacon
else if the target is not k, then finish the loop

Correct?

Re: Lighters pause in midpoints

Posted: Fri Oct 20, 2017 8:12 am
by Azhukar
HedgeHog builder wrote:
Fri Oct 20, 2017 7:33 am
So line by line:

For each index and value of opposing pens do;
for each index and value of beacons do;
if the target of opposing pens is non existent; then target is set to index of a beacon
else if the target is not k, then finish the loop

Correct?
Yes. 'break' always ends only one loop, the one it is called from. The outer loop keeps going.

The target is then removed once collision is detected and target is destroyed. If you want to be fancy you can determine the target by calculating the distance from the unit to all the targets and picking the one with the one with shortest distance.

Re: Lighters pause in midpoints

Posted: Sun Oct 22, 2017 3:07 am
by HedgeHog builder
So if I wanted to do comparison on distance between 2 points from the unit would I do

Code: Select all

unitdist 1 = unit.x - k.x
unit 2 = unit.x - k.x

if unitdist1 < unit2 then
target = unitdist1
else 
target = unit2

Re: Lighters pause in midpoints

Posted: Sun Oct 22, 2017 11:55 pm
by Azhukar
HedgeHog builder wrote:
Sun Oct 22, 2017 3:07 am
So if I wanted to do comparison on distance between 2 points from the unit would I do
Distance between 2 points would be

Code: Select all

local distance = math.sqrt((unit.x-k.x)^2 + (unit.y-k.y)^2)
Based on the https://en.wikipedia.org/wiki/Pythagorean_theorem

Re: Lighters pause in midpoints

Posted: Mon Oct 23, 2017 7:51 pm
by HedgeHog builder
How would I select the closest one?

if distance < k.x,k.y?

Re: Lighters pause in midpoints

Posted: Mon Oct 23, 2017 8:56 pm
by Azhukar
HedgeHog builder wrote:
Mon Oct 23, 2017 7:51 pm
How would I select the closest one?
Here is a tutorial for a minimum value algorithm: http://www.programming4beginners.com/tu ... -algorithm

You'll have to iterate through all potential targets and apply that to pick the one with the shortest distance.