agent walking correctly among boxes

This commit is contained in:
Joanna Radoła 2021-03-16 23:02:16 +01:00
parent ef8dd038a6
commit 020b1aa6a1
2 changed files with 59 additions and 60 deletions

View File

@ -1,13 +1,19 @@
from mesa import Agent, Model
from mesa import Agent
import random
def dice(number):
return random.randint(1,number)
class Chest(Agent):
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.gold=3*dice(6)
self.isBox=True
def step(self):
pass
class Creature(Agent):
def __init__(self, unique_id, model):
@ -17,31 +23,35 @@ class Creature(Agent):
self.wisdom = 2
self.maxHealth = 10
self.health = 10
self.gold = dice(6)
self.attack_method = 1
def meleeAttack(self,opponent):
def meleeAttack(self, opponent):
attackValue = self.strength + dice(6)
defenseValue = max(opponent.strength,opponent.agility)
defenseValue = max(opponent.strength, opponent.agility)
damage = attackValue - defenseValue
if damage > 0:
opponent.health = opponent.health - damage
def rangeAttack(self,opponent):
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):
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):
def defaultAttack(self, opponent):
if self.attack_method == 1:
self.meleeAttack(opponent)
elif self.attack_method == 2:
@ -49,6 +59,7 @@ class Creature(Agent):
else:
self.magicAttack(opponent)
class Player(Agent):
def __init__(self, unique_id, model):
super().__init__(unique_id, model)
@ -58,6 +69,8 @@ class Player(Agent):
self.maxHealth = 20
self.health = 20
self.gold = 0
self.isBox=False
def move(self):
possible_steps = self.model.grid.get_neighborhood(
@ -67,27 +80,30 @@ class Player(Agent):
new_position = self.random.choice(possible_steps)
self.model.grid.move_agent(self, new_position)
def meleeAttack(self,opponent):
def meleeAttack(self, opponent):
attackValue = self.strength + dice(6)
defenseValue = max(opponent.strength,opponent.agility)
defenseValue = max(opponent.strength, opponent.agility)
damage = attackValue - defenseValue
if damage > 0:
opponent.health = opponent.health - damage
def rangeAttack(self,opponent):
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):
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:
@ -142,10 +158,11 @@ class Player(Agent):
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
if isinstance(cellmates[0], Box):
self.gold = self.gold + cellmates[0].gold
cellmates[1].gold = 0
else:
opponent = cellmates[1]
fightOrFlight(self,opponent)
self.fightOrFlight(opponent)

View File

@ -1,5 +1,5 @@
from mesa import Model, Agent
from agent import Player
from agent import Player, Box
from mesa.time import RandomActivation
from mesa.space import MultiGrid
#from mesa.datacollection import DataCollector
@ -40,21 +40,3 @@ class GameMap(Model):
def step(self):
self.schedule.step()
# self.datacollector.collect(self) #na razie niepotrzebne
class Box(Agent):
def __init__(self, unique_id, model):
super().__init__(unique_id, model)
# self.x_coor = random.randrange(1, x + 1) # pola 1-10, zmienić na 0-9?
# self.y_coor = random.randrange(1, y + 1)
self.closed = True
self.content = 'unknown'
self.isBox=True
def open(self):
self.closed = False
self.content = random.choice(
possible_contents) # lepiej od razu przypisać content zamiast w ogóle dawać unknown?
def step(self):
pass