This repository has been archived by the owner on Nov 24, 2020. It is now read-only.
/
itemloader.py
65 lines (48 loc) · 1.62 KB
/
itemloader.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import os
import random
import logging
from importlib.machinery import SourceFileLoader
logger = logging.getLogger('rg')
def load_item(name, buff):
path = 'items/{0}/{1}.py'.format(buff, name)
if not os.path.exists(path):
path += 'c'
if not os.path.exists(path):
return None
item_loader = SourceFileLoader(name, path)
item = item_loader.load_module()
return check_item(item, name, buff)
def check_item(item, name, buff):
item.code_name = name
item.buff = buff
required = [ 'name', 'description', 'price' ]
for r in required:
if not hasattr(item, r):
logger.warn('Item "{0}" has no attribute {1}!'.format(name, r))
return None
defaults = [
( lambda *args: None, [ 'on_room', 'on_enemy', 'on_escape', 'on_corridor', 'on_shop', 'on_pray', 'on_buy', 'on_dice', 'on_use' ] ), # callbacks
( lambda *args: 0, [ 'get_dice_bonus', 'get_damage_bonus', 'fight_use' ]),
( 0, [ 'damage', 'mana_damage', 'charisma', 'intelligence', 'defence' ] ), # buffs
( '', [ 'aura' ] ), # eeeh. meh
( [ ], [ 'tags', 'loot' ] ), # some arrays?
( False, [ 'usable', 'fightable', 'iscursed' ])
]
for def_val, names in defaults:
for name in names:
if not hasattr(item, name):
setattr(item, name, def_val)
return item
def load_random_item(buff):
pth = 'items/' + buff + '/'
items = [ f[:-3] for f in os.listdir(pth) if f.endswith('.py') ]
comp_items = [ f[:-4] for f in os.listdir(pth) if f.endswith('.pyc') ]
return (buff, random.choice(items + comp_items))
def load_shop_items():
items = [
load_random_item('bad'),
load_random_item('good'),
load_random_item('neutral')
]
random.shuffle(items)
return items