Paradox 的數據文件格式
Paradox 是我很喜歡的一個游戲公司,在所謂 P 社 5 萌中,十字軍之王和鋼鐵雄心都只有淺嘗,但在維多利亞和群星上均投入了大量時間和精力。 這些游戲基于同一套引擎,所以數據文件格式也是共通的。P 社開放了 Mod ,允許玩家來修改游戲,所以數據文件都是明文文本存放在文件系統中,這給了我們一個極好的學習機會:對于游戲從業者,我很有興趣看看成熟引擎是如何管理游戲數據和游戲邏輯的。
據我所接觸到的國內游戲公司,包括我們自己公司在內,游戲數據大都是基于 excel 這種二維表來表達的。我把它稱為 csv 模式。這種模式的特點是,基礎數據結構基于若干張二維表,每張表有不確定的行數,但每行有固定了列數。用它做基礎數據結構的缺陷是很明顯的,比如它很難表達樹狀層級結構。這往往就依賴做一個中間層,規范一些使用格式,在其上模擬出復雜數據結構。
另一種在軟件行業廣泛使用的基礎數據結構是 json/xml 模式。json 比 xml 要簡單。它的特點就是定義了兩種基礎的復合結構,字典和數組,允許結構嵌套。基于這種模式管理游戲數據的我也見過一些。不過對于策劃來說,編輯樹結構的數據終究不如 excel 拉表方便。查看起來也沒有特別好的可視化工具,所以感覺用的人要少一些。
最開始,我以為 P 社的數據文件是偏向于后一種 json 模式。但實際研究下來又覺得有很大的不同。今天我嘗試用 lpeg 寫了一個簡單的 parser 試圖把它讀進 lua vm ,寫完 parser 后突然醒悟過來,其實它就是基于的嵌套 list ,不正是 lisp 嗎?想明白這點后,有種醍醐灌頂的感覺,的確 lisp 模式要比 json 模式簡潔的多,并不比 csv 模式復雜。但表達能力卻強于它們兩者,的確是一個更好的數據組織方案。
我們來看一個從群星中隨便摘錄的例子(有點長,但挺有代表性):
- country_event = {
- id = primitive.16
- hide_window = yes
- trigger = {
- is_country_type = primitive
- has_country_flag = early_space_age
- #NOT = { has_country_flag = recently_advanced }
- OR = {
- AND = {
- exists = from
- from = {
- OR = {
- is_country_type = default
- is_country_type = awakened_fallen_empire
- }
- }
- }
- years_passed > 25
- }
- }
- mean_time_to_happen = {
- years = 100
- modifier = {
- factor = 0.6
- has_country_flag = acquired_tech
- }
- }
- immediate = {
- remove_country_flag = early_space_age
- set_country_flag = primitives_can_into_space
- set_country_type = default
- change_country_flag = random
- if = {
- limit = { is_species_class = MAM }
- set_graphical_culture = mammalian_01
- }
- if = {
- limit = { is_species_class = REP }
- set_graphical_culture = reptilian_01
- }
- if = {
- limit = { is_species_class = AVI }
- set_graphical_culture = avian_01
- }
- if = {
- limit = { is_species_class = ART }
- set_graphical_culture = arthropoid_01
- }
- if = {
- limit = { is_species_class = MOL }
- set_graphical_culture = molluscoid_01
- }
- if = {
- limit = { is_species_class = FUN }
- set_graphical_culture = fungoid_01
- }
- change_government = {
- authority = random
- civics = random
- }
- set_name = random
- if = {
- limit = {
- home_planet = {
- has_observation_outpost = yes
- }
- }
- home_planet = {
- observation_outpost_owner = {
- country_event = { id = primitive.17 }
- }
- }
- }
- add_minerals = 1000 # enough for a spaceport and then some
- add_energy = 500
- add_influence = 300
- capital_scope = {
- every_tile = {
- limit = {
- has_blocker = yes
- NOR = {
- has_blocker = tb_decrepit_dwellings
- has_blocker = tb_failing_infrastructure
- }
- }
- remove_blocker = yes
- }
- while = {
- limit = {
- num_pops < 8
- any_tile = {
- has_grown_pop = no
- has_growing_pop = no
- has_blocker = no
- }
- }
- random_tile = {
- limit = {
- has_grown_pop = no
- has_growing_pop = no
- has_blocker = no
- }
- create_pop = {
- species = owner
- }
- }
- }
- random_tile = {
- limit = {
- has_grown_pop = yes
- OR = {
- has_building = "building_primitive_farm"
- has_building = "building_primitive_factory"
- has_building = no
- }
- }
- clear_deposits = yes
- add_deposit = d_mineral_food_deposit
- set_building = "building_capital_2"
- }
- random_tile = {
- limit = {
- has_grown_pop = yes
- OR = {
- has_building = "building_primitive_farm"
- has_building = "building_primitive_factory"
- has_building = no
- }
- }
- clear_deposits = yes
- add_deposit = d_mineral_deposit
- set_building = "building_mining_network_1"
- }
- random_tile = {
- limit = {
- has_grown_pop = yes
- OR = {
- has_building = "building_primitive_farm"
- has_building = "building_primitive_factory"
- has_building = no
- }
- }
- clear_deposits = yes
- add_deposit = d_mineral_deposit
- set_building = "building_mining_network_1"
- }
- random_tile = {
- limit = {
- has_grown_pop = yes
- OR = {
- has_building = "building_primitive_farm"
- has_building = "building_primitive_factory"
- has_building = no
- }
- }
- clear_deposits = yes
- add_deposit = d_farmland_deposit
- set_building = "building_hydroponics_farm_1"
- }
- random_tile = {
- limit = {
- has_grown_pop = yes
- OR = {
- has_building = "building_primitive_farm"
- has_building = "building_primitive_factory"
- has_building = no
- }
- }
- clear_deposits = yes
- add_deposit = d_farmland_deposit
- set_building = "building_hydroponics_farm_1"
- }
- random_tile = {
- limit = {
- has_grown_pop = yes
- OR = {
- has_building = "building_primitive_farm"
- has_building = "building_primitive_factory"
- has_building = no
- }
- }
- clear_deposits = yes
- add_deposit = d_energy_deposit
- set_building = "building_power_plant_1"
- }
- random_tile = {
- limit = {
- has_grown_pop = yes
- OR = {
- has_building = "building_primitive_farm"
- has_building = "building_primitive_factory"
- has_building = no
- }
- }
- clear_deposits = yes
- add_deposit = d_energy_deposit
- set_building = "building_power_plant_1"
- }
- random_tile = {
- limit = {
- has_grown_pop = yes
- OR = {
- has_building = "building_primitive_farm"
- has_building = "building_primitive_factory"
- has_building = no
- }
- }
- clear_deposits = yes
- add_deposit = d_energy_deposit
- set_building = "building_power_plant_1"
- }
- remove_all_armies = yes
- create_army = {
- name = random
- owner = PREV
- species = owner_main_species
- type = "defense_army"
- }
- create_army = {
- name = random
- owner = PREV
- species = owner_main_species
- type = "defense_army"
- }
- create_army = {
- name = random
- owner = PREV
- species = owner_main_species
- type = "defense_army"
- }
- create_army = {
- name = random
- owner = PREV
- species = owner_main_species
- type = "defense_army"
- }
- }
- random_owned_ship = {
- limit = { is_ship_size = primitive_space_station }
- fleet = { destroy_fleet = THIS }
- }
- }
- }
起初,我很疑惑在這個格式中,為啥賦值和相等都用的 = ,這不是容易引起歧義么?但是你從 lisp 的角度來看就簡單了。等于號只是為了便于策劃書寫和閱讀的一個變形。所謂 id = primitive.16 你可以理解為 ( id, primitive.16 ) 而 iscountrytype = default 一樣可以理解為 ( iscountrytype , default ) 。 而
- create_army = {
- name = random
- owner = PREV
- species = owner_main_species
- type = "defense_army"
- }
本質上是 ( create_army , ( ( name, random ) , (owner, PREV), (species, owner_main_species), (type, "defense_army") ) )。
基礎數據結構只要能表達出來,怎么理解這些 list 是更上層的工作,這就和我們在 csv 中去模擬樹結構是一樣的道理。只不過 years_passed > 25 這樣的東西,被翻譯成 ( years_passed, > , 25 ) 有三個元素。上層解析的時候,如果確定它是一個邏輯表達式,就很容易在 2 個元素的 list 中間插入一個 = 補全。
這種結構很容易描述一些控制結構,比如上面例子中的 if 。我還在其它數據中發現了 repeat while 等控制結構,這些都是上層的工作,和底層數據模型無關。但不得不說,lisp 模式比 csv 模式更容易做此類控制結構。
把這種數據結構翻譯成 lua 也很容易:只需要用 lua table 的 array 來保存即可。但為了使用方便,可以加一個代理結構。如果上層業務想把一個 list 解析成字典,就在 cache 中臨時生成一個 hash 表加快查詢即可。我們甚至可以把它直接存在 C 內存中,只在 lua 中暴露出遍歷以及高層的訪問方法。所謂高層的訪問方法指,可以直接讀取 if repeat 等控制結構,或是把帶 AND OR 這樣的復合 list 直接翻譯成一個條件表達式。
原文鏈接:https://blog.codingnow.com/2017/07/paradox_data_format.html#more
【本文為51CTO專欄作者“云風”的原創稿件,轉載請通過51CTO聯系原作者獲取授權】






















