Skip to content

Commit

Permalink
Merge pull request #121 from kywch/cython
Browse files Browse the repository at this point in the history
Minigame PR
  • Loading branch information
kywch authored Mar 26, 2024
2 parents ee671c9 + 2f1fc7b commit 77c55fe
Show file tree
Hide file tree
Showing 99 changed files with 4,332 additions and 2,510 deletions.
5 changes: 4 additions & 1 deletion .github/workflows/pylint-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
# Randomly hitting TypeError: object int can't be used in 'await' expression in 3.11
# So, excluding 3.11 for now
python-version: ["3.8", "3.9", "3.10"]
steps:
- uses: actions/checkout@v3
Expand All @@ -16,8 +18,9 @@ jobs:
python-version: ${{ matrix.python-version }}
- name: Install dependencies
run: |
python -m pip install --upgrade pip setuptools wheel
python -m pip install --upgrade pip setuptools wheel cython
pip install .
python setup.py build_ext --inplace
- name: Running unit tests
run: pytest
- name: Analysing the code with pylint
Expand Down
6 changes: 4 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@ __pycache__/
*.py[cod]
*$py.class

# C extensions
# C extensions, cython
*.so
*.c

# Distribution / packaging
.Python
Expand Down Expand Up @@ -126,13 +127,14 @@ celerybeat.pid

# Environments
.env
.venv
.*venv
env/
venv/
ENV/
env.bak/
venv.bak/


# Spyder project settings
.spyderproject
.spyproject
Expand Down
49 changes: 23 additions & 26 deletions nmmo/core/action.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
# CHECK ME: Should these be fixed as well?
# pylint: disable=no-method-argument,unused-argument,no-self-argument,no-member

from enum import Enum, auto
import numpy as np
from nmmo.core.observation import Observation

from nmmo.lib import utils
from nmmo.lib.utils import staticproperty
from nmmo.systems.item import Stack
from nmmo.lib.log import EventCode
from nmmo.lib.event_code import EventCode
from nmmo.core.observation import Observation


class NodeType(Enum):
Expand Down Expand Up @@ -48,7 +46,7 @@ def leaf():
def N(cls, config):
return len(cls.edges)

def deserialize(realm, entity, index, obs: Observation):
def deserialize(realm, entity, index: int, obs: Observation):
return index

class Fixed:
Expand Down Expand Up @@ -76,7 +74,7 @@ def hook(config):
action.init(config)
for args in action.edges: # pylint: disable=not-an-iterable
args.init(config)
if not 'edges' in args.__dict__:
if not "edges" in args.__dict__:
continue
for arg in args.edges:
arguments.append(arg)
Expand All @@ -92,7 +90,7 @@ def n():
# pylint: disable=invalid-overridden-method
@classmethod
def edges(cls, config):
'''List of valid actions'''
"""List of valid actions"""
edges = [Move]
if config.COMBAT_SYSTEM_ENABLED:
edges.append(Attack)
Expand Down Expand Up @@ -124,12 +122,15 @@ def call(realm, entity, direction):
realm.map.tiles[r_new, c_new].impassible:
return

if entity.status.freeze > 0:
# ALLOW_MOVE_INTO_OCCUPIED_TILE only applies to players, NOT npcs
if entity.is_player and not realm.config.ALLOW_MOVE_INTO_OCCUPIED_TILE and \
realm.map.tiles[r_new, c_new].occupied:
return

entity.row.update(r_new)
entity.col.update(c_new)
if entity.status.freeze > 0:
return

entity.set_pos(r_new, c_new)
realm.map.tiles[r, c].remove_entity(ent_id)
realm.map.tiles[r_new, c_new].add_entity(entity)

Expand Down Expand Up @@ -165,7 +166,7 @@ class Direction(Node):
def edges():
return [North, South, East, West, Stay]

def deserialize(realm, entity, index, obs: Observation):
def deserialize(realm, entity, index: int, obs):
return deserialize_fixed_arg(Direction, index)

# a quick helper function
Expand Down Expand Up @@ -243,8 +244,8 @@ def call(realm, entity, style, target):
target.history.time_alive < immunity:
return None

#Check if self targeted
if entity.ent_id == target.ent_id:
#Check if self targeted or target already dead
if entity.ent_id == target.ent_id or not target.alive:
return None

#Can't attack out of range
Expand All @@ -253,17 +254,14 @@ def call(realm, entity, style, target):

#Execute attack
entity.history.attack = {}
entity.history.attack['target'] = target.ent_id
entity.history.attack['style'] = style.__name__
entity.history.attack["target"] = target.ent_id
entity.history.attack["style"] = style.__name__
target.attacker = entity
target.attacker_id.update(entity.ent_id)

from nmmo.systems import combat
dmg = combat.attack(realm, entity, target, style.skill)

if style.freeze and dmg > 0:
target.status.freeze.update(config.COMBAT_FREEZE_TIME)

# record the combat tick for both entities
# players and npcs both have latest_combat_tick in EntityState
for ent in [entity, target]:
Expand All @@ -277,7 +275,7 @@ class Style(Node):
def edges():
return [Melee, Range, Mage]

def deserialize(realm, entity, index, obs: Observation):
def deserialize(realm, entity, index: int, obs):
return deserialize_fixed_arg(Style, index)

class Target(Node):
Expand Down Expand Up @@ -511,7 +509,6 @@ def N(cls, config):
def deserialize(realm, entity, index: int, obs: Observation):
if index >= len(obs.market.ids):
return None

return realm.items.get(obs.market.ids[index])

class Buy(Node):
Expand All @@ -532,7 +529,7 @@ def call(realm, entity, item):
assert entity.alive, "Dead entity cannot act"
assert entity.is_player, "Npcs cannot buy an item"
assert item.quantity.val > 0, "Item quantity cannot be 0" # indicates item leak
assert item.equipped.val == 0, 'Listed item must not be equipped'
assert item.equipped.val == 0, "Listed item must not be equipped"

if not realm.config.EXCHANGE_SYSTEM_ENABLED:
return
Expand Down Expand Up @@ -601,8 +598,8 @@ def call(realm, entity, item, price):
def init_discrete(values):
classes = []
for i in values:
name = f'Discrete_{i}'
cls = type(name, (object,), {'val': i})
name = f"Discrete_{i}"
cls = type(name, (object,), {"val": i})
classes.append(cls)

return classes
Expand All @@ -628,21 +625,21 @@ def index(cls, price):
def edges():
return Price.classes

def deserialize(realm, entity, index, obs: Observation):
def deserialize(realm, entity, index: int, obs):
return deserialize_fixed_arg(Price, index)

class Token(Node):
argType = Fixed

@classmethod
def init(cls, config):
Token.classes = init_discrete(range(config.COMMUNICATION_NUM_TOKENS))
Token.classes = init_discrete(range(1, config.COMMUNICATION_NUM_TOKENS+1))

@staticproperty
def edges():
return Token.classes

def deserialize(realm, entity, index, obs: Observation):
def deserialize(realm, entity, index: int, obs):
return deserialize_fixed_arg(Token, index)

class Comm(Node):
Expand Down
Loading

0 comments on commit 77c55fe

Please sign in to comment.