In Campaign mode, you can check your strategies on already defeated bases. You will not lose your troops.在戰役模式中,你能查看已被打敗的玩家的塔防策略,而且你不會損失任何戰鬥單位。Let's w ...
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