Build Marine! and Attack!
μ΄ μ₯μμ λ°°μΈκ²
μμμ΄ λλ©΄, supply depotμ μ§κ³ , Barrackμ μ§μ λ€, 곡격κ°λ κ²μ λͺ©νλ‘ νκ² μ΅λλ€.
Let's Start!
μ¬μ€, μνλΌμ΄ λν νλ μ§κ²νλλ°λ μκ·Όν μκ°ν΄λ³Ό κ²λ€μ΄ λ§μ΅λλ€.
μνλΌμ΄λνμ μ§λλ€.
μΈκ΅¬μ 체ν¬!
μ§κΈ μνλΌμ΄λνμ μ§κ³ μλμ§ νμΈ!
SCVκ° λ Έλμ§ μ²΄ν¬!
μμ νμΈ!(μ§κΈ κ°λ₯ννλμΈμ§νμΈ!)
SCVλ₯Ό μ νν΄ μ λΉν μμΉμ μ§κΈ° μμν΄μΌν©λλ€.
μ΄λ κ² rule-basedκ° μ΄λ ΅μ΅λλ€ γ γ . νκΊΌλ²μ μ μ©ν μ€ν¬λ¦½νΈλ₯Ό λ³΄κ³ λΉ λ₯΄κ² λμ΄κ°λλ‘νμ£ !
class Agent(base_agent.BaseAgent):
def unit_type_is_selected(self,obs,unit_type):
if (len(obs.observation.single_select) > 0 and obs.observation.single_select[0].unit_type == unit_type):
return True
elif (len(obs.observation.multi_select) > 0 and obs.observation.multi_select[0].unit_type == unit_type):
return True
else :
return False
def can_do(self,obs,action_id):
return action_id in obs.observation.available_actions
def selected_units_idle_check(self,obs):
selected_units = [x for x in obs.observation.feature_units if x.is_selected == 1]
if len(selected_units) == 0 :
print("not selected!")
return True
else:
selected_unit = [x.order_length for x in selected_units]
if sum(selected_unit) > 0:
return False
return True
def food_check(self,obs):
food_enough = obs.observation.player.food_cap - obs.observation.player.food_used
if food_enough > 4:
return False
else:
return True
def build_building_now(self,obs,building_id):
units = [unit.build_progress for unit in obs.observation.feature_units if \
(unit.unit_type == building_id)]
if len(units) > 0 :
return True
else:
return False
def step(self,obs):
super(Agent,self).step(obs)
if obs.first():
player_y, player_x = (obs.observation.feature_minimap.player_relative == PLAYER_SELF).nonzero()
xmean = player_x.mean()
ymean = player_y.mean()
if xmean <= MINIMAP_SIZE/2 and ymean <= MINIMAP_SIZE/2:
self.scout_coordinates = (40, 40)
else:
self.scout_coordinates = (20, 20)
scvs = [unit for unit in obs.observation.feature_units if unit.unit_type == units.Terran.SCV]
if len(scvs) > 0 and not self.unit_type_is_selected(obs,units.Terran.SCV):
#μ λ μ
λ
scv = scvs[0]
return actions.FUNCTIONS.select_point("select",(scv.x,scv.y))
elif self.can_do(obs,BUILD_SUPPLYDEPOT) and \
self.food_check(obs) and \
(not self.build_building_now(obs,units.Terran.SupplyDepot))\
and [x.order_id_0 for x in obs.observation.feature_units if x.is_selected == 1][0] == \
HARVEST_GATHER_SCV_UNIT:
x,y = random.randint(0,SCREEN_SIZE),random.randint(0,SCREEN_SIZE)
return actions.FunctionCall(BUILD_SUPPLYDEPOT,[NOT_QUEUED,[x,y]])
elif self.can_do(obs,BUILD_BARRACKS) and \
(not self.build_building_now(obs,units.Terran.Barracks)) and \
(len([x for x in obs.observation.feature_units if x.unit_type == units.Terran.Barracks]) == 0) and \
self.selected_units_idle_check(obs):
x,y = random.randint(0,SCREEN_SIZE),random.randint(0,SCREEN_SIZE)
return actions.FunctionCall(BUILD_BARRACKS,[NOT_QUEUED,[x,y]])
return actions.FUNCTIONS.no_op()
λ§μ½ μνλΌμ΄λνμ μ§λ€κ° νλ©΄λ°μΌλ‘ λ²μ΄λλ©΄ μ΄λ¬ν rule-based Agentλ μ§κ³ μλ μνλΌμ΄ λνμ λν μ 보λ μ»μ μκ° μμ΄ μΈκ΅¬μκ° λ§νμ§ μμλλΌλ μ μνλΌμ΄λνμ μ§μ κ°λ₯μ±μ΄ μμ΅λλ€!(μμμ΄ λ§λ€λ©΄ APMμ΄ 300μ΄λκΉ μ μ λ€λ₯Ένλ©΄μΌλ‘ νλμνμλ€κ° λͺμ΄λ§μ λͺ¨λ μΌκΎΌμ΄ μνλΌμ΄ λνμ μ§λ κ²μ λ³Ό μ μμ΅λλ€.)
μ΄λ²μ λ°°λμ μ§μ΄λ³΄κ² μ΅λλ€!
if (len([x for x in obs.observation.feature_units if x.unit_type == units.Terran.Barracks]) == 1)\
and self.can_do(obs,TRAIN_MARINE_QUICK):
return actions.FunctionCall(TRAIN_MARINE_QUICK,[NOT_QUEUED])
μ΄ μ€ν¬λ¦½νΈλ μ€ν¬λ¦°μ λ°°λμ΄ λ³΄μ΄μ§μμΌλ©΄ μμ₯μ°½ μ§μ΄μ§λ λ¬Έμ κ° μμ΅λλ€..γ γ
μ΄μ λ§λ¦°μ κ³μν΄μ λ½μλ³΄κ² μ΅λλ€.
def step(self,obs):
super(Agent,self).step(obs)
if obs.first():
player_y, player_x = (obs.observation.feature_minimap.player_relative == PLAYER_SELF).nonzero()
xmean = player_x.mean()
ymean = player_y.mean()
if xmean <= MINIMAP_SIZE/2 and ymean <= MINIMAP_SIZE/2:
self.scout_coordinates = (40, 40)
else:
self.scout_coordinates = (20, 20)
scvs = [unit for unit in obs.observation.feature_units if unit.unit_type == units.Terran.SCV]
brracks = [unit for unit in obs.observation.feature_units if unit.unit_type == units.Terran.Barracks]
if len(scvs) > 0 and len(brracks) == 0 and not self.unit_type_is_selected(obs,units.Terran.SCV):
#μ λ μ
λ
scv = scvs[0]
return actions.FUNCTIONS.select_point("select",(scv.x,scv.y))
elif self.can_do(obs,BUILD_SUPPLYDEPOT) and \
self.food_check(obs) and \
(not self.build_building_now(obs,units.Terran.SupplyDepot))\
and [x.order_id_0 for x in obs.observation.feature_units if x.is_selected == 1][0] == \
HARVEST_GATHER_SCV_UNIT:
x,y = random.randint(0,SCREEN_SIZE),random.randint(0,SCREEN_SIZE)
return actions.FunctionCall(BUILD_SUPPLYDEPOT,[NOT_QUEUED,[x,y]])
elif self.can_do(obs,BUILD_BARRACKS) and \
(not self.build_building_now(obs,units.Terran.Barracks)) and \
(len([x for x in obs.observation.feature_units if x.unit_type == units.Terran.Barracks]) == 0) and \
self.selected_units_idle_check(obs):
x,y = random.randint(0,SCREEN_SIZE),random.randint(0,SCREEN_SIZE)
return actions.FunctionCall(BUILD_BARRACKS,[NOT_QUEUED,[x,y]])
elif len(brracks) == 1 and not self.unit_type_is_selected(obs,units.Terran.Barracks):
brrack = brracks[0]
return actions.FUNCTIONS.select_point("select",(brrack.x,brrack.y))
elif self.unit_type_is_selected(obs,units.Terran.Barracks) and self.can_do(obs,TRAIN_MARINE_QUICK):
return actions.FunctionCall(TRAIN_MARINE_QUICK,[NOT_QUEUED])
return actions.FUNCTIONS.no_op()
μ²μμ SCVλ₯Όμ νν λ, λ°°λλ μ§μΌλ΄μΌνκΈ°λλ¬Έμ μ 체μ μ½λλ₯Ό λ£μ΄ λ°λμ μ μ°Ύλλ‘ νμμ΅λλ€. κ·Έλ¬λ©΄ μ΄λ κ²λλ©΄ κ³μ λ§λ¦°μ λ½μ μ μκ² λμμ΅λλ€. λ§μ§λ§μΌλ‘ μ μ κΈ°μ§λ‘ 곡격μ 보λ΄κ² μ΅λλ€.
from pysc2.env import sc2_env
from pysc2.agents import base_agent
from pysc2.lib import actions,units,features
import random
from absl import app
MAPNAME = 'Simple64'
APM = 300
APM = int(APM / 18.75)
UNLIMIT = 0
VISUALIZE = True
REALTIME = True
PLAYER_SELF =features.PlayerRelative.SELF
SCREEN_SIZE = 84
MINIMAP_SIZE = 64
MOVE_MINIMAP = 332
BUILD_SUPPLYDEPOT = 91
BUILD_BARRACKS = 42
TRAIN_MARINE_QUICK = 477
HARVEST_GATHER_SCV_UNIT = 359
TRAIN_MARINE_QUICK = 477
NOT_QUEUED = [0]
players = [sc2_env.Agent(sc2_env.Race.terran),\
sc2_env.Bot(sc2_env.Race.zerg,\
sc2_env.Difficulty.very_easy)]
interface = features.AgentInterfaceFormat(\
feature_dimensions = features.Dimensions(\
screen = SCREEN_SIZE, minimap = MINIMAP_SIZE), use_feature_units = True)
class Agent(base_agent.BaseAgent):
def unit_type_is_selected(self,obs,unit_type):
if (len(obs.observation.single_select) > 0 and obs.observation.single_select[0].unit_type == unit_type):
return True
elif (len(obs.observation.multi_select) > 0 and obs.observation.multi_select[0].unit_type == unit_type):
return True
else :
return False
def can_do(self,obs,action_id):
return action_id in obs.observation.available_actions
def selected_units_idle_check(self,obs):
selected_units = [x for x in obs.observation.feature_units if x.is_selected == 1]
if len(selected_units) == 0 :
print("not selected!")
return True
else:
selected_unit = [x.order_length for x in selected_units]
if sum(selected_unit) > 0:
return False
return True
def food_check(self,obs):
food_enough = obs.observation.player.food_cap - obs.observation.player.food_used
if food_enough > 4:
return False
else:
return True
def build_building_now(self,obs,building_id):
units = [unit.build_progress for unit in obs.observation.feature_units if \
(unit.unit_type == building_id)]
if len(units) > 0 :
return True
else:
return False
def step(self,obs):
super(Agent,self).step(obs)
if obs.first():
player_y, player_x = (obs.observation.feature_minimap.player_relative == PLAYER_SELF).nonzero()
xmean = player_x.mean()
ymean = player_y.mean()
if xmean <= MINIMAP_SIZE/2 and ymean <= MINIMAP_SIZE/2:
self.attack_coordinates = (40, 40)
else:
self.attack_coordinates = (20, 20)
scvs = [unit for unit in obs.observation.feature_units if unit.unit_type == units.Terran.SCV]
brracks = [unit for unit in obs.observation.feature_units if unit.unit_type == units.Terran.Barracks]
if len(scvs) > 0 and len(brracks) == 0 and not self.unit_type_is_selected(obs,units.Terran.SCV):
#μ λ μ
λ
scv = scvs[0]
return actions.FUNCTIONS.select_point("select",(scv.x,scv.y))
elif self.can_do(obs,BUILD_SUPPLYDEPOT) and \
self.food_check(obs) and \
(not self.build_building_now(obs,units.Terran.SupplyDepot))\
and [x.order_id_0 for x in obs.observation.feature_units if x.is_selected == 1][0] == \
HARVEST_GATHER_SCV_UNIT:
x,y = random.randint(0,SCREEN_SIZE),random.randint(0,SCREEN_SIZE)
return actions.FunctionCall(BUILD_SUPPLYDEPOT,[NOT_QUEUED,[x,y]])
elif self.can_do(obs,BUILD_BARRACKS) and \
(not self.build_building_now(obs,units.Terran.Barracks)) and \
(len([x for x in obs.observation.feature_units if x.unit_type == units.Terran.Barracks]) == 0) and \
self.selected_units_idle_check(obs):
x,y = random.randint(0,SCREEN_SIZE),random.randint(0,SCREEN_SIZE)
return actions.FunctionCall(BUILD_BARRACKS,[NOT_QUEUED,[x,y]])
elif len(brracks) == 1 and not self.unit_type_is_selected(obs,units.Terran.Barracks) and not self.unit_type_is_selected(obs,units.Terran.Marine):
brrack = brracks[0]
return actions.FUNCTIONS.select_point("select",(brrack.x,brrack.y))
elif self.unit_type_is_selected(obs,units.Terran.Barracks) and self.can_do(obs,TRAIN_MARINE_QUICK):
return actions.FunctionCall(TRAIN_MARINE_QUICK,[NOT_QUEUED])
elif len([x for x in obs.observation.feature_units if x.unit_type == units.Terran.Marine]) > 3\
and not self.unit_type_is_selected(obs,units.Terran.Marine):
marines = [unit for unit in obs.observation.feature_units if unit.unit_type == units.Terran.Marine]
return actions.FUNCTIONS.select_point("select_all_type",(marines[0].x,marines[0].y))
elif self.unit_type_is_selected(obs,units.Terran.Marine) and self.selected_units_idle_check(obs):
x,y = self.attack_coordinates
return actions.FunctionCall(MOVE_MINIMAP,[NOT_QUEUED,[x,y]])
return actions.FUNCTIONS.no_op()
def main(args):
agent = Agent()
try:
with sc2_env.SC2Env(map_name = MAPNAME, players = players,\
agent_interface_format = interface,\
step_mul = APM, game_steps_per_episode = UNLIMIT,\
visualize = VISUALIZE, realtime = REALTIME) as env:
agent.setup(env.observation_spec(), env.action_spec())
timestep = env.reset()
agent.reset()
while True:
step_actions = [agent.step(timestep[0])]
if timestep[0].last():
break
timestep = env.step(step_actions)
except KeyboardInterrupt:
pass
app.run(main)
λ§λ¦° λ€κΈ°κ° λλ² λλ² κ±Έμ΄κ° μ μ 곡격νλ 보μ€μ μμ κ²μ λλ€!
Last updated
Was this helpful?