169 lines
5.3 KiB
Python
169 lines
5.3 KiB
Python
from mesa import Agent
|
|
import random
|
|
|
|
|
|
def dice(number):
|
|
return random.randint(1, number)
|
|
|
|
|
|
class Box(Agent):
|
|
def __init__(self, unique_id, model):
|
|
super().__init__(unique_id, model)
|
|
self.gold=3*dice(6)
|
|
self.isBox=True
|
|
|
|
def step(self):
|
|
pass
|
|
|
|
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
|
|
self.isBox=False
|
|
|
|
|
|
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[0], Box):
|
|
self.gold = self.gold + cellmates[0].gold
|
|
cellmates[1].gold = 0
|
|
else:
|
|
opponent = cellmates[1]
|
|
self.fightOrFlight(opponent)
|
|
|
|
|