rpg-szi/agent.py

169 lines
5.3 KiB
Python
Raw Normal View History

2021-03-16 23:02:16 +01:00
from mesa import Agent
import random
2021-03-16 23:02:16 +01:00
def dice(number):
2021-03-16 23:02:16 +01:00
return random.randint(1, number)
2021-03-16 23:02:16 +01:00
class Box(Agent):
def __init__(self, unique_id, model):
super().__init__(unique_id, model)
2021-03-16 23:02:16 +01:00
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)
2021-03-16 23:02:16 +01:00
self.strength = 3
self.agility = 3
self.wisdom = 2
self.maxHealth = 10
self.health = 10
self.gold = dice(6)
self.attack_method = 1
2021-03-16 23:02:16 +01:00
def meleeAttack(self, opponent):
attackValue = self.strength + dice(6)
2021-03-16 23:02:16 +01:00
defenseValue = max(opponent.strength, opponent.agility)
damage = attackValue - defenseValue
if damage > 0:
opponent.health = opponent.health - damage
2021-03-16 23:02:16 +01:00
def rangeAttack(self, opponent):
attackValue = self.agility + dice(6)
defenseValue = opponent.agility
damage = attackValue - defenseValue
if damage > 0:
opponent.health = opponent.health - damage
2021-03-16 23:02:16 +01:00
def magicAttack(self, opponent):
attackValue = self.wisdom + dice(6)
defenseValue = opponent.wisdom
damage = attackValue - defenseValue
if damage > 0:
opponent.health = opponent.health - damage
2021-03-16 23:02:16 +01:00
def defaultAttack(self, opponent):
if self.attack_method == 1:
self.meleeAttack(opponent)
elif self.attack_method == 2:
self.rangeAttack(opponent)
else:
self.magicAttack(opponent)
2021-03-16 23:02:16 +01:00
class Player(Agent):
def __init__(self, unique_id, model):
super().__init__(unique_id, model)
2021-03-16 23:02:16 +01:00
self.strength = 3
self.agility = 3
self.wisdom = 3
self.maxHealth = 20
self.health = 20
self.gold = 0
2021-03-16 23:02:16 +01:00
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)
2021-03-16 23:02:16 +01:00
def meleeAttack(self, opponent):
attackValue = self.strength + dice(6)
2021-03-16 23:02:16 +01:00
defenseValue = max(opponent.strength, opponent.agility)
damage = attackValue - defenseValue
if damage > 0:
opponent.health = opponent.health - damage
2021-03-16 23:02:16 +01:00
def rangeAttack(self, opponent):
attackValue = self.agility + dice(6)
defenseValue = opponent.agility
damage = attackValue - defenseValue
if damage > 0:
opponent.health = opponent.health - damage
2021-03-16 23:02:16 +01:00
def magicAttack(self, opponent):
attackValue = self.wisdom + dice(6)
defenseValue = opponent.wisdom
damage = attackValue - defenseValue
if damage > 0:
opponent.health = opponent.health - damage
2021-03-16 23:02:16 +01:00
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)
2021-03-16 23:02:16 +01: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-16 23:02:16 +01:00
if isinstance(cellmates[0], Box):
self.gold = self.gold + cellmates[0].gold
cellmates[1].gold = 0
else:
opponent = cellmates[1]
2021-03-16 23:02:16 +01:00
self.fightOrFlight(opponent)