-
Notifications
You must be signed in to change notification settings - Fork 0
/
royale.py
147 lines (123 loc) · 4.78 KB
/
royale.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
import random
from world import World
VERBOSE = True
AGENT_CHARS = [ '!', '@', '$', '%', '^', '&', '(', ')' ]
AGENT_COMMANDS = [
'pass',
'north', 'south', 'east', 'west',
'take',
]
def main():
w = World()
w.randomize( 20, 12, 30 )
print( w )
player_obj = [
StumbleAgent(),
StumbleAgent(),
StumbleAgent(),
]
state_obj = []
for i in range( len( player_obj ) ):
( x, y ) = w.get_empty()
state_obj.append(
{
'health': 100, #
'stamina': 100, # actions take stamina
'initiative': 100, # action priority
'weapon_name': 'Unarmed', # optional? an agent might care
'weapon_damage_n': 1, # number of dice to roll
'weapon_damage_d': 4, # sides on each die (1d4 here)
'visibility': 3, # sight squares
'x': x, #
'y': y, #
'inventory': [], #
'c': random.choice( AGENT_CHARS ),
}
)
cycles_left = 3
done = False
while not done:
if VERBOSE:
print( '# Cycle {0} #'.format( cycles_left ) )
w.reset_characters()
for i in range( len( player_obj ) ):
w.add_character( state_obj[ i ][ 'x' ], state_obj[ i ][ 'y' ],
state_obj[ i ] )
# TODO obscure some of this state
print( state_obj[ i ] )
print( w )
action_obj = []
for i in range( len( player_obj ) ):
state = {
'agent': {
'health': state_obj[ i ][ 'health' ],
'weapon_name': state_obj[ i ][ 'weapon_name' ],
'weapon_damage_n': state_obj[ i ][ 'weapon_damage_n' ],
'weapon_damage_d': state_obj[ i ][ 'weapon_damage_d' ],
},
'view': w.get_local( state_obj[ i ][ 'x' ],
state_obj[ i ][ 'y' ],
state_obj[ i ][ 'visibility' ] ),
}
action = player_obj[ i ].update( state )
action_obj.append( ( i, action, state_obj[ i ][ 'initiative' ] ) )
if VERBOSE:
print( '\tplayer {0} ({1}): {2}'.format(
i, str( player_obj[ i ] ), action ) )
# first we need to process actions
# for example, attack another player based on their position
# once that's done, we make appropriate changes to the state
state_change_obj = []
for i in range( len( player_obj ) ):
state_change_obj.append( {} )
random.shuffle( action_obj ) # TODO consider initiative
for action in action_obj:
p = action[ 0 ]
a = action[ 1 ].split(' ')
if a[ 0 ] not in AGENT_COMMANDS:
print( 'WARNING: {0}'.format( action ) )
continue
if a[ 0 ] == 'pass':
pass
elif a[ 0 ] == 'north':
if w.is_empty( state_obj[ p ][ 'x' ],
state_obj[ p ][ 'y' ] - 1 ):
state_change_obj[ p ][ 'y' ] = state_obj[ p ][ 'y' ] - 1
elif a[ 0 ] == 'south':
if w.is_empty( state_obj[ p ][ 'x' ],
state_obj[ p ][ 'y' ] + 1 ):
state_change_obj[ p ][ 'y' ] = state_obj[ p ][ 'y' ] + 1
elif a[ 0 ] == 'east':
if w.is_empty( state_obj[ p ][ 'x' ] + 1,
state_obj[ p ][ 'y' ] ):
state_change_obj[ p ][ 'x' ] = state_obj[ p ][ 'x' ] + 1
elif a[ 0 ] == 'west':
if w.is_empty( state_obj[ p ][ 'x' ] - 1,
state_obj[ p ][ 'y' ] ):
state_change_obj[ p ][ 'x' ] = state_obj[ p ][ 'x' ] - 1
#print(action)
for i in range( len( player_obj ) ):
for k in state_change_obj[ i ].keys():
state_obj[ i ][ k ] = state_change_obj[ i ][ k ]
cycles_left -= 1
if cycles_left <= 0:
done = True
class AbstractAgent():
def __str__( self ):
return 'AbstractAgent'
def update( self, state ):
return ( 'pass' )
class PassAgent( AbstractAgent ):
def __str__( self ):
return 'PassAgent'
def update( self, state ):
return ( 'pass' )
class StumbleAgent( AbstractAgent ):
def __str__( self ):
return 'StumbleAgent'
def update( self, state ):
print( state[ 'view' ] )
actions = [ 'north', 'south', 'east', 'west' ]
return random.choice( actions )
if __name__ == "__main__":
main()