GetDestinationsByUnitTestScript

From Wesnoth

This is a basic Python script for illustration of my question on the python API function wesnoth.get_destinations_by_unit(). Note that everything is hardcoded so output are as close as possible as the one given in the question discussion page.

See also: GetDestinationsByUnitQuestion

To use the script to generate the problem identification data, you should type something like this:

wesnoth --multiplayer --scenario=multiplayer3pTripleBlitz
     --controller1=ai
     --controller2=ai
     --side3=Rebels --controller3=ai --algorithm3=python_ai --parm3=python_script:ai_test.py

This will start wesnoth on the TripleBlitz scenario, and make player 3 use the python AI with the test script. Only two turns are implemented so that the ouput size stays under control. This python AI does:

 - turn 1:
   - recruit a Mage
   - move the leader to a village
 - turn 2:
   - move the leader to another village
   - print get_destinations_by_unit() output
   - move the Mage to the first village
   - print get_destinations_by_unit() output

import wesnoth

units = wesnoth.get_units()
destinations = wesnoth.get_destinations_by_unit()
gamestatus = wesnoth.get_gamestatus()

if gamestatus.turn == 1:
  for src in destinations.keys():
    if units[src].can_recruit:
      for location in wesnoth.get_adjacent_tiles(src):
        if location.x == 10:
          if location.y == 17:
            wesnoth.recruit_unit("Mage",location)
            break
      for location in destinations[src]:
        if location.x == 12:
          if location.y == 19:
            dst = location
            dst_village = location
            break
      wesnoth.move_unit( src, dst )
elif gamestatus.turn == 2:
  for src in destinations.keys():
    if units[src].can_recruit:
      for location in destinations[src]:
        if location.x == 12:
          if location.y == 19:
            dst_village = location
        if location.x == 15:
          if location.y == 15:
            dst = location
      print "Unit %s(%s) at (%s,%s)"%(units[src].type().name,units[src].name,src.x,src.y)
      print "Is there unit a (%s,%s) ? %s"%(dst_village.x,dst_village.y,units.has_key(dst_village))
      new_src = wesnoth.move_unit( src, dst )
      units = wesnoth.get_units()
      destinations = wesnoth.get_destinations_by_unit()

      print "Unit %s(%s) at (%s,%s)"%(units[new_src].type().name,units[new_src].name,new_src.x,new_src.y)
      print "Is there unit a (%s,%s) ? %s"%(dst_village.x,dst_village.y,units.has_key(dst_village))
      if not src in destinations.keys():
        print "(%s,%s) not in destination list"%(src.x,src.y)
      else:
        print_list = []
        print "Location around (%s,%s)"%(src.x,src.y)
        for location in destinations[src]:
          if not location in print_list:
            print_list.append(location)
            print "Location (%s,%s)"%(location.x,location.y)
      break

  for src in destinations.keys():
    if not units[src].can_recruit:
      for location in destinations[src]:
        if location.x == 12:
          if location.y == 19:
            dst = location
            break
      print "Unit %s(%s) at (%s,%s)"%(units[src].type().name,units[src].name,src.x,src.y)
      print "Is there unit a (%s,%s) ? %s"%(dst_village.x,dst_village.y,units.has_key(dst_village))

      if not src in destinations.keys():
        print "(%s,%s) not in destination list"%(src.x,src.y)
      else:
        print_list = []
        print "Location around (%s,%s)"%(src.x,src.y)
        for location in destinations[src]:
          if not location in print_list:
            print_list.append(location)
            print "Location (%s,%s)"%(location.x,location.y)
      if not dst in destinations.keys():
        print "(%s,%s) not in destination list"%(dst.x,dst.y)
      else:
        print_list = []
        print "Location around (%s,%s)"%(dst.x,dst.y)
        for location in destinations[dst]:
          if not location in print_list:
            print_list.append(location)
            print "Location (%s,%s)"%(location.x,location.y)

      new_src = wesnoth.move_unit( src, dst )
      units = wesnoth.get_units()
      destinations = wesnoth.get_destinations_by_unit()
      print "Unit %s(%s) at (%s,%s)"%(units[new_src].type().name,units[new_src].name,new_src.x,new_src.y)
      print "Is there unit a (%s,%s) ? %s"%(dst_village.x,dst_village.y,units.has_key(dst_village))
      if not src in destinations.keys():
        print "(%s,%s) not in destination list"%(src.x,src.y)
      else:
        print_list = []
        print "Location around (%s,%s)"%(src.x,src.y)
        for location in destinations[src]:
          if not location in print_list:
            print_list.append(location)
            print "Location (%s,%s)"%(location.x,location.y)
      if not dst in destinations.keys():
        print "(%s,%s) not in destination list"%(dst.x,dst.y)
      else:
        print_list = []
        print "Location around (%s,%s)"%(dst.x,dst.y)
        for location in destinations[dst]:
          if not location in print_list:
            print_list.append(location)
            print "Location (%s,%s)"%(location.x,location.y)
      break

This page was last modified 14:37, 20 March 2008.