Skip to content

Commit

Permalink
started creating mobprob language using a simple parser, and utilizin…
Browse files Browse the repository at this point in the history
…g the `exec` function.
  • Loading branch information
duysqubix committed Dec 1, 2020
1 parent 2df880d commit 61c8a7d
Show file tree
Hide file tree
Showing 6 changed files with 118 additions and 0 deletions.
5 changes: 5 additions & 0 deletions server/conf/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,11 @@
'persistent': True,
'desc': 'storage for room data'
},
'trigdb': {
'typeclass': 'typeclasses.scripts.EntityDB',
'persistent': True,
'desc': 'storage for triggers'
},
'zonedb': {
'typeclass': 'typeclasses.scripts.EntityDB',
'persistent': True,
Expand Down
Empty file added world/edit/trigedit.py
Empty file.
6 changes: 6 additions & 0 deletions world/globals.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,12 @@
'reset_msg': "zone has reset"
}

DEFAULT_TRIG_STRUCT = {
'name': 'unfinished trigger',
'attachedto': '',
'prog': ''
}

DAM_TYPES = {
"physical": {
"hit", "sting", "whip", "slash", "bite", "bludgeon", "crush", "pound",
Expand Down
Empty file added world/mobprog/__init__.py
Empty file.
77 changes: 77 additions & 0 deletions world/mobprog/parser.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
from yapf.yapflib.yapf_api import FormatCode
from evennia import search_object


class MobProgParser:
"""
Parses a trigger script into valid python code that is then executed via `exec`
There are two objects interacting within scripts
1) the object that triggered this script and..
2) the object this script is attached too.
valid tokens:
$i the first of the names of the object itself.
$I the short description of the object itself.
$n the name of whomever caused the trigger to happen.
$N the name and title of whomever caused the trigger to happen.
$t the name of a secondary character target (i.e A smiles at B)
$T the short description, or name and title of target (NPC vs PC)
$r the name of a random char in the room with the mobile (never == $i)
$R the short description, or name and title of the random char
$j he,she,it based on sex of $i.
$e he,she,it based on sex of $n.
$E he,she,it based on sex of $t.
$J he,she,it based on sex of $r.
$k him,her,it based on sex of $i.
$m him,her,it based on sex of $n.
$M him,her,it based on sex of $t.
$K him,her,it based on sex of $r.
$l his,hers,its based on sex of $i.
$s his,hers,its based on sex of $n.
$S his,hers,its based on sex of $t.
$L his,hers,its based on sex of $r.
$o the first of the names of the primary object (i.e A drops B)
$O the short description of the primary object
$p the first of the names of the secondary object (i.e A puts B in C)
$P the short description of the secondary object
$a a,an based on first character of $o
$A a,an based on first character of $p
"""
def __init__(self, attached_obj, triggerer_obj):
"""
attached_name: the name of the object the script is attached too
triggerer_name: the name of the object that triggered the script
attached_obj can be either mob/room/object
triggerer_objcan be either pc or npc
"""

self.attached_obj = attached_obj
self.triggerer_obj = triggerer_obj

self.header = """
from evennia import search_object
from world.mobprog.utils import *
ch = search_object("{ch}", use_dbref=True, exact=True)[0]
self = search_object("{vict}", use_dbref=True, exact=True)[0]
""".format(ch=triggerer_obj.dbref, vict=attached_obj.dbref)

def parse(self, data):
"""
returns a valid python script
that if executed via `exec` it will perform what it needs to do
"""
data = self._token_replace(data)
return FormatCode(self.header + data)[0]

def _token_replace(self, data):
data = data.replace("$n", 'ch').replace("$i", "vict")
return data
30 changes: 30 additions & 0 deletions world/mobprog/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
"""
mobprog valid functions
"""
import random
from world.utils.utils import is_npc, is_pc


def ispc(obj):
return is_pc(obj)


def isnpc(obj):
return is_npc(obj)


def level(obj):
if obj.attributes.has('attrs'): # npc/pc
return obj.attrs.level.value
elif obj.attributes.has('level'): # object
return obj.db.level


def is_immort(obj):
if level(obj) > 200:
return True
return False


def rand(num):
return random.randint(0, 100) <= num

0 comments on commit 61c8a7d

Please sign in to comment.