Prześlij pliki do ''

Plik "agent.py" zawierający klasy gracza, potworów i skrzyń wraz z procedurami interakcji pomiędzy nimi
This commit is contained in:
Grzegorz Gapiński 2021-03-13 17:38:07 +01:00
parent 1373ea2da7
commit edca1c742a

151
agent.py Normal file
View File

@ -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)