日本综合一区二区|亚洲中文天堂综合|日韩欧美自拍一区|男女精品天堂一区|欧美自拍第6页亚洲成人精品一区|亚洲黄色天堂一区二区成人|超碰91偷拍第一页|日韩av夜夜嗨中文字幕|久久蜜综合视频官网|精美人妻一区二区三区

RELATEED CONSULTING
相關咨詢
選擇下列產品馬上在線溝通
服務時間:8:30-17:00
你可能遇到了下面的問題
關閉右側工具欄

新聞中心

這里有您想知道的互聯(lián)網營銷解決方案
Paradox的數據文件格式

Paradox 是我很喜歡的一個游戲公司,在所謂 P 社 5 萌中,十字軍之王和鋼鐵雄心都只有淺嘗,但在維多利亞和群星上均投入了大量時間和精力。 這些游戲基于同一套引擎,所以數據文件格式也是共通的。P 社開放了 Mod ,允許玩家來修改游戲,所以數據文件都是明文文本存放在文件系統(tǒng)中,這給了我們一個極好的學習機會:對于游戲從業(yè)者,我很有興趣看看成熟引擎是如何管理游戲數據和游戲邏輯的。

據我所接觸到的國內游戲公司,包括我們自己公司在內,游戲數據大都是基于 excel 這種二維表來表達的。我把它稱為 csv 模式。這種模式的特點是,基礎數據結構基于若干張二維表,每張表有不確定的行數,但每行有固定了列數。用它做基礎數據結構的缺陷是很明顯的,比如它很難表達樹狀層級結構。這往往就依賴做一個中間層,規(guī)范一些使用格式,在其上模擬出復雜數據結構。

另一種在軟件行業(yè)廣泛使用的基礎數據結構是 json/xml 模式。json 比 xml 要簡單。它的特點就是定義了兩種基礎的復合結構,字典和數組,允許結構嵌套?;谶@種模式管理游戲數據的我也見過一些。不過對于策劃來說,編輯樹結構的數據終究不如 excel 拉表方便。查看起來也沒有特別好的可視化工具,所以感覺用的人要少一些。

最開始,我以為 P 社的數據文件是偏向于后一種 json 模式。但實際研究下來又覺得有很大的不同。今天我嘗試用 lpeg 寫了一個簡單的 parser 試圖把它讀進 lua vm ,寫完 parser 后突然醒悟過來,其實它就是基于的嵌套 list ,不正是 lisp 嗎?想明白這點后,有種醍醐灌頂的感覺,的確 lisp 模式要比 json 模式簡潔的多,并不比 csv 模式復雜。但表達能力卻強于它們兩者,的確是一個更好的數據組織方案。

我們來看一個從群星中隨便摘錄的例子(有點長,但挺有代表性):

 
 
 
  1. country_event = { 
  2.     id = primitive.16 
  3.     hide_window = yes 
  4.  
  5.     trigger = { 
  6.         is_country_type = primitive 
  7.         has_country_flag = early_space_age 
  8.         #NOT = { has_country_flag = recently_advanced } 
  9.         OR = { 
  10.             AND = { 
  11.                 exists = from 
  12.                 from = { 
  13.                     OR = { 
  14.                         is_country_type = default 
  15.                         is_country_type = awakened_fallen_empire 
  16.                     } 
  17.                 } 
  18.             } 
  19.             years_passed > 25 
  20.         } 
  21.     } 
  22.  
  23.     mean_time_to_happen = { 
  24.         years = 100 
  25.  
  26.         modifier = { 
  27.             factor = 0.6 
  28.             has_country_flag = acquired_tech 
  29.         } 
  30.     } 
  31.  
  32.     immediate = { 
  33.         remove_country_flag = early_space_age 
  34.         set_country_flag = primitives_can_into_space 
  35.         set_country_type = default 
  36.         change_country_flag = random 
  37.         if = { 
  38.             limit = { is_species_class = MAM } 
  39.             set_graphical_culture = mammalian_01 
  40.         } 
  41.         if = { 
  42.             limit = { is_species_class = REP } 
  43.             set_graphical_culture = reptilian_01 
  44.         } 
  45.         if = { 
  46.             limit = { is_species_class = AVI } 
  47.             set_graphical_culture = avian_01 
  48.         } 
  49.         if = { 
  50.             limit = { is_species_class = ART } 
  51.             set_graphical_culture = arthropoid_01 
  52.         } 
  53.         if = { 
  54.             limit = { is_species_class = MOL } 
  55.             set_graphical_culture = molluscoid_01 
  56.         } 
  57.         if = { 
  58.             limit = { is_species_class = FUN } 
  59.             set_graphical_culture = fungoid_01 
  60.         } 
  61.         change_government = { 
  62.             authority = random 
  63.             civics = random 
  64.         } 
  65.         set_name = random 
  66.         if = { 
  67.             limit = { 
  68.                 home_planet = { 
  69.                     has_observation_outpost = yes 
  70.                 } 
  71.             } 
  72.             home_planet = { 
  73.                 observation_outpost_owner = { 
  74.                     country_event = { id = primitive.17 } 
  75.                 } 
  76.             } 
  77.         } 
  78.         add_minerals = 1000 # enough for a spaceport and then some 
  79.         add_energy = 500 
  80.         add_influence = 300 
  81.         capital_scope = { 
  82.             every_tile = { 
  83.                 limit = { 
  84.                     has_blocker = yes 
  85.                     NOR = { 
  86.                         has_blocker = tb_decrepit_dwellings 
  87.                         has_blocker = tb_failing_infrastructure 
  88.                     } 
  89.                 } 
  90.                 remove_blocker = yes 
  91.             } 
  92.             while = { 
  93.                 limit = {  
  94.                     num_pops < 8 
  95.                     any_tile = { 
  96.                         has_grown_pop = no 
  97.                         has_growing_pop = no 
  98.                         has_blocker = no 
  99.                     } 
  100.                 } 
  101.                 random_tile = { 
  102.                     limit = { 
  103.                         has_grown_pop = no 
  104.                         has_growing_pop = no 
  105.                         has_blocker = no 
  106.                     } 
  107.                     create_pop = { 
  108.                         species = owner 
  109.                     } 
  110.                 } 
  111.             } 
  112.             random_tile = { 
  113.                 limit = { 
  114.                     has_grown_pop = yes 
  115.                     OR = { 
  116.                         has_building = "building_primitive_farm" 
  117.                         has_building = "building_primitive_factory" 
  118.                         has_building = no 
  119.                     } 
  120.                 } 
  121.                 clear_deposits = yes 
  122.                 add_deposit = d_mineral_food_deposit 
  123.                 set_building = "building_capital_2" 
  124.             } 
  125.             random_tile = { 
  126.                 limit = { 
  127.                     has_grown_pop = yes 
  128.                     OR = { 
  129.                         has_building = "building_primitive_farm" 
  130.                         has_building = "building_primitive_factory" 
  131.                         has_building = no 
  132.                     } 
  133.                 } 
  134.                 clear_deposits = yes 
  135.                 add_deposit = d_mineral_deposit 
  136.                 set_building = "building_mining_network_1" 
  137.             } 
  138.             random_tile = { 
  139.                 limit = { 
  140.                     has_grown_pop = yes 
  141.                     OR = { 
  142.                         has_building = "building_primitive_farm" 
  143.                         has_building = "building_primitive_factory" 
  144.                         has_building = no 
  145.                     } 
  146.                 } 
  147.                 clear_deposits = yes 
  148.                 add_deposit = d_mineral_deposit 
  149.                 set_building = "building_mining_network_1" 
  150.             } 
  151.             random_tile = { 
  152.                 limit = { 
  153.                     has_grown_pop = yes 
  154.                     OR = { 
  155.                         has_building = "building_primitive_farm" 
  156.                         has_building = "building_primitive_factory" 
  157.                         has_building = no 
  158.                     } 
  159.                 } 
  160.                 clear_deposits = yes 
  161.                 add_deposit = d_farmland_deposit 
  162.                 set_building = "building_hydroponics_farm_1" 
  163.             } 
  164.             random_tile = { 
  165.                 limit = { 
  166.                     has_grown_pop = yes 
  167.                     OR = { 
  168.                         has_building = "building_primitive_farm" 
  169.                         has_building = "building_primitive_factory" 
  170.                         has_building = no 
  171.                     } 
  172.                 } 
  173.                 clear_deposits = yes 
  174.                 add_deposit = d_farmland_deposit 
  175.                 set_building = "building_hydroponics_farm_1" 
  176.             } 
  177.             random_tile = { 
  178.                 limit = { 
  179.                     has_grown_pop = yes 
  180.                     OR = { 
  181.                         has_building = "building_primitive_farm" 
  182.                         has_building = "building_primitive_factory" 
  183.                         has_building = no 
  184.                     } 
  185.                 } 
  186.                 clear_deposits = yes 
  187.                 add_deposit = d_energy_deposit 
  188.                 set_building = "building_power_plant_1" 
  189.             } 
  190.             random_tile = { 
  191.                 limit = { 
  192.                     has_grown_pop = yes 
  193.                     OR = { 
  194.                         has_building = "building_primitive_farm" 
  195.                         has_building = "building_primitive_factory" 
  196.                         has_building = no 
  197.                     } 
  198.                 } 
  199.                 clear_deposits = yes 
  200.                 add_deposit = d_energy_deposit 
  201.                 set_building = "building_power_plant_1" 
  202.             } 
  203.             random_tile = { 
  204.                 limit = { 
  205.                     has_grown_pop = yes 
  206.                     OR = { 
  207.                         has_building = "building_primitive_farm" 
  208.                         has_building = "building_primitive_factory" 
  209.                         has_building = no 
  210.                     } 
  211.                 } 
  212.                 clear_deposits = yes 
  213.                 add_deposit = d_energy_deposit 
  214.                 set_building = "building_power_plant_1" 
  215.             } 
  216.             remove_all_armies = yes 
  217.             create_army = { 
  218.                 name = random 
  219.                 owner = PREV 
  220.                 species = owner_main_species 
  221.                 type = "defense_army" 
  222.             } 
  223.             create_army = { 
  224.                 name = random 
  225.                 owner = PREV 
  226.                 species = owner_main_species 
  227.                 type = "defense_army" 
  228.             } 
  229.             create_army = { 
  230.                 name = random 
  231.                 owner = PREV 
  232.                 species = owner_main_species 
  233.                 type = "defense_army" 
  234.             } 
  235.             create_army = { 
  236.                 name = random 
  237.                 owner = PREV 
  238.                 species = owner_main_species 
  239.                 type = "defense_army" 
  240.             } 
  241.         } 
  242.         random_owned_ship = { 
  243.             limit = { is_ship_size = primitive_space_station } 
  244.             fleet = { destroy_fleet = THIS } 
  245.         } 
  246.     } 

起初,我很疑惑在這個格式中,為啥賦值和相等都用的 = ,這不是容易引起歧義么?但是你從 lisp 的角度來看就簡單了。等于號只是為了便于策劃書寫和閱讀的一個變形。所謂 id = primitive.16 你可以理解為 ( id, primitive.16 ) 而 iscountrytype = default 一樣可以理解為 ( iscountrytype , default ) 。 而

 
 
 
  1. create_army = { 
  2.                 name = random 
  3.                 owner = PREV 
  4.                 species = owner_main_species 
  5.                 type = "defense_army" 
  6.             } 

本質上是 ( create_army , ( ( name, random ) , (owner, PREV), (species, owner_main_species), (type, "defense_army") ) )。

基礎數據結構只要能表達出來,怎么理解這些 list 是更上層的工作,這就和我們在 csv 中去模擬樹結構是一樣的道理。只不過 years_passed > 25 這樣的東西,被翻譯成 ( years_passed, > , 25 ) 有三個元素。上層解析的時候,如果確定它是一個邏輯表達式,就很容易在 2 個元素的 list 中間插入一個 = 補全。

這種結構很容易描述一些控制結構,比如上面例子中的 if 。我還在其它數據中發(fā)現(xiàn)了 repeat while 等控制結構,這些都是上層的工作,和底層數據模型無關。但不得不說,lisp 模式比 csv 模式更容易做此類控制結構。

把這種數據結構翻譯成 lua 也很容易:只需要用 lua table 的 array 來保存即可。但為了使用方便,可以加一個代理結構。如果上層業(yè)務想把一個 list 解析成字典,就在 cache 中臨時生成一個 hash 表加快查詢即可。我們甚至可以把它直接存在 C 內存中,只在 lua 中暴露出遍歷以及高層的訪問方法。所謂高層的訪問方法指,可以直接讀取 if repeat 等控制結構,或是把帶 AND OR 這樣的復合 list 直接翻譯成一個條件表達式。

原文鏈接:https://blog.codingnow.com/2017/07/paradox_data_format.html#more

【本文為專欄作者“云風”的原創(chuàng)稿件,轉載請通過聯(lián)系原作者獲取授權】

戳這里,看該作者更多好文


本文名稱:Paradox的數據文件格式
網站URL:http://www.dlmjj.cn/article/djichoi.html