EmpireofCode文档翻译 https://empireofcode.com/game/

In Campaign mode, you can check your strategies on already defeated bases. You will not lose your troops.
在战役模式中,你能查看已被打败的玩家的塔防策略,而且你不会损失任何战斗单位。
Let's work on some new code for our units. All units in the current craft run the same code which starts when a battle begins.
The script you are currently writing will command one craft. So if a craft has 7 units inside, that means 7 copies of this script will be launched.
让我们来为我们的战斗单位编写一些新的代码。 在战斗开始时开始,当前飞船中的所有战斗单位运行相同的代码。 你正在写的脚本将命令一整个飞船的战斗单位。
所以如果一个飞船里面有7个单位的话,那就意味着这个脚本的7个副本将会被启动。
The commanding principles are based on the 3 main groups of functions.
指挥的原理基于三大函数实现。
Asks are started with ask. Ask functions provide information about the unit you control or environment around it. For example,
check out the following code...
信息获取 通过 ask 开始。ask函数提供你所控制战斗单位的信息或战斗单位周围环境的信息,请看下列代码:

from battle import commander
unit_client = commander.Client()
my_info = unit_client.ask_my_info()
print("My ID:{}".format(my_info['id']))

... this shows a current unit's ID in the battle console.
上述代码显示了当前战场上的一个战斗单位的ID.

Actions are started with do. The Action function sends a command to a unit. The unit can only hold information about the last command,
so every following command will overwrite previous one. For example, check out the following code...
动作指令 通过 do 开始。动作指令函数向一个战斗单位传递一条命令。这个战斗单位只能接收最后一个命令,
所以每一条新到的命令都将覆盖前面的那条命令。如下示例:
from battle import commander
unit_client = commander.Client()
unit_client.do_move((30, 30))
unit_client.do_move((20, 30))

... that code commands units to go to the point (20, 30), but the unit will never get to the point (30, 30).
上述代码命令战斗单位前往战场坐标(20,30),但这些战斗单位永远不会前往战场坐标(30,30)。

Subscriptions are started with when. The subscribe function always has a callback argument.
订阅指令 通过 when 开始。订阅函数通常有一个 callback 的参数。
Callback is the function that gets called when a specific event occurs. For example, check out the following code...
callback 函数(要自己定义!)会在一个特殊事件发生时启动,请看下列代码:

from battle import commander
unit_client = commander.Client()

def attack_near_enemy(data):
    unit_client.do_attack(data['id'])
    unit_client.when_enemy_in_range(attack_near_enemy)

... that commands the unit to attack any enemy that comes into its firing range.
上述代码命令战斗单位攻击任何在射程内的敌方单位。

Prints. Feel free to use the print function and see every script's output in the right-hand panel for battle replays.
多用Python的print功能去查看脚本的输出。输出结果显示在右手边的战斗回放窗格中。
Your main goal is to destroy the enemy center.
你的最终目标是摧毁敌方的center.

Battle Field 战场
The battle field has a size of 40 by 40 tiles, but the half of that is occupied by rocks. The zero coordinates are placed in the top corner.
This image should help you to understand how axises are situated:
战场有一个 40*40 的面积.但有一半位置被岩石占据.(0,0)的坐标在顶部的角落上.这张图片应当能帮你了解坐标轴的坐落.
Map Axises 坐标地图轴:

Items 单位
Units, towers, buildings and other objects on the map are called "items". When you ask for info about specific items,
you will receive a dictionary with the item data, or a list of these dictionaries. The item info can contain various fields,
so it is better to use the dict.get method. An item can have the following keys:
战斗单位,塔,建筑,其它东西(主要是装饰物)在地图上统称为 "items".当你获取特定单位的信息时,
你会收到一个装有该单位信息的字典,或一个包含这些信息字典的列表.单位的信息包含各个领域,
所以最好使用python的 dict.get 方法.一个单位有下列键:

    "id": (int) Unique identifier for the item. All items have this field. (int)类型,单位的唯一标志符.所有的单位拥有这个信息.
    "player_id": (int) the ownership of the item. (int)类型,单位所有者的信息.
    "role": (str) Describes the role of the item. It can be a unit, tower, building, center, or obstacle. (str)类型,描述单位的角色属性.它可以是unit,tower,building,center,或者obstacle.
            You can read more below on the different roles. 你能从下面的文本中读到更多不同的角色属性.
    "type": (str) Describes the type of the item. It can be a sentryGun, infantryBot etc. (str)类型,描述单位的 type 类型,它能是sentryGun,infantryBot等类型.
    "hit_points": (int/float) Defines the durability of the item. If "hit_points" is zero or lower, the item is destroyed. (int/float)类型.表示血量.小于等于零时表示单位被摧毁.
    "coordinates": (list of two int/float): The item's location coordinates. Units are single point objects. (两个由浮点型或整型数字组成的列表)单位的坐标位置.战斗单位是单点对象.
                    For large objects such as buildings, this field contains the coordinates of the center (middle) point.大的对象如建筑等,这个数据表示它的中心点位置.
    "size": (int/float) Units don't have a size. All static objects (buildings, towers etc) are square and the edge length is equal to their "size".
            (int/float)类型.战斗单位无size属性.所有的静止单位(建筑,塔等)都是平面的,它们的边缘长度等于它们的size长度.
    "speed": (int/float) This is a unit attribute only. It describes how fast the unit may move. (int/float)类型,它是战斗单位独有的属性.它描述单位的移动速度.
    "damage_per_shot": (int/float) This is a unit/tower attribute which describes how many hit points an attack will take. (int/float)类型,它是塔和战斗单位才有的属性,描述每次攻击打出的伤害值.
    "rate_of_fire": (int/float) This is a unit/tower attribute which describes how many attacks per second the item can take. (int/float)类型,它是塔和战斗单位才有的属性,它描述每秒攻击的次数.
    "firing_range": (int/float) This is a unit/tower attribute which describes the maximum distance it can attack. (int/float)类型,它是塔和战斗单位的属性,描述最大攻击范围.

Roles 角色
You can use predefined constants instead of string variables. 你可以使用预定义的常量而不是字符串变量

from battle import ROLE

    unit - Mobile fighting items, these come from crafts. ROLE.UNIT #unit是移动战斗单位,从飞船里来. ROLE.UNIT
    tower - Stationary fighting items. ROLE.TOWER # tower是固定的战斗单位. ROLE.TOWER
    center - Command Centers, the main building in the game. If they're destroyed, then a battle is over. ROLE.CENTER # 指挥中心,游戏中核心建筑.如果它被摧毁,游戏结束 ROLE.CENTER
    building - All other stationary buildings. ROLE.BUILDING # 其它所有的静止事物. ROLE.BUILDING
    obstacle - Neutral stationary objects like rocks or plants. ROLE.OBSTACLE # 中性的静止物体,如岩石或植物 ROLE.OBSTACLE

Ask info 信息获取

    ask_my_info() Returns information about the current item. # 返回当前单位的信息.
    这里返还的是有多少个units就返还多少份字典数据,每个unit有自己的ID。

    ask_item_info(item_id) Returns information about the item with id == item_id or None. # 返回 id==item_id的信息或者 None.

    ask_enemy_items() Returns a list with information on the enemy items. # 以列表形式返回敌方单位的信息.

    ask_my_items() Returns a list with information on your items. # 以列表形式返回你的单位的信息.

    ask_buildings() Returns a list with information for all buildings including the Command Center. # 以列表的形式返回包括指挥中心在内的所有建筑的信息.

    ask_towers() Returns a list with information for all towers. # 返回一个包含所有的塔的信息的字典.
    返回一个包含塔信息的字典的列表,有几个塔,列表中就有几个字典

    ask_center() Returns information about the Command Center. # 返回指挥中心的信息
    返还中心(基地)的信息,格式是字典,只有一个

    ask_units() Returns a list with information for all units. # 返回一个包含所有战斗单位信息的列表
    返还一个列表,列表中是士兵的字典,士兵有几个,列表中就有几个字典

    ask_nearest_enemy() Returns a list with information on all enemies in the current item's firing range. # 以列表形式返回所有当前单位射程内的敌人信息.
    可以返还地图上的所有单位,包括中心,建筑,塔

    ask_nearest_enemy(role_list) Returns information about the nearest enemy item with role from role_list. # 返回 role-list 中的最近的敌人的信息.

from battle import ROLE
near_tower = unit_client.ask_nearest_enemy([ROLE.TOWER])

    ask_my_range_enemy_items()
    Returns a list with information on all enemies in the current items firing range. # 以列表形式返回一个当前战斗单位射程范围内的所有敌人信息.

    ask_cur_time() Returns current in-game time. (secs) # 以(secs)形式返回游戏中的时间

Commands.

    do_attack(item_id) Attack the item with id == item_id. If the target is too far, then the unit will move to the target. # 攻击 id==item_id的单位.如果单位太远,战斗单位将向目标移动.

    do_move(coordinates) move to the point with the given coordinates. coordinates: list/tuple of two int/float. # 移动到给定的坐标点.坐标形式:列表或元组形式的两个整形或浮点型数字.

    do_moves(steps) A unit only command. Move through the list of coordinates. # 战斗单位特有的命令.移动到列表所指的坐标位置.示例如下:

unit_client.do_moves([
  [35, 35],
  [35, 20]
])

def do_attack_center(*args):
    unit_client.do_atack(unit_client.ask_center()['id'])
    unit_client.when_idle(do_attack_center)

LEVEL 4 等级4

for units with level 4 or more. 对于4级或更高等级的战斗单位
        
    do_message_to_id(message, item_id) send a message to a unit with item_id. # 向一个id为item_id的战斗单位发送一个message.

    do_message_to_craft(message) send a message to all units from your craft. # 向所有来自你飞船的战斗单位发送一个message.

    do_message_to_team(message) send a message to all units from your team. # 向所有来自你的队伍的战斗单位发送一个message.

Subscribes. 订阅指令

You can subscribe your units to an event and when this event occurs the callback function will be called. # 你能为你的战斗单位订阅一个事件,当该事件触发时,callback函数将被调用.
The callback function will receive input data related to the subscription. All subscriptions are disposable and removed when triggered. # callback函数将接收与事件订阅相关的 input 数据.所有订阅都是一次性的,并在触发时被移除。

    when_in_area(center, radius, callback) Is triggered when the current unit is in the circle.
    center describes the coordinates of the center point and radius describes the length of the circle's radius.
    when_in_area(center, radius, callback) 函数在当当前单位处于一个圆圈里时触发. center参数描述圆圈的中心点,radius参数描述圆圈的半径.

    when_item_in_area(center, radius, callback) The same as whenInArea but gets triggered for any item.
    when_item_in_area(center, radius, callback) 函数与上面的when_in_area函数功能一样,但它能被任何单位触发.

    when_idle(callback) Is triggered when the current unit is idle (finishes moving, destroys an enemy or doesn't have commands).
    when_idle(callback) 函数在战斗单位闲置时触发(闲置:战斗单位完成了移动,摧毁敌人的命令或者没有命令可执行时)

    when_enemy_in_range(callback) Is triggered when an enemy item is in the current item's firing range.
    when_enemy_in_range(callback) 函数在一个敌方单位处于当前单位射程内时触发.


    when_enemy_out_range(item_id, callback) Is triggered when the item with item_id is out of the current item's firing range.
    when_enemy_out_range(item_id, callback) 函数在id为item_id的单位在当前单位射程外时触发.

    when_item_destroyed(item_id, callback) Is triggered when the item with item_id is destroyed.
    when_item_destroyed(item_id, callback) 函数在id为item_id的单位被摧毁时触发.

LEVEL 2 等级2

for units with level 2 or more. 对于2级或更高等级的战斗单位

    when_time(secs, callback) Is triggered at a specific game time. Very useful for the synchronization of units.
    when_time(secs, callback) 函数在特定的游戏时间被触发。 非常有利于同步单位。

LEVEL 4 等级4

for units with level 4 or more. 对于4级或更高等级的战斗单位

    when_message(callback, infinity=True) Is triggered when a unit gets a message from another unit.
    when_message(callback, infinity=True) 函数在当一个战斗单位从另一个战斗单位接收到了一个message时触发.
    The infinity argument indicates that you don't need to subscribe on the event again after getting the message and should be used if you want use when_message again.
    infinity参数表示在收到消息后不需要再次订阅事件,如果你想再次使用when_message,请再次调用它。
    The callback function gets one argument as a dict with the message and from_id keys.
    callback 函数用 message 和 from_id 键来获取一个字典信息的参数.

原创,转载请说明出处

参考:https://translate.google.cn/#en/zh-CN

原文地址:https://www.cnblogs.com/fengbo1113/p/8129175.html