Пример #1
0
def battle_packet_handler(data, player_data):
    if data[0] == 1:
        q = storage.sql("select battle_data from last_battle where id=?",
                        (player_data["id"], ))
        return bytearray(q[0]["battle_data"])

    print(data)
    rnd_items_1 = []
    rnd_items_2 = []
    for i in range(0, 8):
        rnd_items_1.append(random.choice(list(items.ITEM_CLASSES.keys())))
    for i in range(0, 8):
        rnd_items_2.append(random.choice(list(items.ITEM_CLASSES.keys())))
    rnd_state = {
        "species": random.choice(list(resources.MON_DATA.keys())),
        "mon_name": "Shadow",
        "moves": ["SCRATCH"],
        "name": "X",
        "items": rnd_items_1
    }

    attacking_player = json.loads(player_data["mon"])
    defending_player = trainerloader.load_trainer(data[2] * 256 + data[1])

    b = battle.battle(attacking_player, defending_player)

    r = bytearray(
        bytes(defending_player["items"]) +  # attacker items
        bytes(attacking_player["items"]) +  # defender items
        bytes([defending_player["class_id"]]) +  # trainer class id
        bytes(util.bcd(defending_player["bp_reward"])) +  # bp reward
        bytes(util.bcd(defending_player["credits_reward"])) +  # credit reward
        bytes([
            resources.MON_DATA[attacking_player["species"]]["mon_numeric_id"]
        ]) +  # attacker species id
        bytes([
            resources.MON_DATA[defending_player["species"]]["mon_numeric_id"]
        ]) +  # defender species id
        bytes(util.parse_text(defending_player["name"], 21)) +  # defender name
        b.log_bytes()  # offset to stringbuf, battle data, stringbuf
    )
    print(r)

    storage.sql(
        """
        update last_battle set battle_data=? where id=?
    """, (r, player_data["id"]))

    return r
Пример #2
0
 def on_evaded(self, owner, state, log, unique):
     if unique:
         log.announce_item_trigger()
         val = damage(state, "special", state["attacker"]["hp"] * 0.1)
         apply_damage(state["attacker"], val, log)
         log.push(BATTLE_ACTION_MESSAGE, MSG_BATTLE_NORMAL_DAMAGE,
                  state["attacker"]["string_buffer_id"], util.bcd(val))
Пример #3
0
 def on_postdamage(self, owner, state, log, unique):
     if unique:
         log.announce_item_trigger()
         val = int(state["damage_dealt"] * 0.2)
         apply_damage(owner, -val, log)
         log.push(BATTLE_ACTION_MESSAGE, MSG_BATTLE_RECOVERED,
                  util.bcd(val), log.item_stringbuf_id(self.numeric_id()))
Пример #4
0
 def on_endstep(self, owner, state, log, unique):
     if unique:
         log.announce_item_trigger()
         val = int((owner["hp"] - owner["cur_hp"]) * 0.1)
         apply_damage(owner, -val, log)
         log.push(BATTLE_ACTION_MESSAGE, MSG_BATTLE_RECOVERED,
                  util.bcd(val), log.item_stringbuf_id(self.numeric_id()))
Пример #5
0
 def on_precombat(self, owner, state, log, unique):
     log.announce_item_trigger()
     stat = random.choice(
         ("attack", "defense", "special_attack", "special_defense"))
     perc = 0.6 * owner["base_" + stat]
     owner[stat] += perc
     log.push(BATTLE_ACTION_MESSAGE, MSG_BATTLE_EFFECT_ROWHAMMER,
              log.get_stringbuf(STAT_NAMES[stat]), util.bcd(int(perc)))
Пример #6
0
 def on_postdamage(self, owner, state, log, unique):
     if unique:
         if MOVE_DATA[state["move_id"]]["effect"] in DAMAGING_EFFECTS:
             state["permvars"]["b2b_stacks"] += 1
         else:
             state["permvars"]["b2b_stacks"] = 0
         log.push(BATTLE_ACTION_MESSAGE, MSG_BATTLE_B2B,
                  util.bcd(state["permvars"]["b2b_stacks"]))
Пример #7
0
 def on_real_afterdamaged(self, owner, state, log, unique):
     if state["move_type"] == "physical" and MOVE_DATA[
             state["move_id"]]["effect"] in DAMAGING_EFFECTS:
         dmg = damage(
             state, "special",
             max(0.05 * state["attacker"]["cur_hp"],
                 0.01 * state["attacker"]["hp"]))
         log.announce_item_trigger()
         apply_damage(state["attacker"], dmg, log)
         log.push(BATTLE_ACTION_MESSAGE,
                  MSG_BATTLE_EFFECT_DAIKATANA_PHYSICAL, util.bcd(dmg),
                  log.item_stringbuf_id(self.numeric_id()))
     if state["move_type"] == "special" and MOVE_DATA[
             state["move_id"]]["effect"] in DAMAGING_EFFECTS:
         dmg = int(
             max(
                 0.05 *
                 (state["defender"]["hp"] - state["defender"]["cur_hp"]),
                 0.01 * state["defender"]["hp"]))
         log.announce_item_trigger()
         apply_damage(state["defender"], -dmg, log)
         log.push(BATTLE_ACTION_MESSAGE, MSG_BATTLE_RECOVERED,
                  util.bcd(dmg), log.item_stringbuf_id(self.numeric_id()))
Пример #8
0
 def on_predamage(self, owner, state, log, unique):
     if unique:
         if state["move_type"] == "physical":
             dmg = damage(state, "physical", 0.1 * state["defender"]["hp"])
             log.announce_item_trigger()
             apply_damage(state["defender"], dmg, log)
             log.push(BATTLE_ACTION_MESSAGE,
                      MSG_BATTLE_EFFECT_DAIKATANA_PHYSICAL, util.bcd(dmg),
                      log.item_stringbuf_id(self.numeric_id()))
         else:
             state["effective_accuracy"] += 50
             log.announce_item_trigger()
             log.push(BATTLE_ACTION_MESSAGE,
                      MSG_BATTLE_EFFECT_DAIKATANA_SPECIAL,
                      log.item_stringbuf_id(self.numeric_id()))
Пример #9
0
 def on_moveused(self, owner, state, log, unique):
     if "elemental_stacks" not in state["permvars"]:
         state["permvars"]["elemental_stacks"] = []
     if unique:
         cur_type = MOVE_DATA[state["move_id"]]["type"]
         prev_stacks = len(state["permvars"]["elemental_stacks"])
         if cur_type not in state["permvars"]["elemental_stacks"]:
             state["permvars"]["elemental_stacks"].append(cur_type)
         cur_stacks = len(state["permvars"]["elemental_stacks"])
         if cur_stacks > prev_stacks:
             log.announce_item_trigger()
             log.push(BATTLE_ACTION_MESSAGE, MSG_BATTLE_STACKS,
                      util.bcd(cur_stacks))
         state[
             "effective_attack"] += owner["base_attack"] * 0.1 * cur_stacks
         state["effective_sp_attack"] += owner[
             "base_special_attack"] * 0.1 * cur_stacks
Пример #10
0
def battle(attacking_player, defending_player):
    attacker = create_basic_state(attacking_player["species"])
    defender = create_basic_state(defending_player["species"])
    attacker["string_buffer_id"] = 0
    defender["string_buffer_id"] = 1
    attacker["moves"] = deepcopy(attacking_player["moves"])
    defender["moves"] = deepcopy(defending_player["moves"])

    defender["items"] = deepcopy(defending_player["items"])
    attacker["items"] = deepcopy(attacking_player["items"])

    battle_log = log.BattleLog()
    battle_log.add_to_stringbuf(attacking_player["mon_name"])
    battle_log.add_to_stringbuf(defending_player["mon_name"])

    turns_passed = 0

    items.load_item_classes(attacker)
    items.apply_combat_stats(attacker)
    items.load_item_classes(defender)
    items.apply_combat_stats(defender)
    attacker["cur_hp"] = attacker["hp"]
    defender["cur_hp"] = defender["hp"]

    permvars = {}
    precombat_state = {
        "attacker_turn": False,
        "win": False,
        "permvars": permvars,
        "attacker": attacker,
        "defender": defender,
        "unavoidable_encounter": False
    }

    if attacker['speed'] > defender['speed']:
        precombat_state["attacker_turn"] = True
    elif attacker['speed'] == defender['speed']:
        precombat_state["attacker_turn"] = random.random() > 0.5

    precombat_state["attacker_side"] = True
    items.precombat(attacker, precombat_state, battle_log)
    precombat_state["attacker_side"] = False
    items.precombat(defender, precombat_state, battle_log)

    if precombat_state["win"]:
        battle_log.push(BATTLE_ACTION_FAINT, defender["string_buffer_id"])

    if not precombat_state["attacker_turn"]:
        attacker, defender = defender, attacker

    if not precombat_state["win"]:
        battle_log.push(BATTLE_ACTION_MESSAGE, MSG_BATTLE_FIRST,
                        attacker["string_buffer_id"])

    while not precombat_state["win"]:
        print("****** %s %i/%i | %s %i/%i" %
              (attacker["name"], attacker["cur_hp"], attacker["hp"],
               defender["name"], defender["cur_hp"], defender["hp"]))
        print("%s: sp atk %i / sp def %i / atk %i / def %i " %
              (attacker["name"], attacker['special_attack'],
               attacker['special_defense'], attacker['attack'],
               attacker['defense']))
        print("%s: sp atk %i / sp def %i / atk %i / def %i " %
              (defender["name"], defender['special_attack'],
               defender['special_defense'], defender['attack'],
               defender['defense']))
        #print("%s items: %s" % (attacker["name"], attacker["items"]))
        #print("%s items: %s" % (defender["name"], defender["items"]))

        action_array = create_action_state(attacker, defender)
        action_array["permvars"] = permvars

        if attacker["flinching"]:
            battle_log.push(BATTLE_ACTION_MESSAGE, MSG_BATTLE_FLINCHING,
                            attacker["string_buffer_id"])
            attacker["flinching"] = False
        else:
            move_id = random.choice(attacker["moves"])
            battle_log.push(
                BATTLE_ACTION_MESSAGE, MSG_BATTLE_USED,
                attacker["string_buffer_id"],
                battle_log.get_stringbuf(MOVE_DATA[move_id]['display_name']))
            use_move(move_id, battle_log, action_array)
            if action_array["damage_dealt"] > 0:
                apply_damage(defender, action_array["damage_dealt"],
                             battle_log)
                battle_log.push(BATTLE_ACTION_MESSAGE,
                                MSG_BATTLE_NORMAL_DAMAGE,
                                defender["string_buffer_id"],
                                util.bcd(action_array["damage_dealt"]))
                items.realafterdamaged(defender, action_array, battle_log)
            if MOVE_DATA[move_id]['effect'] in EFFECT_HANDLERS:
                EFFECT_HANDLERS[MOVE_DATA[move_id]['effect']](move_id,
                                                              battle_log,
                                                              action_array)

        if defender["cur_hp"] == 0:
            battle_log.push(BATTLE_ACTION_FAINT, defender["string_buffer_id"])
            battle_log.push(BATTLE_ACTION_MESSAGE, MSG_BATTLE_FAINTED,
                            defender["string_buffer_id"])
            break

        items.endstep(attacker, action_array, battle_log)
        if action_array["win"]:
            battle_log.push(BATTLE_ACTION_FAINT, defender["string_buffer_id"])
            break

        turns_passed += 1
        if turns_passed == 10:
            battle_log.push(BATTLE_ACTION_MESSAGE, MSG_BATTLE_TURN_LIMIT_1)
        if turns_passed == 11:
            battle_log.push(BATTLE_ACTION_MESSAGE, MSG_BATTLE_TURN_LIMIT_2)
        if turns_passed == 12:
            battle_log.push(BATTLE_ACTION_MESSAGE, MSG_BATTLE_TURN_LIMIT_3)
        if turns_passed == 13:
            battle_log.push(BATTLE_ACTION_MESSAGE, MSG_BATTLE_SUDDEN_DEATH)
            atk_score = attacker["cur_hp"]
            def_score = defender["cur_hp"]
            atk_score *= defender["hp"]
            def_score *= attacker["hp"]
            if atk_score > def_score:
                apply_damage(defender, 99999, battle_log)
                battle_log.push(BATTLE_ACTION_FAINT,
                                defender["string_buffer_id"])
            else:
                apply_damage(attacker, 99999, battle_log)
                battle_log.push(BATTLE_ACTION_FAINT,
                                attacker["string_buffer_id"])
            break

        attacker, defender = defender, attacker

    return battle_log