agent walking correctly among boxes
This commit is contained in:
parent
ef8dd038a6
commit
020b1aa6a1
27
agent.py
27
agent.py
@ -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):
|
||||
|
||||
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):
|
||||
@ -17,6 +23,7 @@ class Creature(Agent):
|
||||
self.wisdom = 2
|
||||
self.maxHealth = 10
|
||||
self.health = 10
|
||||
|
||||
self.gold = dice(6)
|
||||
self.attack_method = 1
|
||||
|
||||
@ -27,6 +34,7 @@ class Creature(Agent):
|
||||
if damage > 0:
|
||||
opponent.health = opponent.health - damage
|
||||
|
||||
|
||||
def rangeAttack(self, opponent):
|
||||
attackValue = self.agility + dice(6)
|
||||
defenseValue = opponent.agility
|
||||
@ -34,6 +42,7 @@ class Creature(Agent):
|
||||
if damage > 0:
|
||||
opponent.health = opponent.health - damage
|
||||
|
||||
|
||||
def magicAttack(self, opponent):
|
||||
attackValue = self.wisdom + dice(6)
|
||||
defenseValue = opponent.wisdom
|
||||
@ -41,6 +50,7 @@ class Creature(Agent):
|
||||
if damage > 0:
|
||||
opponent.health = opponent.health - damage
|
||||
|
||||
|
||||
def defaultAttack(self, opponent):
|
||||
if self.attack_method == 1:
|
||||
self.meleeAttack(opponent)
|
||||
@ -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(
|
||||
@ -74,6 +87,7 @@ class Player(Agent):
|
||||
if damage > 0:
|
||||
opponent.health = opponent.health - damage
|
||||
|
||||
|
||||
def rangeAttack(self, opponent):
|
||||
attackValue = self.agility + dice(6)
|
||||
defenseValue = opponent.agility
|
||||
@ -81,6 +95,7 @@ class Player(Agent):
|
||||
if damage > 0:
|
||||
opponent.health = opponent.health - damage
|
||||
|
||||
|
||||
def magicAttack(self, opponent):
|
||||
attackValue = self.wisdom + dice(6)
|
||||
defenseValue = opponent.wisdom
|
||||
@ -88,6 +103,7 @@ class Player(Agent):
|
||||
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)
|
||||
|
||||
|
20
model.py
20
model.py
@ -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
|
Loading…
Reference in New Issue
Block a user