2021-03-30 14:17:54 +02:00
|
|
|
from mesa import Agent
|
2021-03-29 14:46:05 +02:00
|
|
|
import random
|
|
|
|
|
|
|
|
def dice(number):
|
|
|
|
return random.randint(1,number)
|
|
|
|
|
|
|
|
class Wall(Agent):
|
|
|
|
def __init__(self, unique_id, model):
|
|
|
|
super().__init__(unique_id, model)
|
|
|
|
|
2021-03-30 14:17:54 +02:00
|
|
|
def step(self):
|
|
|
|
pass
|
|
|
|
|
|
|
|
class Box(Agent):
|
|
|
|
def __init__(self, unique_id, model):
|
2021-03-29 14:46:05 +02:00
|
|
|
super().__init__(unique_id, model)
|
2021-03-30 14:17:54 +02:00
|
|
|
self.gold=3*dice(6)
|
|
|
|
self.isBox=True
|
|
|
|
self.isCreature=False
|
|
|
|
|
|
|
|
def step(self):
|
|
|
|
pass
|
2021-03-29 14:46:05 +02:00
|
|
|
|
|
|
|
class Weapon():
|
2021-03-30 14:17:54 +02:00
|
|
|
def __init__(self,name,type,damage):
|
2021-03-29 14:46:05 +02:00
|
|
|
self.name = name
|
2021-03-30 14:17:54 +02:00
|
|
|
self.type = type
|
2021-03-29 14:46:05 +02:00
|
|
|
self.damage = damage
|
|
|
|
|
|
|
|
class Armor():
|
|
|
|
def __init__(self,name,defence,mp):
|
|
|
|
self.name = name
|
|
|
|
self.defence = defence
|
|
|
|
self.mag_protection = mp
|
|
|
|
|
|
|
|
class Creature(Agent):
|
|
|
|
def __init__(self, unique_id, model, n, s, a, w, maxhp, hp, weap, arm, g):
|
|
|
|
super().__init__(unique_id, model)
|
|
|
|
self.name = n
|
|
|
|
self.strength = s
|
|
|
|
self.agility = a
|
|
|
|
self.wisdom = w
|
|
|
|
self.maxHealth = maxhp
|
|
|
|
self.health = hp
|
|
|
|
self.gold = g
|
|
|
|
self.weapon1 = weap
|
|
|
|
self.armor = arm
|
2021-03-30 14:17:54 +02:00
|
|
|
self.isBox = False
|
|
|
|
self.isCreature=True
|
|
|
|
|
2021-03-29 14:46:05 +02:00
|
|
|
|
|
|
|
def meleeAttack(self,opponent):
|
|
|
|
attackValue = self.strength + dice(6)
|
|
|
|
defenseValue = opponent.strength + opponent.armor.defence
|
|
|
|
damage = attackValue - defenseValue
|
|
|
|
if damage > 0:
|
|
|
|
opponent.health = opponent.health - (damage + self.weapon1.damage)
|
|
|
|
|
|
|
|
def rangeAttack(self,opponent):
|
|
|
|
attackValue = self.agility + dice(6)
|
|
|
|
defenseValue = opponent.agility
|
|
|
|
damage = attackValue - defenseValue
|
|
|
|
if (damage > 0) and (damage + self.weapon1.damage - opponent.armor.defence > 0):
|
|
|
|
opponent.health = opponent.health - (damage + self.weapon1.damage - opponent.armor.defence)
|
|
|
|
|
|
|
|
def magicAttack(self,opponent):
|
|
|
|
attackValue = self.wisdom + dice(6)
|
|
|
|
defenseValue = opponent.wisdom
|
|
|
|
damage = attackValue - defenseValue
|
|
|
|
if (damage > 0) and (damage + self.weapon1.damage - opponent.armor.mag_protection > 0):
|
2021-03-30 14:17:54 +02:00
|
|
|
opponent.health = opponent.health - (damage + self.weapon1.damage - opponent.armor.mag_protection)
|
2021-03-30 23:36:19 +02:00
|
|
|
|
2021-03-29 14:46:05 +02:00
|
|
|
def defaultAttack(self,opponent):
|
2021-03-30 14:17:54 +02:00
|
|
|
if self.weapon1.type == "Meele":
|
2021-03-29 14:46:05 +02:00
|
|
|
self.meleeAttack(opponent)
|
2021-03-30 14:17:54 +02:00
|
|
|
elif self.weapon1.type == "Range":
|
2021-03-29 14:46:05 +02:00
|
|
|
self.rangeAttack(opponent)
|
|
|
|
else:
|
|
|
|
self.magicAttack(opponent)
|
|
|
|
|
|
|
|
class Player(Creature):
|
|
|
|
def __init__(self, unique_id, model, n, s, a, w, maxhp, hp, weap, arm, g, w2, w3):
|
2021-03-30 14:17:54 +02:00
|
|
|
super().__init__(unique_id, model, n, s, a, w, maxhp, hp, weap, arm, g)
|
2021-03-29 14:46:05 +02:00
|
|
|
self.name = n
|
|
|
|
self.strength = s
|
|
|
|
self.agility = a
|
|
|
|
self.wisdom = w
|
|
|
|
self.maxHealth = maxhp
|
|
|
|
self.health = hp
|
|
|
|
self.gold = g
|
|
|
|
self.weapon1 = weap
|
|
|
|
self.weapon2 = w2
|
|
|
|
self.weapon3 = w3
|
|
|
|
self.armor = arm
|
2021-03-30 14:17:54 +02:00
|
|
|
self.isBox = False
|
|
|
|
self.isCreature = False
|
2021-03-29 14:46:05 +02:00
|
|
|
|
|
|
|
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 = opponent.strength + opponent.armor.defence
|
|
|
|
damage = attackValue - defenseValue
|
|
|
|
if damage > 0:
|
|
|
|
opponent.health = opponent.health - (damage + self.weapon1.damage)
|
|
|
|
|
|
|
|
def rangeAttack(self,opponent):
|
|
|
|
attackValue = self.agility + dice(6)
|
|
|
|
defenseValue = opponent.agility
|
|
|
|
damage = attackValue - defenseValue
|
|
|
|
if (damage > 0) and (damage + self.weapon2.damage - opponent.armor.defence > 0):
|
|
|
|
opponent.health = opponent.health - (damage + self.weapon2.damage - opponent.armor.defence)
|
|
|
|
|
|
|
|
def magicAttack(self,opponent):
|
|
|
|
attackValue = self.wisdom + dice(6)
|
|
|
|
defenseValue = opponent.wisdom
|
|
|
|
damage = attackValue - defenseValue
|
|
|
|
if (damage > 0) and (damage + self.weapon3.damage - opponent.armor.mag_protection > 0):
|
|
|
|
opponent.health = opponent.health - (damage + self.weapon3.damage - opponent.armor.mag_protection)
|
|
|
|
|
|
|
|
def fightOrFlight(self, opponent):
|
|
|
|
combat = True
|
|
|
|
while combat:
|
|
|
|
choice = dice(4)
|
2021-03-30 14:17:54 +02:00
|
|
|
print("dice rolled:", choice)
|
2021-03-29 14:46:05 +02:00
|
|
|
if choice == 1:
|
|
|
|
running_speed = self.agility + dice(6)
|
|
|
|
opponent_speed = opponent.agility + dice(6)
|
|
|
|
if running_speed > opponent_speed:
|
|
|
|
combat = False
|
2021-03-30 14:17:54 +02:00
|
|
|
print("Player ran away")
|
2021-03-29 14:46:05 +02:00
|
|
|
self.step()
|
|
|
|
else:
|
|
|
|
opponent.defaultAttack(self)
|
|
|
|
if self.health <= 0:
|
|
|
|
combat = False
|
2021-03-30 14:17:54 +02:00
|
|
|
print("Player died :/")
|
2021-03-29 14:46:05 +02:00
|
|
|
elif choice == 2:
|
|
|
|
self.meleeAttack(opponent)
|
|
|
|
if opponent.health > 0:
|
|
|
|
opponent.defaultAttack(self)
|
|
|
|
if self.health <= 0:
|
|
|
|
combat = False
|
2021-03-30 14:17:54 +02:00
|
|
|
print("Player died :/")
|
2021-03-29 14:46:05 +02:00
|
|
|
else:
|
|
|
|
combat = False
|
|
|
|
self.gold = self.gold + opponent.gold
|
|
|
|
opponent.gold = 0
|
|
|
|
opponent.model.grid.remove_agent(opponent)
|
2021-03-30 14:17:54 +02:00
|
|
|
print("Fight won")
|
2021-03-29 14:46:05 +02:00
|
|
|
elif choice == 3:
|
|
|
|
self.rangeAttack(opponent)
|
|
|
|
if opponent.health > 0:
|
|
|
|
opponent.defaultAttack(self)
|
|
|
|
if self.health <= 0:
|
|
|
|
combat = False
|
2021-03-30 14:17:54 +02:00
|
|
|
print("Player died :/")
|
2021-03-29 14:46:05 +02:00
|
|
|
else:
|
|
|
|
combat = False
|
|
|
|
self.gold = self.gold + opponent.gold
|
|
|
|
opponent.gold = 0
|
|
|
|
opponent.model.grid.remove_agent(opponent)
|
2021-03-30 14:17:54 +02:00
|
|
|
print("Fight won")
|
2021-03-29 14:46:05 +02:00
|
|
|
else:
|
2021-03-30 14:17:54 +02:00
|
|
|
self.magicAttack(opponent)
|
2021-03-29 14:46:05 +02:00
|
|
|
if opponent.health > 0:
|
|
|
|
opponent.defaultAttack(self)
|
|
|
|
if self.health <= 0:
|
|
|
|
combat = False
|
2021-03-30 14:17:54 +02:00
|
|
|
print("Player died :/")
|
2021-03-29 14:46:05 +02:00
|
|
|
else:
|
|
|
|
combat = False
|
|
|
|
self.gold = self.gold + opponent.gold
|
|
|
|
opponent.gold = 0
|
|
|
|
opponent.model.grid.remove_agent(opponent)
|
2021-03-30 14:17:54 +02:00
|
|
|
print("Fight won")
|
2021-03-29 14:46:05 +02:00
|
|
|
|
|
|
|
def openChest(self,chest):
|
|
|
|
self.gold = self.gold + chest.gold
|
2021-03-30 14:17:54 +02:00
|
|
|
print("Chest opened. Gold inside:", chest.gold)
|
2021-03-29 14:46:05 +02:00
|
|
|
chest.gold = 0
|
2021-03-30 14:17:54 +02:00
|
|
|
# if isinstance(chest.loot,Armor):
|
|
|
|
# buffer = self.armor
|
|
|
|
# self.armor = chest.loot
|
|
|
|
# chest.loot = buffer
|
|
|
|
# if isinstance(chest.loot,Weapon):
|
|
|
|
# if chest.loot.type == "Melee":
|
|
|
|
# buffer = self.weapon1
|
|
|
|
# self.weapon1 = chest.loot
|
|
|
|
# chest.loot = buffer
|
|
|
|
# elif chest.loot.type == "Range":
|
|
|
|
# buffer = self.weapon2
|
|
|
|
# self.weapon2 = chest.loot
|
|
|
|
# chest.loot = buffer
|
|
|
|
# elif chest.loot.type == "Magic":
|
|
|
|
# buffer = self.weapon3
|
|
|
|
# self.weapon3 = chest.loot
|
|
|
|
# chest.loot = buffer
|
2021-03-29 14:46:05 +02:00
|
|
|
|
|
|
|
def step(self):
|
|
|
|
if self.health > 0:
|
|
|
|
self.move()
|
|
|
|
cellmates = self.model.grid.get_cell_list_contents([self.pos])
|
|
|
|
if len(cellmates) > 1:
|
2021-03-30 14:17:54 +02:00
|
|
|
if isinstance(cellmates[0],Box):
|
|
|
|
self.openChest(cellmates[0])
|
2021-03-29 14:46:05 +02:00
|
|
|
else:
|
2021-03-30 14:17:54 +02:00
|
|
|
opponent = cellmates[0]
|
|
|
|
print("Fighting")
|
|
|
|
self.fightOrFlight(opponent)
|
|
|
|
print("HP: "+str(self.health)+" / "+str(self.maxHealth))
|
|
|
|
print("Gold: "+str(self.gold))
|
2021-03-29 14:46:05 +02:00
|
|
|
else:
|
2021-03-30 14:17:54 +02:00
|
|
|
print("HP: 0 / "+str(self.maxHealth))
|
2021-03-29 14:46:05 +02:00
|
|
|
|