(combat-base-framework)= # 作戰基地框架 戰鬥是許多遊戲的核心。其具體運作方式很大程度取決於遊戲。在本課中,我們將建立一個框架來實現兩種常見的風格: - 「基於抽搐的」戰鬥([此處的特定課程](./Beginner-Tutorial-Combat-Twitch.md))意味著您透過輸入指令來執行戰鬥動作,並在一段延遲後(這可能取決於您的技能等),該動作發生。之所以稱為“抽搐”,是因為行動通常發生得足夠快,以至於改變策略可能需要快速思考和“抽動扳機手指”。 - 「回合製」戰鬥([具體教訓](./Beginner-Tutorial-Combat-Turnbased.md))意味著玩家以清晰的回合輸入動作。輸入/排隊操作的超時通常比基於抽搐的風格長得多。一旦每個人都做出了選擇(或達到超時),每個人的行動都會立即發生,然後在下一回合開始。這種戰鬥方式需要較少的玩家反應。 我們將設計一個支援這兩種風格的基礎戰鬥系統。 - 我們需要`CombatHandler`來追蹤戰鬥進度。這將是 [Script](../../../Components/Scripts.md)。 Twitch 戰鬥和回合製戰鬥的具體工作方式(以及存放位置)會有所不同。我們將在本課中建立其通用框架。 - 戰鬥分為_動作_。我們希望能夠透過更多可能的行動輕鬆擴充套件我們的戰鬥。操作需要 Python 程式碼來顯示執行操作時實際發生的情況。我們將在`Action`類別中定義這樣的程式碼。 - 我們還需要一種方法來描述給定操作的_特定例項_。也就是說,當我們做出「攻擊」動作時,我們至少需要知道誰在被攻擊。為此,我們將使用 Python `dicts`,我們稱之為 `action_dicts`。 (combathandler)= ## CombatHandler > 建立一個新模組`evadventure/combat_base.py` ```{sidebar} 在 `evennia/contrib/tutorials/evadventure/` 下的 [combat_base.py](evennia.contrib.tutorials.evadventure.combat_base) 中,您將找到基本戰鬥模組的完整實作。 ``` 我們的「戰鬥處理程式」將負責戰鬥方面的管理。它需要是_持久的_(即使我們重新載入伺服器,你的戰鬥也應該繼續進行)。 建立 CombatHandler 有點像第 22 條軍規 - 它的工作原理取決於 Actions 和 Action-dicts 的外觀。但如果沒有 CombatHandler,就很難知道如何設計 Actions 和 Action-dicts。因此,我們將從其總體結構開始,並在本課後面填寫詳細資料。 下面,帶有 `pass` 的方法將在本課中填寫,而那些提高 `NotImplementedError` 的方法將在 Twitch/回合製戰鬥中有所不同,並將在本課之後的各自課程中實施。 ```python # in evadventure/combat_base.py from evennia import DefaultScript class CombatFailure(RuntimeError): """If some error happens in combat""" pass class EvAdventureCombatBaseHandler(DefaultSCript): """ This should be created when combat starts. It 'ticks' the combat and tracks all sides of it. """ # common for all types of combat action_classes = {} # to fill in later fallback_action_dict = {} @classmethod def get_or_create_combathandler(cls, obj, **kwargs): """ Get or create combathandler on `obj`.""" pass def msg(self, message, combatant=None, broadcast=True, location=True): """ Send a message to all combatants. """ pass # TODO def get_combat_summary(self, combatant): """ Get a nicely formatted 'battle report' of combat, from the perspective of the combatant. """ pass # TODO # implemented differently by Twitch- and Turnbased combat def get_sides(self, combatant): """ Get who's still alive on the two sides of combat, as a tuple `([allies], [enemies])` from the perspective of `combatant` (who is _not_ included in the `allies` list. """ raise NotImplementedError def give_advantage(self, recipient, target): """ Give advantage to recipient against target. """ raise NotImplementedError def give_disadvantage(self, recipient, target): """ Give disadvantage to recipient against target. """ raise NotImplementedError def has_advantage(self, combatant, target): """ Does combatant have advantage against target? """ raise NotImplementedError def has_disadvantage(self, combatant, target): """ Does combatant have disadvantage against target? """ raise NotImplementedError def queue_action(self, combatant, action_dict): """ Queue an action for the combatant by providing action dict. """ raise NotImplementedError def execute_next_action(self, combatant): """ Perform a combatant's next action. """ raise NotImplementedError def start_combat(self): """ Start combat. """ raise NotImplementedError def check_stop_combat(self): """ Check if the combat is over and if it should be stopped. """ raise NotImplementedError def stop_combat(self): """ Stop combat and do cleanup. """ raise NotImplementedError ``` 戰鬥處理程式是[Script](../../../Components/Scripts.md)。 Scripts 是型別分類實體,這意味著它們永續性地儲存在資料庫中。 Scripts 可以選擇儲存在其他物件「上」(例如角色或房間),或在沒有任何此類連線的情況下儲存為「全域」。雖然 Scripts 有一個可選的計時器元件,但預設情況下它不處於活動狀態,並且 Scripts 通常用作普通儲存。由於 Scripts 在遊戲中不存在,因此它們非常適合在各種「系統」上儲存資料,包括我們的戰鬥。 讓我們實作我們需要的通用方法。 (combathandlerget_or_create_combathandler)= ### CombatHandler。 get_or_create_combathandler 一種快速獲取正在進行的戰鬥和戰鬥人員的戰鬥處理程式的輔助方法。 我們期望在一個物件「上」建立script(我們還不知道哪個物件,但我們期望它是一個型別分類的實體)。 ```python # in evadventure/combat_base.py from evennia import create_script # ... class EvAdventureCombatBaseHandler(DefaultScript): # ... @classmethod def get_or_create_combathandler(cls, obj, **kwargs): """ Get or create a combathandler on `obj`. Args: obj (any): The Typeclassed entity to store this Script on. Keyword Args: combathandler_key (str): Identifier for script. 'combathandler' by default. **kwargs: Extra arguments to the Script, if it is created. """ if not obj: raise CombatFailure("Cannot start combat without a place to do it!") combathandler_key = kwargs.pop("key", "combathandler") combathandler = obj.ndb.combathandler if not combathandler or not combathandler.id: combathandler = obj.scripts.get(combathandler_key).first() if not combathandler: # have to create from scratch persistent = kwargs.pop("persistent", True) combathandler = create_script( cls, key=combathandler_key, obj=obj, persistent=persistent, **kwargs, ) obj.ndb.combathandler = combathandler return combathandler # ... ``` 此輔助方法使用 `obj.scripts.get()` 來查詢戰鬥 script 是否已經存在於所提供的 `obj` 上。如果沒有,它將使用 Evennia 的 [create_script](evennia.utils.create.create_script) 函式來建立它。為了獲得額外的速度,我們將處理程式快取為 `obj.ndb.combathandler` `.ndb.`(非資料庫)意味著處理程式僅快取在記憶體中。 ```{sidebar} 檢查.id(或.pk) 當從快取中獲取它時,我們確保也檢查我們獲得的戰鬥處理程式是否有一個不是 `None` 的資料庫 `.id` (我們還可以檢查 `.pk`,代表「主鍵」)。如果是 `None`,這表示資料庫實體已被刪除,我們剛剛從記憶體中獲取了其快取的 Python 表示形式 - 我們需要重新建立它。 ``` `get_or_create_combathandler` 被修飾為 [classmethod](https://docs.python.org/3/library/functions.html#classmethod),這意味著它應該直接在處理程式類別上使用(而不是在所述類別的例項上)。這是有道理的,因為該方法實際上應該傳回新例項。 作為類別方法,我們需要直接在類別上呼叫它,如下所示: ```python combathandler = EvAdventureCombatBaseHandler.get_or_create_combathandler(combatant) ``` 結果將會是一個新的處理程式或一個已定義的處理程式。 (combathandlermsg)= ### CombatHandler.msg ```python # in evadventure/combat_base.py # ... class EvAdventureCombatBaseHandler(DefaultScript): # ... def msg(self, message, combatant=None, broadcast=True, location=None): """ Central place for sending messages to combatants. This allows for adding any combat-specific text-decoration in one place. Args: message (str): The message to send. combatant (Object): The 'You' in the message, if any. broadcast (bool): If `False`, `combatant` must be included and will be the only one to see the message. If `True`, send to everyone in the location. location (Object, optional): If given, use this as the location to send broadcast messages to. If not, use `self.obj` as that location. Notes: If `combatant` is given, use `$You/you()` markup to create a message that looks different depending on who sees it. Use `$You(combatant_key)` to refer to other combatants. """ if not location: location = self.obj location_objs = location.contents exclude = [] if not broadcast and combatant: exclude = [obj for obj in location_objs if obj is not combatant] location.msg_contents( message, exclude=exclude, from_obj=combatant, mapping={locobj.key: locobj for locobj in location_objs}, ) # ... ``` ```{sidebar} Script 的 `self.obj` 屬性是 Script “所在”的實體。如果在角色上設定,`self.obj` 將是該角色。如果在一個房間裡,那就是那個房間。對於全域 script,`self.obj` 是 `None`。 ``` 我們之前在[物件課的武器類](./Beginner-Tutorial-Objects.md#weapons)中看到了`location.msg_contents()`方法。其目的是獲取 `"$You() do stuff against $you(key)"` 形式的字串,並確保各方都能看到適合自己的字串。預設情況下,我們的 `msg()` 方法會將訊息廣播給房間中的每個人。
你會這樣使用它: ```python combathandler.msg( f"$You() $conj(throw) {item.key} at $you({target.key}).", combatant=combatant, location=combatant.location ) ``` 如果戰鬥者是`Trickster`,`item.key`是“綵球”,`target.key`是“哥布林”,那麼 戰鬥者會看到: You throw a colorful ball at Goblin. 哥布林看見了 騙子丟一個彩色的球給你。 房間裡的其他人都看到了 騙子向哥布林丟了一個彩色球。 (combathandlerget_combat_summary)= ### 戰鬥人員。 get_combat_summary 我們希望能夠展示當前戰鬥的精彩總結: ```shell Goblin shaman (Perfect) Gregor (Hurt) Goblin brawler(Hurt) Bob (Perfect) vs Goblin grunt 1 (Hurt) Goblin grunt 2 (Perfect) Goblin grunt 3 (Wounded) ``` ```{code-block} python :linenos: :emphasize-lines: 15,17,21,22,28,41 # in evadventure/combat_base.py # ... from evennia import EvTable # ... class EvAdventureCombatBaseHandler(DefaultScript): # ... def get_combat_summary(self, combatant): allies, enemies = self.get_sides(combatant) nallies, nenemies = len(allies), len(enemies) # prepare colors and hurt-levels allies = [f"{ally} ({ally.hurt_level})" for ally in allies] enemies = [f"{enemy} ({enemy.hurt_level})" for enemy in enemies] # the center column with the 'vs' vs_column = ["" for _ in range(max(nallies, nenemies))] vs_column[len(vs_column) // 2] = "|wvs|n" # the two allies / enemies columns should be centered vertically diff = abs(nallies - nenemies) top_empty = diff // 2 bot_empty = diff - top_empty topfill = ["" for _ in range(top_empty)] botfill = ["" for _ in range(bot_empty)] if nallies >= nenemies: enemies = topfill + enemies + botfill else: allies = topfill + allies + botfill # make a table with three columns return evtable.EvTable( table=[ evtable.EvColumn(*allies, align="l"), evtable.EvColumn(*vs_column, align="c"), evtable.EvColumn(*enemies, align="r"), ], border=None, maxwidth=78, ) # ... ``` 這看起來可能很複雜,但複雜之處僅在於弄清楚如何組織三列,尤其是如何調整到兩側的`vs`大致垂直對齊。 - **第 15 行**:我們使用了 `self.get_sides(combatant)` 方法,但我們尚未實際實現。這是因為基於回合和基於抽搐的戰鬥需要不同的方式來找出雙方是誰。 `allies` 和 `enemies` 是清單。 - **第 17 行**:`combatant` 不是 `allies` 清單的一部分(這就是我們定義 `get_sides` 的工作方式),因此我們將其插入清單的頂部(因此它們首先顯示在左側)。 - **第 21、22 行**:我們利用所有生物的 `.hurt_level` 值(請參閱[角色課程的LivingMixin](./Beginner-Tutorial-Characters.md))。 - **第28-39行**:我們透過在內容的上方和下方新增空行來確定如何使兩側垂直居中。 - **第 41 行**:[Evtable](../../../Components/EvTable.md) 是一個用於製作文字表的 Evennia 實用程式。一旦我們對這些列感到滿意,我們就把它們輸入到表中,然後讓 Evennia 完成剩下的工作。 `EvTable` 值得探索,因為它可以幫助您建立各種漂亮的佈局。 (actions)= ## 行動 在EvAdventure中,我們將只支援一些常見的戰鬥動作,對映到_Knave_中使用的等效擲骰和檢定。我們將設計我們的戰鬥框架,以便以後可以輕鬆地透過其他動作進行擴充套件。 - `hold` - 最簡單的操作。你只是向後靠,什麼都不做。 - `attack` - 你使用目前裝備的武器攻擊給定的`target`。這將成為針對目標ARMOR的STR或WIS擲骰。 - `stunt` - 你做了一個“特技”,用角色扮演的術語來說,這意味著你絆倒你的對手,嘲諷或以其他方式試圖在不傷害他們的情況下佔據上風。您可以這樣做,為自己(或盟友)在下一步行動中提供_優勢_`target`。您也可以針對您或盟友的下一步給予 `target` _disadvantage_。 - `use item` - 您使用庫存中的`Consumable`。當對自己使用時,它通常就像治療藥水一樣。如果對敵人使用,它可能是燃燒彈或一瓶酸。 - `wield` - 你擁有一件物品。根據所使用的物品,它會以不同的方式使用:頭盔會戴在頭上,一件盔甲會戴在胸前。一隻手揮舞著劍,另一手揮舞著盾牌。雙手斧頭會用掉兩隻手。這樣做會將之前的所有內容移至揹包中。 - `flee` - 你逃跑/脫離。此動作僅適用於回合製戰鬥(在基於抽搐的戰鬥中,您只需移動到另一個房間即可逃離)。因此,我們將等到[回合製戰鬥課程](./Beginner-Tutorial-Combat-Turnbased.md)後再定義此動作。 (action-dicts)= ## 行動指令 為了傳遞攻擊的詳細資訊(上面的第二點),我們將使用 `dict`。 `dict` 很簡單,也很容易儲存在 `Attribute` 中。我們稱之為 `action_dict`,這是每個操作所需的內容。 > 您無需在任何地方輸入這些內容,此處列出以供參考。我們將在呼叫 `combathandler.queue_action(combatant, action_dict)` 時使用這些字典。 ```python hold_action_dict = { "key": "hold" } attack_action_dict = { "key": "attack", "target":