2020-11-08 07:18:12 +00:00
|
|
|
import sys
|
2020-11-09 04:15:31 +00:00
|
|
|
import json
|
2020-11-09 06:09:47 +00:00
|
|
|
import random
|
2020-11-09 06:38:02 +00:00
|
|
|
import logger
|
2020-11-08 07:18:12 +00:00
|
|
|
|
2020-11-08 07:16:30 +00:00
|
|
|
this = sys.modules[__name__]
|
|
|
|
|
2020-11-09 05:33:07 +00:00
|
|
|
this.encoded = None
|
|
|
|
this.pattern = None
|
2020-11-09 04:15:31 +00:00
|
|
|
this.values = {
|
|
|
|
"stack": 0,
|
2020-11-10 22:59:59 +00:00
|
|
|
"stack2": 0,
|
|
|
|
"stack3": 0,
|
|
|
|
"stack4": 0,
|
|
|
|
"stack5": 0,
|
|
|
|
"stack6": 0,
|
2020-11-11 00:15:29 +00:00
|
|
|
"stackr": 0,
|
|
|
|
"stackg": 0,
|
|
|
|
"stackb": 0,
|
2020-11-09 04:15:31 +00:00
|
|
|
"r": 0,
|
|
|
|
"g": 0,
|
2020-11-09 06:09:47 +00:00
|
|
|
"b": 0,
|
|
|
|
"tick": 0,
|
|
|
|
"index": 0
|
2020-11-09 04:15:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-11-10 22:59:59 +00:00
|
|
|
def constant(target, arg, index):
|
|
|
|
return (arg, index)
|
2020-11-09 04:15:31 +00:00
|
|
|
|
|
|
|
|
2020-11-10 22:59:59 +00:00
|
|
|
def add(target, arg, index):
|
|
|
|
return (target + arg, index)
|
2020-11-09 04:15:31 +00:00
|
|
|
|
|
|
|
|
2020-11-10 22:59:59 +00:00
|
|
|
def sub(target, arg, index):
|
|
|
|
return (target - arg, index)
|
2020-11-09 06:29:22 +00:00
|
|
|
|
|
|
|
|
2020-11-10 22:59:59 +00:00
|
|
|
def mult(target, arg, index):
|
|
|
|
return (target * arg, index)
|
2020-11-09 06:29:22 +00:00
|
|
|
|
|
|
|
|
2020-11-10 22:59:59 +00:00
|
|
|
def div(target, arg, index):
|
|
|
|
return (target / arg, index)
|
2020-11-09 06:29:22 +00:00
|
|
|
|
|
|
|
|
2020-11-10 22:59:59 +00:00
|
|
|
def mod(target, arg, index):
|
2020-11-09 06:33:30 +00:00
|
|
|
if arg <= 0:
|
2020-11-09 06:38:02 +00:00
|
|
|
return target
|
2020-11-10 22:59:59 +00:00
|
|
|
return (target % arg, index)
|
2020-11-09 04:15:31 +00:00
|
|
|
|
|
|
|
|
2020-11-09 06:09:47 +00:00
|
|
|
def rand(target, arg):
|
|
|
|
return random.randrange(0, 255)
|
|
|
|
|
|
|
|
|
2020-11-10 22:59:59 +00:00
|
|
|
def jnz(target, arg, index):
|
|
|
|
if target != 0:
|
|
|
|
return (target, arg)
|
|
|
|
else:
|
|
|
|
return (target, index)
|
|
|
|
|
|
|
|
|
|
|
|
def jez(target, arg, index):
|
|
|
|
if target == 0:
|
|
|
|
return (target, arg)
|
|
|
|
else:
|
|
|
|
return (target, index)
|
|
|
|
|
|
|
|
|
|
|
|
def _apply(index, target, arg, func):
|
2020-11-09 06:48:04 +00:00
|
|
|
if type(arg) is int:
|
2020-11-11 00:28:48 +00:00
|
|
|
print("{} {} {} {}".format(index, func.__name__, target, arg))
|
2020-11-10 22:59:59 +00:00
|
|
|
return func(target, arg, index)
|
2020-11-09 06:48:04 +00:00
|
|
|
elif type(arg) is str and arg in this.values:
|
2020-11-11 00:28:48 +00:00
|
|
|
print("{} {} {} {}".format(index, func.__name__, target, this.values[arg]))
|
2020-11-10 22:59:59 +00:00
|
|
|
return func(target, this.values[arg], index)
|
2020-11-09 06:09:47 +00:00
|
|
|
|
|
|
|
|
2020-11-10 22:59:59 +00:00
|
|
|
def apply(index, targets, args, func):
|
2020-11-09 04:15:31 +00:00
|
|
|
for target in range(len(targets)):
|
2020-11-09 05:51:49 +00:00
|
|
|
if this.values[targets[target]['channel']] != None:
|
2020-11-09 07:06:05 +00:00
|
|
|
if target < len(args):
|
2020-11-10 22:59:59 +00:00
|
|
|
val, jump = _apply(
|
|
|
|
index, this.values[targets[target]['channel']], args[target], func)
|
|
|
|
if val != this.values[targets[target]['channel']]:
|
|
|
|
this.values[targets[target]['channel']] = val
|
2020-11-10 23:07:36 +00:00
|
|
|
return jump
|
2020-11-09 06:09:47 +00:00
|
|
|
else:
|
2020-11-10 22:59:59 +00:00
|
|
|
val, jump = _apply(
|
|
|
|
index, this.values[targets[target]['channel']], 0, func)
|
|
|
|
if val != this.values[targets[target]['channel']]:
|
|
|
|
this.values[targets[target]['channel']] = val
|
2020-11-10 23:07:36 +00:00
|
|
|
return jump
|
2020-11-09 04:15:31 +00:00
|
|
|
|
|
|
|
|
|
|
|
this.instructions = {
|
|
|
|
"CONSTANT": constant,
|
|
|
|
"ADD": add,
|
2020-11-09 06:29:22 +00:00
|
|
|
"SUBTRACT": sub,
|
|
|
|
"MULTIPLY": mult,
|
|
|
|
"DIVIDE": div,
|
|
|
|
"MODULO": mod,
|
2020-11-10 22:59:59 +00:00
|
|
|
"RANDOM": rand,
|
|
|
|
"JNZ": jnz,
|
|
|
|
"JEZ": jez
|
2020-11-09 04:15:31 +00:00
|
|
|
}
|
|
|
|
|
2020-11-08 07:16:30 +00:00
|
|
|
|
|
|
|
def default(index, tick):
|
|
|
|
return (((index + tick) * 5) % 255, (tick * 42) % 255, (tick * 50) % 255)
|
|
|
|
|
|
|
|
|
|
|
|
def pat(index, tick, previous_values):
|
2020-11-09 06:35:07 +00:00
|
|
|
this.values['tick'] = tick
|
|
|
|
this.values['index'] = index
|
2020-11-09 04:15:31 +00:00
|
|
|
if this.pattern != None:
|
|
|
|
for i in range(len(this.pattern)):
|
2020-11-09 07:52:45 +00:00
|
|
|
if i < len(this.pattern):
|
|
|
|
name = this.pattern[i]['instruction']['name']
|
|
|
|
targets = this.pattern[i]['instruction']['targets']
|
|
|
|
args = []
|
|
|
|
if 'args' in this.pattern[i]['instruction']:
|
|
|
|
args = this.pattern[i]['instruction']['args']
|
|
|
|
if this.instructions[name] != None:
|
2020-11-11 00:30:06 +00:00
|
|
|
jump = apply(i, targets, args, this.instructions[name])
|
2020-11-10 22:59:59 +00:00
|
|
|
if jump != i and jump < len(this.pattern) - 1:
|
|
|
|
i = jump - 1
|
2020-11-09 04:15:31 +00:00
|
|
|
r = this.values["r"]
|
|
|
|
g = this.values["g"]
|
|
|
|
b = this.values["b"]
|
|
|
|
this.values["r"] = 0
|
|
|
|
this.values["g"] = 0
|
|
|
|
this.values["b"] = 0
|
|
|
|
this.values["stack"] = 0
|
2020-11-09 06:29:22 +00:00
|
|
|
if r > 255:
|
|
|
|
r = 255
|
|
|
|
elif r < 0:
|
|
|
|
r = 0
|
|
|
|
if g > 255:
|
|
|
|
g = 255
|
|
|
|
elif g < 0:
|
|
|
|
g = 0
|
|
|
|
if b > 255:
|
|
|
|
b = 255
|
|
|
|
elif b < 0:
|
|
|
|
b = 0
|
2020-11-09 04:15:31 +00:00
|
|
|
return (r, g, b)
|
|
|
|
else:
|
|
|
|
return default(index, tick)
|
2020-11-08 07:16:30 +00:00
|
|
|
|
|
|
|
|
|
|
|
def parse(str):
|
|
|
|
this.encoded = str
|
2020-11-09 06:38:02 +00:00
|
|
|
logger.debug(this.encoded)
|
2020-11-09 04:15:31 +00:00
|
|
|
this.pattern = json.loads(this.encoded)
|