-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathowwlib.py
More file actions
203 lines (150 loc) · 6.87 KB
/
Copy pathowwlib.py
File metadata and controls
203 lines (150 loc) · 6.87 KB
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
#Collection of Overwatch Workshop function calls
#This module contains a number of functions callable from within
#OverScript. Each function takes a variable amount of arguments,
#however, it is always passed at least one argument, which is
#the current compiler instance. Thus, each function defined here
#has access to the complete compiler state, variable mappings,
#loop and function stacks and already defined rules and actions.
#The function then may insert an arbitrary amount of actions
#into the current rule before returning.
#Each function should return a string specifying the value or
#action requested for the current context.
#TODO: Extend this to cover all workshop actions and values
#Copyright (c) 2019 fredi_68
#Permission is hereby granted, free of charge, to any person obtaining a copy
#of this software and associated documentation files (the "Software"), to deal
#in the Software without restriction, including without limitation the rights
#to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
#copies of the Software, and to permit persons to whom the Software is
#furnished to do so, subject to the following conditions:
#The above copyright notice and this permission notice shall be included in all
#copies or substantial portions of the Software.
#THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
#IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
#FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
#AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
#LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
#OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
#SOFTWARE.
#make sure to define builtins that we will override
#so we can still access them later
_range = __builtins__["range"]
_input = __builtins__["input"]
#======================
#ACTIONS
#======================
def wait(ctx, time, cond=None):
if cond is None:
cond = "Ignore Condition"
else:
cond = ctx._parseExpr(cond)
return "Wait(%s, %s)" % (ctx._parseExpr(time), cond)
def appendToArray(ctx, array, element):
return "Append To Array(%s, %s)" % (ctx._parseExpr(array), ctx._parseExpr(element))
def applyImpulse(ctx, player, direction, speed, relative, motion):
return "Apply Impulse(%s, %s, %s, %s, %s)" % (ctx._parseExpr(player), ctx._parseExpr(direction), ctx._parseExpr(speed), ctx._parseExpr(relative), ctx._parseExpr(motion))
def bigMessage(ctx, visibleTo, header):
return "Big Message(%s, %s)" % (ctx._parseExpr(visibleTo), ctx._parseExpr(header))
#======================
#VALUES
#======================
#-----------
#Arithmetic
#-----------
def abs(ctx, x):
return "Absolute Value(%s)" % ctx._parseExpr(x)
#-----------
#Datatypes
#-----------
def vector(ctx, x, y, z):
return "Vector(%s, %s, %s)" % (ctx._parseExpr(x), ctx._parseExpr(y), ctx._parseExpr(z))
def hero(ctx, h):
return "Hero(%s)" % ctx._parseExpr(h)
def backward(ctx):
return "Backward"
def team(ctx, team):
return "Team(%s)" % ctx._parseExpr(team)
def victim(ctx):
return "Victim"
def attacker(ctx):
return "Attacker"
#-----------
#Other
#-----------
def heroOf(ctx, player):
return "HeroOf(%s)" % ctx._parseExpr(player)
def isButtonHeld(ctx, player, button):
return "Is Button Held(%s, %s)" % (ctx._parseExpr(player), ctx._parseExpr(button))
def allDeadPlayers(ctx, team):
return "All Dead Players(%s)" % ctx._parseExpr(team)
def allHeroes(ctx):
return "All Heroes()"
def allLivingPlayers(ctx, team):
return "All Living Players(%s)" % ctx._parseExpr(team)
def allPlayers(ctx, team):
return "All Players(%s)" % ctx._parseExpr(team)
def allPlayersNotOnObjective(ctx, team):
return "All Players Not On Objective(%s)" % ctx._parseExpr(team)
def allPlayersOnObjective(ctx, team):
return "All Players On Objective(%s)" % ctx._parseExpr(team)
def allowedHeroes(ctx, player):
return "Allowed Heroes(%s)" % ctx._parseExpr(player)
def altitudeOf(ctx, player):
return "Altitude Of(%s)" % ctx._parseExpr(player)
def angleDifference(ctx, value1, value2):
return "Angle Difference(%s, %s)" % (ctx._parseExpr(value1), ctx._parseExpr(value2))
def arrayContains(ctx, array, value):
return "Array Contains(%s, %s)" % (ctx._parseExpr(array), ctx._parseExpr(value))
def arraySlice(ctx, array, start, count):
return "Array Slice(%s, %s, %s)" % (ctx._parseExpr(array), ctx._parseExpr(start), ctx._parseExpr(count))
def closestPlayerTo(ctx, center, team):
return "Closest Player To(%s, %s)" % (ctx_parseExpr(center, team))
def countOf(ctx, array):
return "Count Of(%s)" % ctx._parseExpr(array)
#======================
#BUILTIN PYTHON FUNCTIONS
#======================
#These functions are builtin Python functions,
#made available to workshop scripts for convenience.
#Unless stated otherwise, these functions do NOT convert
#to workshop instructions, but are merely shorthands for
#literal values or transformations on them.
#In most cases, passing non literal values as arguments
#will result in compiler errors.
def range(ctx, *args):
#Bit dodgy this, may want to revise
return ctx._create_1d_array(_range(*map(lambda x: int(ctx._parseExpr(x)), args)))
len = countOf
#======================
#OTHER
#======================
#I/O
def input(ctx, var, player=None):
var = ctx._parseExpr(var)
if var in ctx.used_vars:
raise RuntimeError("Use of external variable %s prohibited: Variable is already in use by the compiler." % var)
if player is not None:
player = ctx._parseExpr(player)
return "Player Variable(%s, %s)" % (player, var)
return "Global Variable(%s)" % var
def output(ctx, value, var, player=None):
var = ctx._parseExpr(var)
if var in ctx.used_vars:
raise RuntimeError("Use of external variable %s prohibited: Variable is already in use by the compiler." % var)
if player is not None:
player = ctx._parseExpr(player)
return "Set Player Variable(%s, %s, %s)" % (player, var, ctx._parseExpr(value))
return "Set Global Variable(%s, %s)" % (var, ctx._parseExpr(value))
#Override standard variable I/O to raise an exception if used
def setGlobalVariable(*args):
raise SyntaxError("Use of setGlobalVariable is prohibited. Use output() instead.")
def setPlayerVariable(*args):
raise SyntaxError("Use of setPlayerVariable is prohibited. Use output() instead.")
def setGlobalVariableAtIndex(*args):
raise SyntaxError("Use of setGlobalVariableAtIndex is prohibited. Use output() instead.")
def setPlayerVariableAtIndex(*args):
raise SyntaxError("Use of setPlayerVariableAtIndex is prohibited. Use output() instead.")
def globalVariable(*args):
raise SyntaxError("Use of globalVariable is prohibited. Use input() instead.")
def playerVariable(*args):
raise SyntaxError("Use of playerVariable is prohibited. Use input() instead.")