• Register

The near future: Trains are no longer controlled by humans. Instead, artificial intelligence is used to handle traffic. Program the best AI, beat challenges on various maps and watch it fight other AIs in live, online matches!

Forum Thread
  Posts  
beginner - tutorial 3 - first real challenge stuck :) (Games : TrAInsported : Forum : General AI questions : beginner - tutorial 3 - first real challenge stuck :)) Locked
Thread Options
Apr 17 2013 Anchor

hello, this is a beginners question:

I made this code. I dont know if it works or what does it do anyway, because I get some error:

function ai.foundPassengers(train,passengers)
	if train.ID == 1 then
		i = 1
		while i < #passengers do
			if passenger[1].destX > train.x then
				return passengers[1]
			elseif passenger[2].destX > train.x then
				return passengers[2]
			break
			end
		end
	elseif train.ID == 2 then
		i = 1
		while i < #passengers do
			if passenger[1].destX < train.x then
				return passengers[1]
			elseif passenger[2].destX < train.x then
				return passengers[2]
			break
			end
		end
	else
		return passengers[1]
	end
end

error:

error: ... : 28: 'end' expected (to close 'if' at line 26) near 'break'

did I really dont close any "if" statement..?

edit:
I tried to rewrite the code to make it simple. Still weird error about didnt closed "if", but it is closed, IMO:

function ai.foundPassengers(train,passengers)
	if train.ID == 1 then
		if passengers[1].destX<train.X then
			return passengers[1]
		elseif passengers[2].destX<train.X then
			return passengers[2]
		end
	elseif train.ID == 2 then
		if passengers[1].destX>train.X then
			return passengers[1]
		if passengers[2].destX>train.X then
			return passengers[2]
		end
	else
		return passengers [1]
	end
end

Edited by: funnyman7

Apr 18 2013 Anchor
<span>elseif train.ID == 2 then</span>
<span>    if passengers[1].destX>train.X then</span>
<span>      return passengers[1]</span>
<span>    if passengers[2].destX>train.X then</span>
<span>      return passengers[2]</span>
<span>    end</span>

Should be

<span>elseif train.ID == 2 then</span>
<span>    if passengers[1].destX>train.X then</span>
<span>      return passengers[1]</span>
<span>    elseif passengers[2].destX>train.X then</span>
<span>      return passengers[2]</span>
<span>    end</span>

Edited by: AHT0Hu0

Apr 21 2013 Anchor

I had some problems with where to place the break and return statement I eventually settled on

function ai.foundPassengers(train, passengers )
  if train.ID == 1 then
    i = 1
    while i < #passengers do
      if passengers[i].destX < train.x then
        break
      end
      i = i + 1
    end
    return passengers[i]elseif train.ID == 2 then
    i = 1
    while i > #passengers do
      if passengers[i].destX > train.x then
        break
      end
      i = i + 1
    end
    return passengers[i]else
    return passengers[1]
  end
end

Edited by: DevCroesus

Apr 24 2013 Anchor
function ai.foundPassengers(train, passengers )
  if train.ID == 1 then
    i = 1
    --loop through passengers
    while i < #passengers do
      if passengers[i].destX < train.x then
        --pick up passenger
        return passengers[i]--finish the loop
        break
      end
      i = i + 1
    end
    elseif train.ID == 2 then
    i = 1
    while i > #passengers do
      if passengers[i].destX > train.x then
        return passengers[i]break
      end
      i = i + 1
    end
    else
    return passengers[1]
  end
end

I wrote it as in the tutorial, I think, but it doesnt work... where do I have mistake now?

AHT0Hu0 wrote:

<span>elseif train.ID == 2 then</span>
<span>    if passengers[1].destX>train.X then</span>
<span>      return passengers[1]</span>
<span>    if passengers[2].destX>train.X then</span>
<span>      return passengers[2]</span>
<span>    end</span>

Should be

<span>elseif train.ID == 2 then</span>
<span>    if passengers[1].destX>train.X then</span>
<span>      return passengers[1]</span>
<span>    elseif passengers[2].destX>train.X then</span>
<span>      return passengers[2]</span>
<span>    end</span>

it doesnt work, ends with an error... :/

Apr 24 2013 Anchor

a return statemant ends your function. you do not need the break.
if you want to give more data back, you could try to save all your passengers in an array like this

function ai.foundPassengers(train, passengers )
  if train.ID == 1 then
    i = 1
    counter=0
    array={}
    --loop through passengers
    while i < #passengers do
      if passengers[i].destX < train.x then
        --pick up passenger
        counter=counter+1
        array[counter]=passengers[i]--finish the loop
        break
      end
      i = i + 1
    end
    return array
    elseif train.ID == 2 then
    i = 1
    counter=0
    array={}
    while i > #passengers do
      if passengers[i].destX > train.x then
        counter=counter+1
        array[counter]= passengers[i]break
      end
      i = i + 1
    end
    return array
    else
    return passengers[1]
  end
end

if call this function, you can access them with array[1],array[2],etc...

Apr 24 2013 Anchor

Sritra wrote: a return statemant ends your function. you do not need the break.
if you want to give more data back, you could try to save all your passengers in an array like this

function ai.foundPassengers(train, passengers )
  if train.ID == 1 then
    i = 1
    counter=0
    array={}
    --loop through passengers
    while i < #passengers do
      if passengers[i].destX < train.x then
        --pick up passenger
        counter=counter+1
        array[counter]=passengers[i]--finish the loop
        break
      end
      i = i + 1
    end
    return array
    elseif train.ID == 2 then
    i = 1
    counter=0
    array={}
    while i > #passengers do
      if passengers[i].destX > train.x then
        counter=counter+1
        array[counter]= passengers[i]break
      end
      i = i + 1
    end
    return array
    else
    return passengers[1]
  end
end

if call this function, you can access them with array[1],array[2],etc...

thank you, I'll look on it tommorow.

I have problems with challnge: smalltown3. I wrote my ai.mapEvent like this:

function ai.mapEvent()
	if train.ID == 1 then
		return "N"
	else
		return "E"
	end
end

to send train to right town.
but it keeps falling with error: attempt to index global 'train': a nil value.

Apr 24 2013 Anchor

your train is only a local variable, that is only in functions, where the game gives it to you
e.g.

function foundPassengers(train,passengers)--here the game gives you the train variable and the passengers table

luckily, the map event in this challenge gives you the train, you only need to write ai.mapEvent(train).

but there will nothing happen if you return something to the map event. the map event is only a one way trigger, telling you, that you trigger if you reache a special spot, that isn't even a junction.

the junction you mean, is at 8/9, the map event is triggered at 7/9.
i could write you a pseudo code for your funcs:

function ai.chooseDirection(train, passengers)
   if train ID==1 and train direction==south and next train position==7/9 then
      turn east
   elseif train ID==1 and train direction==east and next train position==7/9 then
      turn north
   elseif train ID==2 and train direction==west and next train position==7/9 then
      turn west
   elseif train ID==2 and train direction==east and next train position==7/9 then
      turn east
   else
      --put here your original chooseDirection
   end
end

train direction:=train.dir
next train position:=train.nextX/ and train.nextY

for the map event i can tell you: as far as i know (i never did this challenge) if your map event is triggered by train1, you need to look if the passenger he is carrying belongs to the northern or the southern park of the city. here you have to do some quite annoying comparisons. however, if the passenger wants to the southern part, you have to drop him here, if not, take him back to the northern part. if it's train to the other way round.

Edited by: Sritra

Apr 26 2013 Anchor

Sritra wrote: your train is only a local variable, that is only in functions, where the game gives it to you
e.g.

function foundPassengers(train,passengers)--here the game gives you the train variable and the passengers table

luckily, the map event in this challenge gives you the train, you only need to write ai.mapEvent(train).

but there will nothing happen if you return something to the map event. the map event is only a one way trigger, telling you, that you trigger if you reache a special spot, that isn't even a junction.

the junction you mean, is at 8/9, the map event is triggered at 7/9.
i could write you a pseudo code for your funcs:

function ai.chooseDirection(train, passengers)
   if train ID==1 and train direction==south and next train position==7/9 then
      turn east
   elseif train ID==1 and train direction==east and next train position==7/9 then
      turn north
   elseif train ID==2 and train direction==west and next train position==7/9 then
      turn west
   elseif train ID==2 and train direction==east and next train position==7/9 then
      turn east
   else
      --put here your original chooseDirection
   end
end

train direction:=train.dir
next train position:=train.nextX/ and train.nextY

for the map event i can tell you: as far as i know (i never did this challenge) if your map event is triggered by train1, you need to look if the passenger he is carrying belongs to the northern or the southern park of the city. here you have to do some quite annoying comparisons. however, if the passenger wants to the southern part, you have to drop him here, if not, take him back to the northern part. if it's train to the other way round.


it looks like the game doesnt allow this code constructions:

if train ID==1 and train direction==south and next train position==7/9 then

game reports an error:
"then expected near 'ID'"

Edited by: funnyman7

Apr 26 2013 Anchor

Sritra only wrote down Pseudocode, so this code won't run in any programming environment. It's not Lua code that he wrote down. He didn't want to give away all the code, so that others can still figure it out by themselves, he just wanted to help you get the idea.
For example, train ID, in trAInsported, would be written like:
train.ID
And "next train position" doesn't exist at all, you'll have to compute it yourself.
Try to read through the pseudocode and try to understand what he's doing - in plain english. Then go back to the game and try to write it in actual code - similar, but not exactly like Sritra is doing.
Once you've done that, show us your results if it's still not working... then we can try and fix it together.

Tutorial 3 is one of the more advanced ones. Once you've beaten that, you're pretty close to writing a very good AI already!

Reply to thread
click to sign in and post

Only registered members can share their thoughts. So come on! Join the community today (totally free - or sign in with your social account on the right) and join in the conversation.