From edca1c742a374cb01bdd8fe8bc5896666de91b4e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Grzegorz=20Gapi=C5=84ski?= Date: Sat, 13 Mar 2021 17:38:07 +0100 Subject: [PATCH] =?UTF-8?q?Prze=C5=9Blij=20pliki=20do=20''?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Plik "agent.py" zawierający klasy gracza, potworów i skrzyń wraz z procedurami interakcji pomiędzy nimi --- agent.py | 151 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 151 insertions(+) create mode 100644 agent.py diff --git a/agent.py b/agent.py new file mode 100644 index 0000000..4e1eeee --- /dev/null +++ b/agent.py @@ -0,0 +1,151 @@ +from mesa import Agent, Model +import random + +def dice(number): + return random.randint(1,number) + +class Chest(Agent): + def __init__(self, unique_id, model): + super().__init__(unique_id, model) + self.gold = 3*dice(6) + +class Creature(Agent): + def __init__(self, unique_id, model): + super().__init__(unique_id, model) + self.strength = 3 + self.agility = 3 + self.wisdom = 2 + self.maxHealth = 10 + self.health = 10 + self.gold = dice(6) + self.attack_method = 1 + + def meleeAttack(self,opponent): + attackValue = self.strength + dice(6) + defenseValue = max(opponent.strength,opponent.agility) + damage = attackValue - defenseValue + if damage > 0: + opponent.health = opponent.health - damage + + def rangeAttack(self,opponent): + attackValue = self.agility + dice(6) + defenseValue = opponent.agility + damage = attackValue - defenseValue + if damage > 0: + opponent.health = opponent.health - damage + + def magicAttack(self,opponent): + attackValue = self.wisdom + dice(6) + defenseValue = opponent.wisdom + damage = attackValue - defenseValue + if damage > 0: + opponent.health = opponent.health - damage + + def defaultAttack(self,opponent): + if self.attack_method == 1: + self.meleeAttack(opponent) + elif self.attack_method == 2: + self.rangeAttack(opponent) + else: + self.magicAttack(opponent) + +class Player(Agent): + def __init__(self, unique_id, model): + super().__init__(unique_id, model) + self.strength = 3 + self.agility = 3 + self.wisdom = 3 + self.maxHealth = 20 + self.health = 20 + self.gold = 0 + + def move(self): + possible_steps = self.model.grid.get_neighborhood( + self.pos, + moore=True, + include_center=False) + new_position = self.random.choice(possible_steps) + self.model.grid.move_agent(self, new_position) + + def meleeAttack(self,opponent): + attackValue = self.strength + dice(6) + defenseValue = max(opponent.strength,opponent.agility) + damage = attackValue - defenseValue + if damage > 0: + opponent.health = opponent.health - damage + + def rangeAttack(self,opponent): + attackValue = self.agility + dice(6) + defenseValue = opponent.agility + damage = attackValue - defenseValue + if damage > 0: + opponent.health = opponent.health - damage + + def magicAttack(self,opponent): + attackValue = self.wisdom + dice(6) + defenseValue = opponent.wisdom + damage = attackValue - defenseValue + if damage > 0: + opponent.health = opponent.health - damage + + def fightOrFlight(self, opponent): + combat = True + while combat: + choice = dice(4) + if choice == 1: + running_speed = self.agility + dice(6) + opponent_speed = opponent.agility + dice(6) + if running_speed > opponent_speed: + combat = False + self.step() + else: + opponent.defaultAttack(self) + if self.health <= 0: + combat = False + elif choice == 2: + self.meleeAttack(opponent) + if opponent.health > 0: + opponent.defaultAttack(self) + if self.health <= 0: + combat = False + else: + combat = False + self.gold = self.gold + opponent.gold + opponent.gold = 0 + opponent.model.grid.remove_agent(opponent) + elif choice == 3: + self.rangeAttack(opponent) + if opponent.health > 0: + opponent.defaultAttack(self) + if self.health <= 0: + combat = False + else: + combat = False + self.gold = self.gold + opponent.gold + opponent.gold = 0 + opponent.model.grid.remove_agent(opponent) + else: + self.magicAttackAttack(opponent) + if opponent.health > 0: + opponent.defaultAttack(self) + if self.health <= 0: + combat = False + else: + combat = False + self.gold = self.gold + opponent.gold + opponent.gold = 0 + opponent.model.grid.remove_agent(opponent) + + + def step(self): + if self.health > 0: + self.move() + cellmates = self.model.grid.get_cell_list_contents([self.pos]) + if len(cellmates) > 1: + if isinstance(cellmates[1],Chest): + self.gold = self.gold + cellmates[1].gold + cellmates[1].gold = 0 + else: + opponent = cellmates[1] + fightOrFlight(self,opponent) + \ No newline at end of file