def pre_process(self): for i, param in enumerate(self.subtract_in): self.subtract_in[i].value = action2var.reduce_varaction2_expr(param.value, 0x0A) for i, param in enumerate(self.add_out): self.add_out[i].value = action2var.reduce_varaction2_expr(param.value, 0x0A) self.again = action2var.reduce_varaction2_expr(self.again, 0x0A) produce_base_class.pre_process(self)
def pre_process(self): generic.print_warning( "Consider using the new produce() syntax for '{}'".format( self.name), self.name.pos) for i, param in enumerate(self.param_list): self.param_list[i] = action2var.reduce_varaction2_expr(param, 0x0A) produce_base_class.pre_process(self)
def resolve_spritegroup_ref(self, sg_ref): """ Resolve a reference to a (sprite/palette) sprite group @param sg_ref: Reference to a sprite group @type sg_ref: L{SpriteGroupRef} @return: Sprite number (index of action1 set) to use @rtype: L{Expression} """ spriteset = action2.resolve_spritegroup(sg_ref.name) if len(sg_ref.param_list) == 0: offset = None elif len(sg_ref.param_list) == 1: id_dicts = [(spriteset.labels, lambda val, pos: expression.ConstantNumeric(val, pos))] offset = action2var.reduce_varaction2_expr(sg_ref.param_list[0], self.feature, self.extra_dicts + id_dicts) if isinstance(offset, expression.ConstantNumeric): generic.check_range(offset.value, 0, len(real_sprite.parse_sprite_data(spriteset)) - 1, "offset within spriteset", sg_ref.pos) else: raise generic.ScriptError("Expected 0 or 1 parameter, got " + str(len(sg_ref.param_list)), sg_ref.pos) num = action1.get_action1_index(spriteset) generic.check_range(num, 0, (1 << 14) - 1, "sprite", sg_ref.pos) return expression.ConstantNumeric(num), offset
def reduce_expressions(self, var_feature, extra_dicts=None): if extra_dicts is None: extra_dicts = [] for r in self.ranges[:]: if r.min is r.max and isinstance( r.min, expression.Identifier) and r.min.value == "default": if self.default is not None: raise generic.ScriptError( "Switch-block has more than one default, which is impossible.", r.result.pos) self.default = r.result self.ranges.remove(r) else: r.reduce_expressions(var_feature, extra_dicts) if self.default is not None: if self.default.value is not None: self.default.value = action2var.reduce_varaction2_expr( self.default.value, var_feature, extra_dicts) if len(self.ranges) != 0: if any(self.default.value != r.result.value for r in self.ranges): return generic.print_warning( "Switch-Block ranges are the same as default, optimising.", self.default.pos) self.ranges = []
def reduce_expressions(self, var_feature, extra_dicts=None): if extra_dicts is None: extra_dicts = [] self.min = self.min.reduce(global_constants.const_list) self.max = self.max.reduce(global_constants.const_list) if self.result.value is not None: self.result.value = action2var.reduce_varaction2_expr(self.result.value, var_feature, extra_dicts)
def pre_process(self): # Check parameter names seen_names = set() for param in self.param_list: if not isinstance(param, expression.Identifier): raise generic.ScriptError( "switch parameter names must be identifiers.", param.pos) if param.value in seen_names: raise generic.ScriptError( "Duplicate parameter name '{}' encountered.".format( param.value), param.pos) seen_names.add(param.value) feature = next(iter(self.feature_set)) var_feature = action2var.get_feature( self) # Feature of the accessed variables # Allocate registers param_map = {} param_registers = [] for param in self.param_list: reg = action2var.VarAction2CallParam(param.value) param_registers.append(reg) param_map[param.value] = reg param_map = (param_map, lambda name, value, pos: action2var. VarAction2LoadCallParam(value, name)) self.register_map[feature] = param_registers self.expr = action2var.reduce_varaction2_expr(self.expr, var_feature, [param_map]) self.body.reduce_expressions(var_feature, [param_map]) switch_base_class.pre_process(self)
def reduce_expressions(self, var_feature): # Do not reduce cargo-id (yet) if self.result.value is None: raise generic.ScriptError( "Returning the computed value is not possible in a graphics-block, as there is no computed value.", self.result.pos) self.result.value = action2var.reduce_varaction2_expr( self.result.value, var_feature)
def reduce_expressions(self, var_feature): self.probability = self.probability.reduce_constant( global_constants.const_list) if self.probability.value <= 0: raise generic.ScriptError( "Random probability must be higher than 0", self.probability.pos) self.result.value = action2var.reduce_varaction2_expr( self.result.value, var_feature)
def __init__(self, param_list, choices, pos): base_statement.BaseStatement.__init__(self, "random_switch-block", pos, False, False) if not (3 <= len(param_list) <= 4): raise generic.ScriptError( "random_switch requires 3 or 4 parameters, encountered {:d}".format(len(param_list)), pos ) # feature feature = general.parse_feature(param_list[0]).value # type self.type = param_list[1] # Extract type name and possible argument if isinstance(self.type, expression.Identifier): self.type_count = None elif isinstance(self.type, expression.FunctionCall): if len(self.type.params) == 0: self.type_count = None elif len(self.type.params) == 1: self.type_count = action2var.reduce_varaction2_expr(self.type.params[0], feature) else: raise generic.ScriptError( "Value for random_switch parameter 2 'type' can have only one parameter.", self.type.pos ) self.type = self.type.name else: raise generic.ScriptError( "random_switch parameter 2 'type' should be an identifier, possibly with a parameter.", self.type.pos ) # name if not isinstance(param_list[2], expression.Identifier): raise generic.ScriptError("random_switch parameter 3 'name' should be an identifier", pos) name = param_list[2] # triggers self.triggers = param_list[3] if len(param_list) == 4 else expression.ConstantNumeric(0) # body self.choices = [] self.dependent = [] self.independent = [] for choice in choices: if isinstance(choice.probability, expression.Identifier): if choice.probability.value == "dependent": self.dependent.append(choice.result) continue elif choice.probability.value == "independent": self.independent.append(choice.result) continue self.choices.append(choice) if len(self.choices) == 0: raise generic.ScriptError("random_switch requires at least one possible choice", pos) self.initialize(name, feature) self.random_act2 = None # Set during action generation to resolve dependent/independent chains
def reduce_expressions(self, var_feature): for r in self.ranges[:]: if r.min is r.max and isinstance(r.min, expression.Identifier) and r.min.value == 'default': if self.default is not None: raise generic.ScriptError("Switch-block has more than one default, which is impossible.", r.result.pos) self.default = r.result self.ranges.remove(r) else: r.reduce_expressions(var_feature) if self.default is not None and self.default.value is not None: self.default.value = action2var.reduce_varaction2_expr(self.default.value, var_feature)
def pre_process(self): generic.print_warning( generic.Warning.DEPRECATION, "Consider using the new produce() syntax for '{}'".format( self.name), self.name.pos, ) var_scope = action2var.get_scope(0x0A) for i, param in enumerate(self.param_list): self.param_list[i] = action2var.reduce_varaction2_expr( param, var_scope) produce_base_class.pre_process(self)
def pre_process(self): for graphics_def in self.graphics_list: graphics_def.reduce_expressions(item_feature) if self.default_graphics is not None: if self.default_graphics.value is None: raise generic.ScriptError( "Returning the computed value is not possible in a graphics-block, as there is no computed value.", self.pos) self.default_graphics.value = action2var.reduce_varaction2_expr( self.default_graphics.value, item_feature) # initialize base class and pre_process it as well (in that order) self.initialize(None, item_feature) graphics_base_class.pre_process(self)
def resolve_prodcb_register(param, varact2parser): if isinstance(param, expression.StorageOp) and param.name == 'LOAD_TEMP' and isinstance(param.register, expression.ConstantNumeric): # We can load a register directly res = action2var.VarAction2Var(0x7D, 0, 0xFFFFFFFF, param.register.value) else: if len(varact2parser.var_list) != 0: varact2parser.var_list.append(nmlop.VAL2) varact2parser.var_list_size += 1 varact2parser.parse_expr(action2var.reduce_varaction2_expr(param, 0x0A)) store_tmp = action2var.VarAction2StoreTempVar() res = action2var.VarAction2LoadTempVar(store_tmp) varact2parser.var_list.append(nmlop.STO_TMP) varact2parser.var_list.append(store_tmp) varact2parser.var_list_size += store_tmp.get_size() + 1 # Add 1 for operator return res
def pre_process(self): var_feature = action2var.get_feature( self) # Feature of the accessed variables self.expr = action2var.reduce_varaction2_expr(self.expr, var_feature) self.body.reduce_expressions(var_feature) switch_base_class.pre_process(self)
def reduce_expressions(self, var_feature): self.min = self.min.reduce(global_constants.const_list) self.max = self.max.reduce(global_constants.const_list) if self.result.value is not None: self.result.value = action2var.reduce_varaction2_expr( self.result.value, var_feature)
def get_layout_action2s(spritelayout, feature, spr_pos): """ @param spr_pos: Position information of the sprite view. @type spr_pos: L{Position} """ ground_sprite = None building_sprites = [] actions = [] if feature not in action2.features_sprite_layout: raise generic.ScriptError( "Sprite layouts are not supported for feature '{}'.".format( general.feature_name(feature))) # Allocate registers param_map = {} param_registers = [] for param in spritelayout.param_list: reg = action2var.VarAction2CallParam(param.value) param_registers.append(reg) param_map[param.value] = reg param_map = (param_map, lambda name, value, pos: action2var. VarAction2LoadCallParam(value, name)) spritelayout.register_map[feature] = param_registers # Reduce all expressions, can't do that earlier as feature is not known all_sprite_sets = [] layout_sprite_list = [] # Create a new structure for layout_sprite in spritelayout.layout_sprite_list: param_list = [] layout_sprite_list.append( (layout_sprite.type, layout_sprite.pos, param_list)) for param in layout_sprite.param_list: param_val = action2var.reduce_varaction2_expr( param.value, action2var.get_scope(feature), [param_map]) param_list.append((param.name, param_val)) if isinstance(param_val, expression.SpriteGroupRef): spriteset = action2.resolve_spritegroup(param_val.name) if not spriteset.is_spriteset(): raise generic.ScriptError( "Expected a reference to a spriteset.", param_val.pos) all_sprite_sets.append(spriteset) actions.extend( action1.add_to_action1(all_sprite_sets, feature, spritelayout.pos)) temp_registers = [] for type, pos, param_list in layout_sprite_list: if type.value not in layout_sprite_types: raise generic.ScriptError( "Invalid sprite type '{}' encountered. Expected 'ground', 'building', or 'childsprite'." .format(type.value), type.pos, ) sprite = Action2LayoutSprite(feature, layout_sprite_types[type.value], pos, [param_map]) for name, value in param_list: sprite.set_param(name, value) temp_registers.extend(sprite.get_all_registers()) if sprite.type == Action2LayoutSpriteType.GROUND: if ground_sprite is not None: raise generic.ScriptError( "Sprite layout can have no more than one ground sprite", spritelayout.pos) ground_sprite = sprite else: building_sprites.append(sprite) if ground_sprite is None: if len(building_sprites) == 0: # no sprites defined at all, that's not very much. raise generic.ScriptError( "Sprite layout requires at least one sprite", spr_pos) # set to 0 for no ground sprite ground_sprite = Action2LayoutSprite(feature, Action2LayoutSpriteType.GROUND) ground_sprite.set_param(expression.Identifier("sprite"), expression.ConstantNumeric(0)) action6.free_parameters.save() act6 = action6.Action6() advanced = any(x.is_advanced_sprite() for x in building_sprites + [ground_sprite]) offset = 4 sprite_num = ground_sprite.get_sprite_number() sprite_num, offset = actionD.write_action_value(sprite_num, actions, act6, offset, 4) if advanced: offset += ground_sprite.get_registers_size() for sprite in building_sprites: sprite_num = sprite.get_sprite_number() sprite_num, offset = actionD.write_action_value( sprite_num, actions, act6, offset, 4) if advanced: offset += sprite.get_registers_size() offset += 3 if sprite.type == Action2LayoutSpriteType.CHILD else 6 if len(act6.modifications) > 0: actions.append(act6) layout_action = Action2Layout( feature, spritelayout.name.value + " - feature {:02X}".format(feature), spritelayout.pos, ground_sprite, building_sprites, param_registers, ) actions.append(layout_action) if temp_registers: varact2parser = action2var.Varaction2Parser( feature, action2var.get_scope(feature)) for register_info in temp_registers: reg, expr = register_info[1], register_info[2] if reg is None: continue varact2parser.parse_expr(expr) varact2parser.var_list.append(nmlop.STO_TMP) varact2parser.var_list.append(reg) varact2parser.var_list.append(nmlop.VAL2) varact2parser.var_list_size += reg.get_size() + 2 # Only continue if we actually needed any new registers if temp_registers and varact2parser.var_list: # lgtm[py/uninitialized-local-variable] # Remove the last VAL2 operator varact2parser.var_list.pop() varact2parser.var_list_size -= 1 actions.extend(varact2parser.extra_actions) extra_act6 = action6.Action6() for mod in varact2parser.mods: extra_act6.modify_bytes(mod.param, mod.size, mod.offset + 4) if len(extra_act6.modifications) > 0: actions.append(extra_act6) varaction2 = action2var.Action2Var( feature, "{}@registers - feature {:02X}".format(spritelayout.name.value, feature), spritelayout.pos, 0x89) varaction2.var_list = varact2parser.var_list ref = expression.SpriteGroupRef(spritelayout.name, [], None, layout_action) varaction2.ranges.append( action2var.VarAction2Range(expression.ConstantNumeric(0), expression.ConstantNumeric(0), ref, "")) varaction2.default_result = ref varaction2.default_comment = "" # Add two references (default + range) # Make sure that registers allocated here are not used by the spritelayout action2.add_ref(ref, varaction2, True) action2.add_ref(ref, varaction2, True) spritelayout.set_action2(varaction2, feature) actions.append(varaction2) else: spritelayout.set_action2(layout_action, feature) action6.free_parameters.restore() return actions
def get_production_actions(produce): """ Get the action list that implements the given produce-block in nfo. @param produce: Produce-block to parse. @type produce: L{Produce} """ action_list = [] act6 = action6.Action6() action6.free_parameters.save() result_list = [] varact2parser = action2var.Varaction2Parser(0x0A) if all(x.supported_by_actionD(False) for x in produce.param_list): version = 0 offset = 4 for i, param in enumerate(produce.param_list): result, offset = actionD.write_action_value( param, action_list, act6, offset, 2 if i < 5 else 1) result_list.append(result.value) else: version = 1 for i, param in enumerate(produce.param_list): if isinstance(param, expression.StorageOp) and param.name == 'LOAD_TEMP' and \ isinstance(param.register, expression.ConstantNumeric): # We can load a register directly result_list.append( action2var.VarAction2Var(0x7D, 0, 0xFFFFFFFF, param.register.value)) else: if len(varact2parser.var_list) != 0: varact2parser.var_list.append(nmlop.VAL2) varact2parser.var_list_size += 1 varact2parser.parse_expr( action2var.reduce_varaction2_expr(param, 0x0A)) store_tmp = action2var.VarAction2StoreTempVar() result_list.append(action2var.VarAction2LoadTempVar(store_tmp)) varact2parser.var_list.append(nmlop.STO_TMP) varact2parser.var_list.append(store_tmp) varact2parser.var_list_size += store_tmp.get_size( ) + 1 # Add 1 for operator if len(act6.modifications) > 0: action_list.append(act6) prod_action = Action2Production(produce.name.value, produce.pos, version, result_list[0:3], result_list[3:5], result_list[5]) action_list.append(prod_action) if len(varact2parser.var_list) == 0: produce.set_action2(prod_action, 0x0A) else: # Create intermediate varaction2 varaction2 = action2var.Action2Var( 0x0A, '{}@registers'.format(produce.name.value), produce.pos, 0x89) varaction2.var_list = varact2parser.var_list action_list.extend(varact2parser.extra_actions) extra_act6 = action6.Action6() for mod in varact2parser.mods: extra_act6.modify_bytes(mod.param, mod.size, mod.offset + 4) if len(extra_act6.modifications) > 0: action_list.append(extra_act6) ref = expression.SpriteGroupRef(produce.name, [], None, prod_action) varaction2.ranges.append( action2var.VarAction2Range(expression.ConstantNumeric(0), expression.ConstantNumeric(0), ref, '')) varaction2.default_result = ref varaction2.default_comment = '' # Add two references (default + range) action2.add_ref(ref, varaction2) action2.add_ref(ref, varaction2) produce.set_action2(varaction2, 0x0A) action_list.append(varaction2) action6.free_parameters.restore() return action_list
def pre_process(self): for i, param in enumerate(self.param_list): self.param_list[i] = action2var.reduce_varaction2_expr(param, 0x0A) produce_base_class.pre_process(self)