旧月 发表于 2010-8-27 17:50:13

AI的一些实用小技巧 附实例 对做AI不小帮助

每隔一段时间更新一些。。持续更新。。大部分是我从别的AI里找的。

检测年代:
(defconst g-age-status 1) ;用来检测年代的GOAL
(defconst gv-dark-age 0)
(defconst gv-advancing-to-feudal 1)
(defconst gv-feudal-age 2)
(defconst gv-advancing-to-castle 3)
(defconst gv-castle-age 4)
(defconst gv-advancing-to-imperial 5)
(defconst gv-imperial-age 6)
;不同的值代表不同时期
(defrule
   (true)
   =>
   (set-goal g-age-status gv-dark-age)
    (disable-self)
)

(defrule
   (can-research feudal-age)
   =>
   (research feudal-age)
   (set-goal g-age-status gv-advancing-to-feudal)
;相当于 (set-goal 1 1)设成这个值代表目前的年代是 升级封建时代中
)
(defrule
   (current-age == feudal-age)
   =>
   (set-goal g-age-status gv-feudal-age)
   (disable-self)
)
;升级到封建时代了。
(defrule
   (can-research castle-age)
   =>
   (research castle-age)
   (set-goal g-age-status gv-advancing-to-castle)
)
(defrule
   (current-age == castle-age)
   =>
   (set-goal g-age-status gv-castle-age)
   (disable-self)
)
(defrule
   (can-research imperial-age)
   =>
   (research imperial-age)
   (set-goal g-age-status gv-advancing-to-imperial)
)
(defrule
   (current-age == imperial-age)
   =>
   (set-goal g-age-status gv-imperial-age)
   (disable-self)
)

利用这个可以做出许多效果,例如升级城堡时代时才研究科技,或者升城时候建马厩,等等。

旧月 发表于 2010-8-27 17:53:03

采矿采完

   (cc-players-unit-type-count 0 66 == 0) ;没金子
   (cc-players-unit-type-count 0 102 == 0) ;没石头

利用这个可以调节分配,当然不一定是0,你可以自己更改

旧月 发表于 2010-8-27 17:55:56

初期的食物搜寻


;全员搜索
(defrule
   (game-time < 600)
   (sheep-and-forage-too-far)
   (not(resource-found food)) ;没找到食物
   (building-type-count-total mill == 0)
   =>
   (set-strategic-number sn-percent-civilian-explorers 100)
   (set-strategic-number sn-minimum-civilian-explorers 100)
   (set-strategic-number sn-cap-civilian-explorers 100)
   (set-strategic-number sn-total-number-explorers 101)
   (set-strategic-number sn-number-explore-groups 101)
   (set-strategic-number sn-minimum-explore-group-size 1)
   (set-strategic-number sn-maximum-explore-group-size 1)
)

;停止搜索
(defrule
   (or(not(sheep-and-forage-too-far))
       (or(resource-found food)
         (building-type-count-total mill > 0)))
   (strategic-number sn-percent-civilian-explorers != 0)
   =>
   (set-strategic-number sn-percent-civilian-explorers 0)
   (set-strategic-number sn-minimum-civilian-explorers 0)
   (set-strategic-number sn-cap-civilian-explorers 0)
   (set-strategic-number sn-total-number-explorers 1)
   (set-strategic-number sn-number-explore-groups 1)
)

;建造磨坊
(defrule
   (or(building-type-count-total house > 0)
       (civ-selected hun))
   (building-type-count-total mill < 1)
   (or(resource-found food)
       (and(cc-players-unit-type-count 0 59 == 0) ;没有草料从
            (not(sheep-and-forage-too-far))))      ;找到羊
   (can-build mill)
   =>
   (build mill)
)

用这个可以有效地在初期找到食物



旧月 发表于 2010-8-27 17:57:39

打猎磨坊

(defrule
   (or(unit-type-count 122 > 0);男猎人
      (unit-type-count 216 > 0));女猎人
   (building-type-count-total mill < 2)
   (can-build mill)
   =>
   (build mill)
)

旧月 发表于 2010-8-27 18:00:01

建造第一个伐木场(优化)


(defrule
   (resource-found wood)
   (strategic-number sn-camp-max-distance == 8)
   (game-time < 4)
   =>
   (set-strategic-number sn-camp-max-distance 11)
   (disable-self)
)
(defrule
   (resource-found wood)
   (strategic-number sn-camp-max-distance == 11)
   (game-time < 7)
   =>
   (set-strategic-number sn-camp-max-distance 13)
   (disable-self)
)
(defrule
   (resource-found wood)
   (strategic-number sn-camp-max-distance == 13)
   (game-time < 12)
   =>
   (set-strategic-number sn-camp-max-distance 15)
   (disable-self)
)
(defrule
   (resource-found wood)
   (strategic-number sn-camp-max-distance == 15)
   (game-time < 16)
   =>
   (set-strategic-number sn-camp-max-distance 17)
   (disable-self)
)
(defrule
   (resource-found wood)
   (strategic-number sn-camp-max-distance == 17)
   (game-time < 25)
   =>
   (set-strategic-number sn-camp-max-distance 20)
   (disable-self)
)
(defrule
   (resource-found wood)
   (strategic-number sn-camp-max-distance == 20)
   (game-time > 25)
   =>
   (set-strategic-number sn-camp-max-distance 22)
   (disable-self)
)

(defrule
   (current-age == dark-age)
   (building-type-count-total mill > 0)
   (resource-found wood)
   (building-type-count-total lumber-camp < 1)
   (can-build lumber-camp)
   =>
   (build lumber-camp)
)

原理是通过逐步扩大距离来争取建造最近的伐木场。

旧月 发表于 2010-8-27 18:28:39

修正伐木场和采矿场

(defrule
(resource-found wood)
(and
   (dropsite-min-distance wood &gt; 5)
   (building-type-count lumber-camp == 1)
)
)
(dropsite-min-distance wood &gt; 3)
(building-type-count-total lumber-camp &lt; 4)
(can-build lumber-camp)
=&gt;
(build lumber-camp)
)

用于在第一个伐木场悲剧的情况下,重新建

(defrule
(building-type-count mining-camp &gt; 0)
(or
(and
    (dropsite-min-distance gold &gt; 5)
    (dropsite-min-distance gold != 255)
)
(and
   (and
    (dropsite-min-distance stone &gt; 5)
    (dropsite-min-distance stone != 255)
   )
   (building-type-count mining-camp &gt; 1)
)
)
(can-build mining-camp)
(building-type-count mining-camp &lt; 5)
=&gt;
(set-strategic-number sn-camp-max-distance 40)
(build mining-camp)
)
由于考虑石矿,所以复杂一点。
但是只要懂得用dropsite-min-distance 来判断是否建不好就可以了

旧月 发表于 2010-8-27 19:14:56

在别的地方重建伐木场和采矿场

(defrule
   (or(unit-type-count 219 &gt; 3);死的伐木村民
      (unit-type-count 228 &gt; 3))
   (can-build lumber-camp)
   =&gt;
   (build lumber-camp)
)

(defrule
(resource-found gold)
(or
(unit-type-count-total 229 &gt;= 1);死的采金矿工
(unit-type-count-total 221 &gt;= 1)
)
(can-build mining-camp)
(building-type-count mining-camp &lt; 3)
=&gt;
(set-strategic-number sn-gold-dropsite-distance 3)
(build mining-camp)
)
这个不是很完善,你可以根据需要自行修改,例如修改最大建造距离后建。

旧月 发表于 2010-8-27 19:33:42

GOAL控制兵力的典型应用

(defrule
   (goal g-train-skirm gv-yes)
   =>
   (set-goal g-train-skirm gv-no)
)
(defrule
   (goal g-train-skirm gv-no)
   (or(players-unit-type-count any-enemy archer-line > 0)
   (or(players-unit-type-count any-enemy cavalry-archer-line > 0)
   (or(players-unit-type-count any-enemy longbowman-line > 0)
   (or(players-unit-type-count any-enemy war-wagon-line > 0)
   (or(players-unit-type-count any-enemy chu-ko-nu-line > 0)
   (or(players-unit-type-count any-enemy plumed-archer-line > 0)
      (players-unit-type-count any-enemy mangudai-line > 0)))))))
   =>
   (set-goal g-train-skirm gv-yes)
)



(defrule
   (goal g-train-spear gv-yes)
   =>
   (set-goal g-train-spear gv-no)
)
(defrule
   (goal g-train-spear gv-no)
   (or(players-unit-type-count any-enemy knight-line > 0)
   (or(players-unit-type-count any-enemy camel-line > 0)
   (or(players-unit-type-count any-enemy war-elephant-line > 0)
   (or(players-unit-type-count any-enemy cataphract-line > 0)
   (or(players-unit-type-count any-enemy tarkan-line > 0)
   (or(players-unit-type-count any-enemy conquistador-line > 0)
      (players-unit-type-count any-enemy war-wagon-line > 0)))))))
   =>
   (set-goal g-train-spear gv-yes)
)


使用前还要定义常数,但是看懂意思就可以了。
这样控制出兵方便多了。
有人可能会有疑问
(defrule
   (goal g-train-spear gv-yes)
   =>
   (set-goal g-train-spear gv-no)
)
这个不是一直设置为否吗?
AI程序是这样执行的:一次又一次自上而下执行代码无数次(当然有disable-self的就只做一次啦)
所以每一轮开始,都是否,然后后面设置成是了,明白没?

旧月 发表于 2010-8-27 19:37:38

生产投石机

(unit-type-count trebuchet 运算符 数值) 这个条件太笨了,不会检测展开的投石机。
我们在计算投石机数量时候要记入展开的投石机(ID是42)
例子中,投石机总和是5

(defrule
   (or(and(unit-type-count-total trebuchet < 5)
          (unit-type-count 42 == 0))
   (or(and(unit-type-count-total trebuchet < 4)
          (unit-type-count 42 == 1))
      (and(unit-type-count-total trebuchet < 3)
          (unit-type-count 42 == 2))))
   (can-train trebuchet)
   =>
   (train trebuchet)
)
(defrule
   (or(and(unit-type-count-total trebuchet < 2)
          (unit-type-count 42 == 3))
      (and(unit-type-count-total trebuchet < 1)
          (unit-type-count 42 == 4)))
   (can-train trebuchet)
   =>
   (train trebuchet)
)

旧月 发表于 2010-8-27 19:39:21

侦察兵力

(defrule
      (timer-triggered 9)
=&gt;
      (disable-timer 9)
      (enable-timer 9 120)
      (set-strategic-number sn-percent-attack-soldiers 1)
      (attack-now)
)

这是最基础的,你可以加上别的条件限定。
例如,没有检测到敌军的是什么兵种才这样做。

旧月 发表于 2010-8-27 20:51:45

大家有什么补充也可以贴上来


判断对手封快
(defrule
(current-age == dark-age)
(or
(and
   (unit-type-count villager &gt;= 26)
   (players-civilian-population every-enemy &lt; 25)
(and
   (unit-type-count villager &gt;= 27)
   (players-civilian-population every-enemy &lt; 26)
)
)
=&gt;
(xxxxxxxxxxxxxxx)
(chat-local-to-self &quot;对方封快&quot;)
)
(defrule
(current-age == dark-age)
(or
(and
   (unit-type-count villager &gt;= 28)
   (players-civilian-population every-enemy &lt; 27)
)
(and
   (unit-type-count villager &gt;= 29)
   (players-civilian-population every-enemy &lt; 28)
)
)
=&gt;
(xxxxxxxxxxxxxxx)
(chat-local-to-self &quot;对方封快&quot;)
)

(xxxxxxxxxxxxxxx)代表别的动作

旧月 发表于 2010-8-28 08:45:01

后期采矿场的建造


(defrule
(dropsite-min-distance gold > 6);后期大一些
(dropsite-min-distance gold < 255); 地图上还有金子
(game-time > 2400); 40分钟后
(unit-type-count-total 229 == 0); 没采金矿工死亡
(unit-type-count-total 221 == 0)
(can-build mining-camp)
(building-type-count mining-camp >= 5);这里根据需要设置
(building-type-count mining-camp < 8)
=>
(build mining-camp)
(set-strategic-number sn-camp-max-distance 65)
)

只是举个如何限制的例子而已,比较着重的是检测有无金矿工人死亡,也可以加入计时器或者(resourse-found gold)来进一步限制

旧月 发表于 2010-8-28 09:08:02

需要时才扩大城镇范围

(defconst increase-town-size-goal 1)
(defrule
(timer-triggered 1)
(strategic-number sn-maximum-town-size == 10)
(not (goal increase-town-size-goal 0))
=>
(disable-timer 1)
(enable-timer 1 3)
(set-strategic-number sn-maximum-town-size 12)
)
(defrule
(timer-triggered 1)
(strategic-number sn-maximum-town-size == 12)
(not (goal increase-town-size-goal 0))
=>
(disable-timer 1)
(enable-timer 1 3)
(set-strategic-number sn-maximum-town-size 14)
)
(defrule
(timer-triggered 1)
(strategic-number sn-maximum-town-size == 14)
(not (goal increase-town-size-goal 0))
=>
(disable-timer 1)
(enable-timer 1 3)
(set-strategic-number sn-maximum-town-size 16)
)
(defrule
(timer-triggered 1)
(strategic-number sn-maximum-town-size == 16)
(not (goal increase-town-size-goal 0))
=>
(disable-timer 1)
(enable-timer 1 3)
(set-strategic-number sn-maximum-town-size 18)
)
(defrule
(timer-triggered 1)
(strategic-number sn-maximum-town-size == 18)
(not (goal increase-town-size-goal 0))
=>
(disable-timer 1)
(enable-timer 1 3)
(set-strategic-number sn-maximum-town-size 20)
)
(defrule
(timer-triggered 1)
(strategic-number sn-maximum-town-size == 20)
(not (goal increase-town-size-goal 0))
=>
(disable-timer 1)
(enable-timer 1 3)
(set-strategic-number sn-maximum-town-size 22)
)
(defrule
(timer-triggered 1)
(strategic-number sn-maximum-town-size == 22)
(not (goal increase-town-size-goal 0))
=>
(disable-timer 1)
(enable-timer 1 3)
(set-strategic-number sn-maximum-town-size 24)
)
(defrule
(timer-triggered 1)
(strategic-number sn-maximum-town-size == 24)
(not (goal increase-town-size-goal 0))
=>
(disable-timer 1)
(enable-timer 1 3)
(set-strategic-number sn-maximum-town-size 26)
)
(defrule
(timer-triggered 1)
(strategic-number sn-maximum-town-size == 26)
(not (goal increase-town-size-goal 0))
=>
(disable-timer 1)
(enable-timer 1 3)
(set-strategic-number sn-maximum-town-size 28)
)
(defrule
(timer-triggered 1)
(strategic-number sn-maximum-town-size == 28)
(not (goal increase-town-size-goal 0))
=>
(disable-timer 1)
(enable-timer 1 3)
(set-strategic-number sn-maximum-town-size 30)
)
(defrule
(timer-triggered 1)
(strategic-number sn-maximum-town-size == 30)
(not (goal increase-town-size-goal 0))
=>
(disable-timer 1)
(enable-timer 1 3)
(set-strategic-number sn-maximum-town-size 32)
)
(defrule
(timer-triggered 1)
(strategic-number sn-maximum-town-size == 32)
(not (goal increase-town-size-goal 0))
=>
(disable-timer 1)
(enable-timer 1 3)
(set-strategic-number sn-maximum-town-size 34)
)
(defrule
(timer-triggered 1)
(strategic-number sn-maximum-town-size == 34)
(not (goal increase-town-size-goal 0))
=>
(disable-timer 1)
(enable-timer 1 3)
(set-strategic-number sn-maximum-town-size 39)
)
应用:
(defrule
(current-age >= castle-age)
(can-afford-building university)
      (别的限制条件)
(building-type-count-total university == 0)
(goal increase-town-size-goal 0)
=>
(set-goal increase-town-size-goal university)
;这里是把GOAL值设定为大学的单位ID,用英文更方便。
;无论increase-town-size-goal值是什么,只要不是0(也就是要建造建筑的时候)就会扩大城镇范围。
(enable-timer increase-town-size-timer 3)
)
(defrule
      (别的限制条件)
(building-type-count-total university < 1)
(can-build university)
=>
(build university)
)

旧月 发表于 2010-8-28 23:11:29

先占一楼。。。。。。。。。。。。。。。。。
PS LS的我可能要编辑掉

wztianfeng 发表于 2010-9-9 18:36:06

呵呵,,看来做AI的技巧多了好多啊...{39}   又能学习了....
页: [1]
查看完整版本: AI的一些实用小技巧 附实例 对做AI不小帮助