Skip to content

Commit

Permalink
refactor(#32): worker for lucIA v2
Browse files Browse the repository at this point in the history
  • Loading branch information
DelgadoPanadero committed Sep 5, 2021
1 parent 3282b50 commit 427528d
Show file tree
Hide file tree
Showing 15 changed files with 65 additions and 72 deletions.
2 changes: 1 addition & 1 deletion city/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
from .create_city import create_night_city
from .create_city import create_simple_city
from .create_city import create_day_city
2 changes: 1 addition & 1 deletion city/create_city.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from bh_trees import Inverter, Sequence, Selector


def create_simple_city():
def create_day_city():

graph = {
Sequence(): {
Expand Down
File renamed without changes.
File renamed without changes.
1 change: 0 additions & 1 deletion doc/city_v1.xml

This file was deleted.

1 change: 1 addition & 0 deletions doc/worker_day_v2.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<mxfile host="app.diagrams.net" modified="2021-09-05T08:58:08.600Z" agent="5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.159 Safari/537.36" etag="WLHtm65OUmekdA_6WHtM" version="15.0.6" type="device"><diagram id="W3p-pNmYOCLfn28GMmYI" name="Page-1">7Vtbc6IwFP41PrrDHXy0iF2nVFov2/XJQYnALhqHxqr76zdIEGmkS2dwAx36YMlJIuH7ziXnEFuivj7ch/bWe4QOCFoC5xxaYq8l4D+Ow/8iyTGWtEVZjiVu6DuxjE8FY/8PIEIy0d35DnjNDEQQBsjfZoVLuNmAJcrI7DCE++ywFQyyd93aLqAE46Ud0NIX30FeLNVkLpV/B77rJXfmkyde28lgInj1bAfuL0Si0RL1EEIUX60POggi9BJc4nn9nN7zwkKwQUUm7OaHo9lVg463mOsqUPqzh+e2mCwOHZMnBg4GgDRhiDzowo0dGKn0LoS7jQOir+VwKx1jQrjFQh4LfwGEjoRNe4cgFnloHZBecPDRz2j6N5m0Zhc9vQP55lPjmDQ2KDxeTIqas8u+dNqplcyLny96qFzciOgV7sIl+AAsgeifHboAfTSuc6YXGwaAa4AXhCeGILCR/5ZdiE0U1D2PSznEF4TGz1DKN5SWTqnIlFGhYbR8I2VKKVnlmx3syJ1erNGDMWoJSoBXfbcI8ZUbXY2N56kx1A1KBbIE7z0fgfHWPmGzx8E4S+bKDwIdBjA8zRVX2hIsl1j+ikL4G1z0LDRZklNK3kCIwOFjUmgMk/CvkehCgj8vqESwT0Mpn8RH7yKMKtyNcOc7jSkVNiWxoClJTC2p2cGUz6jMlNFmA1M+owrTDQwV7fqDYe9arDMNfWKNyo11q5VwPdY5ykKRlRvFOoljHusaz1jcjqSCdqSytCO+8YzlM8o0Weeb1K58RjWWjEo5sY67mw7MqzGvtvld+33Mk0XmMU9p7KmwPckF7YlnWirhtYbS8illmrPLeT5SH0xmX8pFUmkBexep5IE/MsbWdFQyzI4MNEe6BrMmLESlpOxL5ioHs0rBPBjP9e7o3pr3p6ZZQ5Db51eZCcqKxBplLU+Zh0YX6/MEX00GZh1VWqkc1p0rGn322Rhwo2f0agg0tYtljzRPF8zq7jzOBzoqhHJuqpY6j1i5a4e2KFcObVGmcGy28XlgCaQs9O9zCWwPJjTZ9i04Zfs+VaC8IqlccTXOw9TqJQhCYzyfMJ6i7zlj9WVmPGrD6Q04ZesQm/rjLThle8SETrC6+mRgDb/UmQRNehf02J9JEOic62lgmt37OpZoKHwrsKmgS+uP1g8jKoNZ+OPJGg9iNa8d2J3qVdIFuiKmdzG2HPYlNUSYru+y9xfi16uEKWL1ajO0V05yvpoWwFT1P4KMm+nPuE59F7+GE42/</diagram></mxfile>
1 change: 0 additions & 1 deletion doc/worker_night_v0.xml

This file was deleted.

1 change: 1 addition & 0 deletions doc/worker_night_v2.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<mxfile host="app.diagrams.net" modified="2021-09-05T10:09:56.939Z" agent="5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.159 Safari/537.36" etag="6zm5hZTCv8wJ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mxfile>
1 change: 0 additions & 1 deletion doc/worker_simple_v0.xml

This file was deleted.

1 change: 0 additions & 1 deletion doc/worker_v1.xml

This file was deleted.

8 changes: 4 additions & 4 deletions lucia.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from bh_trees import BlackBoard
from city import create_simple_city
from city import create_day_city
from city import create_night_city
from worker import create_simple_worker
from worker import create_day_worker
from worker import create_night_worker


Expand Down Expand Up @@ -77,8 +77,8 @@ def _choose_strategy(self):

# Choose day behavior
if self._turn%40<=30:
self._city_tree = create_simple_city()
self._worker_tree = create_simple_worker()
self._city_tree = create_day_city()
self._worker_tree = create_day_worker()

# Choose night behavior
if self._turn%40>30:
Expand Down
2 changes: 1 addition & 1 deletion worker/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
from .create_worker import create_night_worker
from .create_worker import create_simple_worker
from .create_worker import create_day_worker
65 changes: 28 additions & 37 deletions worker/create_worker.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,36 +12,30 @@
from bh_trees import Inverter, Sequence, Selector


def create_simple_worker():
def create_day_worker():

graph = {
Selector(): {
Inverter(): {
CanAct(): {}
},
Sequence(): {
IsCargoFull(): {},
Selector(): {
Selector(): {
BuildCityTile(): {},
Sequence(): {
IsCityNeeded():{},
FindNearestEmpty():{},
MoveToPosition():{}
}
},
Sequence(): {
FindNearestCity(): {},
MoveToPosition(): {}
}
}
Sequence(): {
CanAct(): {},
Selector(): {
Sequence(): {
IsCargoFull(): {},
IsCityNeeded(): {},
FindNearestEmpty(): {}
},
Sequence(): {
IsCargoFull(): {},
FindNearestCity(): {}
},
FindNearestResource(): {}
},
Selector(): {
Pillage(): {},
Sequence(): {
FindNearestResource(): {},
MoveToPosition(): {}
}
IsCargoFull(): {},
BuildCityTile(): {}
},
Pillage(): {},
MoveToPosition(): {}
}
}
}
Expand All @@ -52,21 +46,18 @@ def create_simple_worker():
def create_night_worker():

graph = {
Selector(): {
Inverter(): {
CanAct(): {}
Sequence(): {
CanAct(): {},
Selector(): {
Sequence(): {
IsCargoFull(): {},
FindNearestCity(): {},
},
FindNearestResource(): {}
},
Sequence(): {
IsCargoFull(): {},
FindNearestCity(): {},
MoveToPosition(): {}
},
Selector(): {
Pillage(): {},
Sequence(): {
FindNearestResource(): {},
MoveToPosition(): {}
}
MoveToPosition(): {}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,32 +26,36 @@ def run(self):
width = self._blackboard.get_value('width')
height = self._blackboard.get_value('height')

# close_city = self.find_closest_city(player, object)
close_city = self.find_closest_city(player, object)

random.shuffle(self.DIRECTIONS)
for dir in self.DIRECTIONS:
pos = object.pos.translate(dir,1)
if pos.x>=0 and pos.y>=0 and pos.x<width and pos.y<height:
cell = game_map.get_cell_by_pos(pos)
if cell.resource is None and cell.citytile is None:
if pos:
self._blackboard.set_values(position = pos)
return True
if close_city:
pos

return False


# def find_closest_city(self, player, object):
def find_closest_city(self, player, object):

closest_city_tile = None
if len(player.cities) > 0:
closest_dist = math.inf

# closest_city_tile = None
# if len(player.cities) > 0:
# closest_dist = math.inf
for k, city in player.cities.items():
for city_tile in city.citytiles:
dist = city_tile.pos.distance_to(object.pos)
if dist < closest_dist:
closest_dist = dist
closest_city_tile = city_tile

# for k, city in player.cities.items():
# for city_tile in city.citytiles:
# dist = city_tile.pos.distance_to(object.pos)
# if dist < closest_dist:
# closest_dist = dist
# closest_city_tile = city_tile
return coses_city_tile if dist<4 else None

# return closest_city_tile
def find_closest_empty(self):

random.shuffle(self.DIRECTIONS)
for dir in self.DIRECTIONS:
pos = object.pos.translate(dir,1)
if pos.x>=0 and pos.y>=0 and pos.x<width and pos.y<height:
cell = game_map.get_cell_by_pos(pos)
if cell.resource is None and cell.citytile is None:
if pos:
return pos
6 changes: 3 additions & 3 deletions worker/tasks/pillage.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ def _init_(self):

def run(self):
map = self._blackboard.get_value('map')
object = self._blackboard.get_value('object')
object = self._blackboard.get_value('object')

if self.is_in_resource(object, map):
if self.is_in_resource(object, map) and object.get_cargo_space_left()>0:
pillage = object.pillage()
self._blackboard.append_values(actions=pillage)
return True
Expand All @@ -20,4 +20,4 @@ def run(self):


def is_in_resource(self, object, map):
return map.get_cell_by_pos(object.pos).resource is not None
return map.get_cell_by_pos(object.pos).resource is not None

0 comments on commit 427528d

Please sign in to comment.