找回密码
 点一下
查看: 7326|回复: 13

AI

[复制链接]
发表于 2009-11-13 16:38:51 | 显示全部楼层 |阅读模式
[jass]
native DebugS               takes string str                            returns nothing
native DebugFI              takes string str, integer val               returns nothing
native DebugUnitID          takes string str, integer val               returns nothing
native DisplayText          takes integer p, string str                 returns nothing
native DisplayTextI         takes integer p, string str, integer val    returns nothing
native DisplayTextII        takes integer p, string str, integer v1, integer v2 returns nothing
native DisplayTextIII       takes integer p, string str, integer v1, integer v2, integer v3 returns nothing
native DoAiScriptDebug      takes nothing                               returns boolean
native GetAiPlayer          takes nothing                               returns integer
native GetHeroId            takes nothing                               returns integer
native GetHeroLevelAI       takes nothing                               returns integer
native GetUnitCount         takes integer unitid                        returns integer
native GetPlayerUnitTypeCount takes player p, integer unitid            returns integer
native GetUnitCountDone     takes integer unitid                        returns integer
native GetTownUnitCount     takes integer id, integer tn, boolean dn    returns integer
native GetUnitGoldCost      takes integer unitid                        returns integer
native GetUnitWoodCost      takes integer unitid                        returns integer
native GetUnitBuildTime     takes integer unitid                        returns integer
native GetMinesOwned        takes nothing                               returns integer
native GetGoldOwned         takes nothing                               returns integer
native TownWithMine         takes nothing                               returns integer
native TownHasMine          takes integer townid                        returns boolean
native TownHasHall          takes integer townid                        returns boolean
native GetUpgradeLevel      takes integer id                            returns integer
native GetUpgradeGoldCost   takes integer id                            returns integer
native GetUpgradeWoodCost   takes integer id                            returns integer
native GetNextExpansion     takes nothing                               returns integer
native GetMegaTarget        takes nothing                               returns unit
native GetBuilding          takes player p                              returns unit
native GetEnemyPower        takes nothing                               returns integer
native SetAllianceTarget    takes unit id                               returns nothing
native GetAllianceTarget    takes nothing                               returns unit
native SetProduce           takes integer qty, integer id, integer town returns boolean
native Unsummon             takes unit unitid                           returns nothing
native SetExpansion         takes unit peon, integer id                 returns boolean
native SetUpgrade           takes integer id                            returns boolean
native SetHeroLevels        takes code func                             returns nothing
native SetNewHeroes         takes boolean state                         returns nothing
native PurchaseZeppelin     takes nothing                               returns nothing
native MergeUnits           takes integer qty, integer a, integer b, integer make returns boolean
native ConvertUnits         takes integer qty, integer id               returns boolean
native SetCampaignAI        takes nothing                               returns nothing
native SetMeleeAI           takes nothing                               returns nothing
native SetTargetHeroes      takes boolean state                         returns nothing
native SetPeonsRepair       takes boolean state                         returns nothing
native SetRandomPaths       takes boolean state                         returns nothing
native SetDefendPlayer      takes boolean state                         returns nothing
native SetHeroesFlee        takes boolean state                         returns nothing
native SetHeroesBuyItems    takes boolean state                         returns nothing
native SetWatchMegaTargets  takes boolean state                         returns nothing
native SetIgnoreInjured     takes boolean state                         returns nothing
native SetHeroesTakeItems   takes boolean state                         returns nothing
native SetUnitsFlee         takes boolean state                         returns nothing
native SetGroupsFlee        takes boolean state                         returns nothing
native SetSlowChopping      takes boolean state                         returns nothing
native SetCaptainChanges    takes boolean allow                         returns nothing
native SetSmartArtillery    takes boolean state                         returns nothing
native SetReplacementCount  takes integer qty                           returns nothing
native GroupTimedLife       takes boolean allow                         returns nothing
native RemoveInjuries       takes nothing                               returns nothing
native RemoveSiege          takes nothing                               returns nothing
native InitAssault          takes nothing                               returns nothing
native AddAssault           takes integer qty, integer id               returns boolean
native AddDefenders         takes integer qty, integer id               returns boolean
native GetCreepCamp         takes integer min, integer max, boolean flyers_ok returns unit
native StartGetEnemyBase    takes nothing                               returns nothing
native WaitGetEnemyBase     takes nothing                               returns boolean
native GetEnemyBase         takes nothing                               returns unit
native GetExpansionFoe      takes nothing                               returns unit
native GetEnemyExpansion    takes nothing                               returns unit
native GetExpansionX        takes nothing                               returns integer
native GetExpansionY        takes nothing                               returns integer
native SetStagePoint        takes real x, real y                        returns nothing
native AttackMoveKill       takes unit target                           returns nothing
native AttackMoveXY         takes integer x, integer y                  returns nothing
native LoadZepWave          takes integer x, integer y                  returns nothing
native SuicidePlayer        takes player id, boolean check_full         returns boolean
native SuicidePlayerUnits   takes player id, boolean check_full         returns boolean
native CaptainInCombat      takes boolean attack_captain                returns boolean
native IsTowered            takes unit target                           returns boolean
native ClearHarvestAI       takes nothing                               returns nothing
native HarvestGold          takes integer town, integer peons           returns nothing
native HarvestWood          takes integer town, integer peons           returns nothing
native GetExpansionPeon     takes nothing                               returns unit
native StopGathering        takes nothing                               returns nothing
native AddGuardPost         takes integer id, real x, real y            returns nothing
native FillGuardPosts       takes nothing                               returns nothing
native ReturnGuardPosts     takes nothing                               returns nothing
native CreateCaptains       takes nothing                               returns nothing
native SetCaptainHome       takes integer which, real x, real y         returns nothing
native ResetCaptainLocs     takes nothing                               returns nothing
native ShiftTownSpot        takes real x, real y                        returns nothing
native TeleportCaptain      takes real x, real y                        returns nothing
native ClearCaptainTargets  takes nothing                               returns nothing
native CaptainAttack        takes real x, real y                        returns nothing
native CaptainVsUnits       takes player id                             returns nothing
native CaptainVsPlayer      takes player id                             returns nothing
native CaptainGoHome        takes nothing                               returns nothing
native CaptainIsHome        takes nothing                               returns boolean
native CaptainIsFull        takes nothing                               returns boolean
native CaptainIsEmpty       takes nothing                               returns boolean
native CaptainGroupSize     takes nothing                               returns integer
native CaptainReadiness     takes nothing                               returns integer
native CaptainRetreating    takes nothing                               returns boolean
native CaptainReadinessHP   takes nothing                               returns integer
native CaptainReadinessMa   takes nothing                               returns integer
native CaptainAtGoal        takes nothing                               returns boolean
native CreepsOnMap          takes nothing                               returns boolean
native SuicideUnit          takes integer count, integer unitid         returns nothing
native SuicideUnitEx        takes integer ct, integer uid, integer pid  returns nothing
native StartThread          takes code func                             returns nothing
native Sleep                takes real seconds                          returns nothing
native UnitAlive            takes unit id                               returns boolean
native UnitInvis            takes unit id                               returns boolean
native IgnoredUnits         takes integer unitid                        returns integer
native TownThreatened       takes nothing                               returns boolean
native DisablePathing       takes nothing                               returns nothing
native SetAmphibious        takes nothing                               returns nothing
native CommandsWaiting      takes nothing                               returns integer
native GetLastCommand       takes nothing                               returns integer
native GetLastData          takes nothing                               returns integer
native PopLastCommand       takes nothing                               returns nothing
native MeleeDifficulty      takes nothing                               returns integer

[/jass]


这个是很郁闷猜测的功能

native DebugS            带 入 S , 除 错 S

native DebugFI           带 入 S 和 数 字 X , 除 错 第 X 个 字 符 串

native DebugUnitID    同 上 。

↓区 别 不 知 道

native DisplayText      播 放 字 符

native DisplayTextI     播 放 字 符

native DisplayTextII    播 放 字 符

native DisplayTextIII   播 放 字 符



native DoAiScriptDebug  AI 是 否 有 错 , 返 回 对 应 的 。

native GetAiPlayer           获 得 正在 执 行 AI 的 的 玩 家 数 字 , 玩 家 1 对 应 0 。

native GetHeroId            返 回 单 位 ID , 和 GetUnitId 同 ( 那 为 何 弄 两 个 ?我 不 知 道 )

native GetHeroLevelAI    不 清 楚 , 是 返 回 等 级 就 是 了 ,被 AI 操 作 的 英 雄 ?

native GetUnitCount      带 入 单 位 ID , 获 得 一 类 单 位 总 数 。

native GetPlayerUnitTypeCount  同 上 多 个 玩 家 限 制 。带 入 多 一 个 玩 家 数 字 。

native GetUnitCountDone           不 知 道 。 返 回 是 玩 家 单 位 数 量 。 ( 被 操 纵 的 单 位 ?)

native GetTownUnitCount           不 知 道 。 城 镇 单 位 ?? 守 城 镇 ? 城 镇 一 定 范 围 的 单 位 ? 路 径 半 径 内 ?

native GetUnitGoldCost               带 入 单 位 ID , 返 回 购 买 此 单 位 所 需 要 的 金 币 。

native GetUnitWoodCost            同 上 , 木 材 。

native GetUnitBuildTime              建 造 ( 训 练 ) 此 单 位 所 需 要 的 时 间 。

native GetMinesOwned               返 回 矿 山 拥 有 者 。 ( 大 概 返 回 中 立 被 动 了 )

native GetGoldOwned                金 币 还 有 拥 有 者 的 ? 暂 不 明 白 。

native TownWithMine                城 镇 拥 有 的 金 矿 数 。

native TownHasMine                  带 入 城 镇 ID , 城 镇 是 否 拥 有 金 矿 。

native TownHasHall                    带 入 城 镇 ID , 城 镇 是 否 拥 有 Hall 。 ( 大 厅 ,出 农 民 的 那 个 ?)

native GetUpgradeLevel             获 得 升 级 的 等 级 。

native GetUpgradeGoldCost       带 入 升 级 技 能 ID , 返 回 升 级 所 需 要 的 金 币 。

native GetUpgradeWoodCost    同 上 , 木 头 。

native GetNextExpansion          不 知 道 。

native GetMegaTarget                什 么 叫 大 单 位 ? 返 回 单 位 。

native GetBuilding                      带 入 玩 家 数 字 , 返 回 正 在 建 造 的 单 位 。( 若 是 多 个 怎 么 办 ? )

native GetEnemyPower             返 回 主 要 敌 对 玩 家 ? 不 知 道 。

native SetAllianceTarget             带 入 单 位 数 据 , 设 置 与 单 位 联 合 。(竟 然 只 同 盟 一 个 单 位 ,真 神 奇 。)

native GetAllianceTarget            返 回 被 同 盟 的 单 位 。

native SetProduce                    带 入 产 数 量 ,生 产 单 位 ID ,城 镇 ? 让 某 单 位 生 产 ( 产 孩 子 ? Muhaha )

native Unsummon                    带 入 单 位 类 型 , 使 单 位 为 非 召 唤 单 位 。 不 知 道 对 否 。

native SetExpansion                 带 入 农 民 单 位 , 数 字 。 不 知 道 撒 。

native SetUpgrade                    带 入 升 级 科 技 ID , 设 置 升 级 。

native SetHeroLevels                 带 入 一 个 func , 设 置 英 雄 单 位 等 级 。

native SetNewHeroes                带 入 布 尔 , 是 否 设 置 新 英 雄 。

native PurchaseZeppelin            购 买 飞 艇 。啥 意 识 ?  偶 也 不 明 白 。

native MergeUnits                     带 入 数 量 , 3 数 字 , 估 计 是 编 组 。 还 返 回 布 尔 (是 否 合 并 成 功 ?)

native ConvertUnits                  不 清 楚 。



native SetCampaignAI               设 置 战 役 AI ( 至 于 战 役 AI 导 入 路 径 偶 就 不 知 道 了 。)

native SetMeleeAI                      设 置 混 战 AI ( 同 上 。 )

↑ 这 个 ……………………

native SetTargetHeroes             带 入 布 尔 , 对 应 设 置 英 雄 目 标 。( 为 主 要 目 标 ?)

native SetPeonsRepair               带 入 布 尔 , 对 应 设 置 主 动 修 理 ?

native SetRandomPaths            带 入 布 尔 , 对 应 设 置 随 机 路 线 ?

native SetDefendPlayer             带 入 布 尔 , 对 应 设 置 帮 助 玩 家 防 御 ?

native SetHeroesFlee                 带 入 布 尔 , 对 应 设 置 英 雄 逃 跑 ?

native SetWatchMegaTargets    带 入 布 尔 , 对 应 设 置 监 视 大 单 位 ( 主 要 的)。

native SetIgnoreInjured            带 入 布 尔 , 对 应 设 置 忽 视 伤 害 。(只 攻 不 守 ?不 回 家 回 血 ?)

native SetHeroesTakeItems       带 入 布 尔 , 对 应 设 置 英 雄 捡 物 品 。 (捡 个 老 虎 回 家 算 了 。。)

native SetUnitsFlee                    同native SetHeroesFlee 所 有 单 位 。

native SetGroupsFlee                同 , 集 体 逃 亡 ? - -

native SetSlowChopping           慢 采 矿 ? 不 是 很 清 楚 。

native SetCaptainChanges        带 入 布 尔 , 对 应 设 置 允 许 改 变 队 长 (带 头 大 哥 → 英 雄 ?)

native SetSmartArtillery            带 入 布 尔 , 对 应 设 置 炮 兵 巧 妙 攻 击 。 (太 近 会 走 开 点 去 攻 击 ?)

native SetReplacementCount   带 入 数 字 , 替 代 原 来 数 字 。(编 组 么 ?)

native GroupTimedLife              带 入 布 尔 , 对 应 设 置 集 体 时 间 生 命 。(太 难 了 。)

native RemoveInjuries              移 除 伤 害 。(没 伤 害 了 ? 起 不 是 无 敌 了 ?貌 似 不 是 那 样 的 。)

native RemoveSiege                 去 除 围 攻 ?

native InitAssault                      启 动 攻 击 ?

native AddAssault                     带 入 单 位 数 量 和 类 型 , 增 加 攻 击 单 位 数 量 。 返 回 布 尔 。

native AddDefenders                增 加 单 位 数 量 和 类 型 , 增 加 防 御 单 位 数 量 。 返 回 布 尔 。

native GetCreepCamp              带 入 X X X X ,最 小 最 大 数 2  , 是 否 飞 行 。  返 回 单 位 。( 中 立 营 地 )

native StartGetEnemyBase       开 始 获 得 敌 人 基 地 ?

native WaitGetEnemyBase        等 待 获 得 敌 人 最 基 本 单 位 ?返 回 布 尔 。

native GetEnemyBase               返 回 敌 人 基 本 单 位 ?

native GetExpansionFoe           返 回 除 住 基 地 外 敌 人 单 位 。 (不 知 道 对 不 对 )

native GetEnemyExpansion      同 上 。

native GetExpansionY              返 回 扩 大 势 力 Y 坐 标 。 (第 二 基 地)

native GetExpansionY              返 回 扩 大 势 力 X 坐 标 。

native SetStagePoint               带 入 X 、Y ,设 置 舞 台 坐 标?

native AttackMoveKill               带 入 单 位 ,攻 击 移 动 直 到 杀 死 此 单 位 。

native AttackMoveXY              带 入 X ,Y , 攻击 移 动 到 X Y 。

native LoadZepWave              带 入 X ,Y ,飞 艇 登 陆 示 意 坐 标 。 (图 上 的 感 叹 圈 圈 号 ?)

native SuicidePlayer                 带 入 玩 家 ID 和 布 尔 , 玩 家 自 杀 。返 回 布 尔 。

native SuicidePlayerUnits         同 上 单 位 自 杀 。

native CaptainInCombat         带 入 布 尔 , 对 应 设 置 攻 击 队 长 (英 雄 ?)

native IsTowered                    带 入 单 位 , 判 断 在 城 镇 内 。返 回 布 尔 。

native ClearHarvestAI             清 除 造 农 民 AI ?

native HarvestGold                 带 入 城 镇 (范 围 ?),农 民 数 量 , 设 置 此 基 地 采 黄 金 农 民 数 量 。

native HarvestWood              同 上 木 头 。

native GetExpansionPeon       获 得 非 主 基 地 农 民 。(不 知 道 )

native StopGathering             停 止 聚 集 。

native AddGuardPost             带 入 单 位 ID , 站 岗 点 X Y ,添 加 卫 士 站 岗 。

native FillGuardPosts              同 上

native ReturnGuardPosts      返 回

native CreateCaptains           建 立 队 长

native SetCaptainHome        设 置 家 。 特 殊 单 位 ID X Y 。

native ResetCaptainLocs      重 置 队 长 坐 标 。

native ShiftTownSpot           带 入 坐 标 , 转 移 城 镇 点

native TeleportCaptain         带 入 坐 标 , 传 送 队 长 点

native ClearCaptainTargets  清 除 队 长 目 标

native CaptainAttack           设 置 主 攻 点 。

native CaptainVsUnits          不 清 楚 。

native CaptainVsPlayer        同 上 。

native CaptainGoHome        队 长 回 家 。

native CaptainIsHome          队 长 在 家 。 返 回 布 尔 。

native CaptainIsFull              队 长 满 ?  返 回 布 尔

native CaptainIsEmpty         队 长 空 ?  返 回 布 尔

native CaptainGroupSize      队 长 组 大 小 。返 回 整 数 。

native CaptainReadiness      队 长 准 备 就 绪 。 同 上 。

native CaptainRetreating     队 长 撤 退 。 返 回 布 尔 。

native CaptainReadinessHP  队 长 准 备 就 绪 血 值 。返 回 整 数 。

native CaptainReadinessMa  魔 法 值 。

native CaptainAtGoal           队 长 在 目 的 地 , 返 回 布 尔 。

native CreepsOnMap           中 立 在 地 图 上 ?

native SuicideUnit               自 杀 单 位 数 量 和 类 型

native SuicideUnitEx           不 知 道 。

native StartThread             带 入 FUNC ,开 始 线 路 。

native Sleep                       带 入 实 数 , AI 作 用 器 停 止 X 秒

native UnitAlive                  带 入 单 位 ID , 单 位 是 否 活 者 。 返 回 布 尔 。

native UnitInvis                  未 知 。

native IgnoredUnits           忽 视 单 位 ,返 回 忽 视 总 数 ?

native TownThreatened      城 镇 被 攻 击 ?返 回 布 尔

native DisablePathing          路 线 不 可 用 。

native SetAmphibious         设 置 为 两 栖 ?

native CommandsWaiting   命 令 滞 后 时 间 。 返 回 整 数 。

native GetLastCommand    获 得 最 后 的 命 令 。 返 回 整 数 。(咋 不 是 S 呢 )

native GetLastData             获 得 最 后 的 数 据 。 返 回 整 数 。

native PopLastCommand   未 知 。

native MeleeDifficulty          返 回 难 度 数 值 。

native GetMinesOwned               返 回 矿 山 拥 有 者 。 ( 大 概 返 回 中 立 被 动 了 )

native GetGoldOwned                金 币 还 有 拥 有 者 的 ? 暂 不 明 白 。


金矿如果有一个玩家正在开采,另一个玩家就无法开采的..

JASS与C语言很类似,所以在阅读以下内容之前,首先应该对TRIGGER JASS有着一定的了解。
了解JASS之后,你将很快的了解AI JASS。
AI JASS的概念
最主要的是理解AI线程(Threads)的概念。
线程是什么?
线程就象TRIGGER的一个循环判断语句,线程会不停的判断在线程程序列表内所缺少的元素,并去按照线程的指令排放顺序去完成它。
就象碗中的饭,有即吃,有即吃,有即吃,Loop......
为什么不用TRIGGER去完成电脑的人工智能呢?
由于使用TRIGGER相比之下可能会消耗大量的内存,所以,最好用AI线程来完成电脑的人工智能。
注意: 线程只针对单个玩家运作。

AI的类型(Melee AI 和 Campaign AI )
Melee AI 对战AI基本上完全可以利用WORLDEDITOR自带的AI编辑器(AI Edior)完成
Campaign AI 战役AI(即“非对战AI”),可以详细了解以下内容即可制作一般常用的战役AI
注:战役AI的用途十分广泛,完全可以利用其制作生存,3C,TD等类型的地图,并且免除了不停使用TRIGGER创造单位所带来的内存泄露。



线程(Threads)和触发器(Trigger)的区别AI脚本只能使用JASS函数库common.j和common.ai中的函数和量Trigger脚本只能使用common.j和Blizzard.j中的函数和量线程只应用于AI脚本(AI JASS), 不能用于触发器脚本(Trigger Jass)触发器只应用用于触发器脚本(Trigger Jass),不能用于AI脚本(AI JASS)
通常, 当AI脚本开始运行时只创建一个线程, 创建更多的线程可以用comman.j的本地函数:
native StartThread takes code func returns nothing
运行方式的区别在于线程一旦开启即可自动运转,触发器必须调用或者借助事件的发生才可以运行。 调用 call StartThread(function myfunc) 将创建一个从函数myfunc开始执行的线程.
调用 call SleepForever() 使线程永久性的关闭 *每个玩家最多可以拥有6个线程(包括一开始执行的主线程在内,当一个玩家有6个线程数时, 调用StartThread()的语句将被忽略. ) *线程不能回收, 当你为某玩家创建了5个自定义线程, 将无法为该玩家创建更多的线程. *局域性的全局状态,在同一玩家中的所有线程都共享全局状态(包括变量). 即是修改某个全局变量, 修改后的值在此玩家的所有线程中都是可见的. *当新线程创建时, 线程立即生效. *当线程让步执行时, 创建此线程的父线程将继续执行.

线程在以下的情况让步执行, 返回父线程
a) 当线程中的操作码(opcode)超出限制, 线程会自动休眠 1 秒
当线程中用使用 Sleep(n), 线程将休眠 n 秒, 然后继续执行.

线程在以下情况会中止, 返回父线程
a) 如果 call StartThread(null)中, 线程中止
当线程的主函数返回, 线程中止.
(StartThread()中之间调用的函数就是主函数.)
c) 当线程中使用没有声明的变量, 线程中止. 在使用之前, 变量必须声明.
d) 当线程中出现零为被除数时, 线程中止
e) 线程主函数出现语法错误.
注意: 虽然AI脚本可以使用大部分common.j的库函数, 但有些类型的函数在AI不能正常工作, 如:
a) 返回字符串类型(string)的本地函数, 如I2S(), SubString()等
需要以code, trigger, boolexpr 等类型数据为参数的本地函数, 如触发器函数, 队列函数(ForGroup, 等)

注意: AI中不可以使用Blizzard.j的函数, 触发器中也不可以使用common.ai的函数, AI和触发器都可以使用common.j的函数(当然, 对于AI, 还受上面所说的限制)

common.ai和common.j是写AI时可以调用和参考库文件, 要研究AI, 先去读这2个文件.******************************************************************

3) 跨脚本通讯(Inter-Script Communication)
在游戏中, 可能会有多个独立的Jass脚本文件同时运行. 比如在对战地图中的游戏, 运行触发器脚本文件的同时, 也可能运行了每个电脑玩家的AI脚本文件. 每个脚本文件之间的全局变量不是共享的. 所以, 一个电脑玩家的AI脚本中设置的全局变量不会影响另一个电脑玩家的AI脚本的执行.

触发器脚本也不可以和AI脚本共享全局变量. 但可以用传递命令的方法进行脚本之间的数据交换. 命令由一对数值型数据(integer)组成: 命令值(command value)和数据值(data value).

从触发器脚本向AI脚本发出通讯命令, 可以使用common.j中定义的以下本地函数:
native CommandAI takes player num,
integer command, integer data returns nothing

参数:
player num //玩家
integer command //命令
integer data //命令数据




常数:
//--------------------------------------------------------------------
constant integer M1 = 60 实际上是时间!
constant integer M2 = 2*60
constant integer M3 = 3*60
constant integer M4 = 4*60
constant integer M5 = 5*60
constant integer M6 = 6*60
constant integer M7 = 7*60
constant integer M8 = 8*60
constant integer M9 = 9*60
constant integer M10 = 10*60
constant integer M11 = 11*60
constant integer M12 = 12*60
constant integer M13 = 13*60
constant integer M14 = 14*60
constant integer M15 = 15*60

constant integer EASY = 1
constant integer NORMAL = 2
constant integer HARD = 3
constant integer INSANE = 4 // not used

constant integer MELEE_NEWBIE = 1
constant integer MELEE_NORMAL = 2
constant integer MELEE_INSANE = 3

constant integer ATTACK_CAPTAIN = 1
constant integer DEFENSE_CAPTAIN = 2
constant integer BOTH_CAPTAINS = 3

constant integer BUILD_UNIT = 1
constant integer BUILD_UPGRADE = 2
constant integer BUILD_EXPAND = 3

constant integer UPKEEP_TIER1 = 50
constant integer UPKEEP_TIER2 = 80




JASS函数库common.ai的全局变量(可以更改,也可以自行增加,以下内容会经常在AI脚本中使用):
player ai_player //AI玩家 integer sleep_seconds
//等待时间
integer total_gold = 0 //总计的金钱 integer total_wood = 0
//总计的木材
integer gold_buffer = 0
// usually for potion money
integer difficulty = NORMAL
//难度,默认为中等难度
integer exp_seen = 0
integer racial_farm = 'hhou'
integer hero_id = 'Hamg'
//第一个英雄的ID,默认为圣骑士
integer hero_id2 = 'Hmkg'
//第二个英雄的ID,默认为山丘之王
integer hero_id3 = 'Hpal'
//第三个英雄的ID,默认为大法
integer array skill
integer array skills1
integer array skills2
integer array skills3
integer max_hero_level = 0
integer array harass_qty
integer array harass_max
integer array harass_units
integer harass_length = 0
防守单位的全局变量
例子:防守单位defense_units[defense_length]的数量为defense_qty[defense_length]
integer array defense_qty //设置防守单位的数量
integer array defense_units //设置防守的单位
integer defense_length = 0 //单位Index
建造的全局变量
为了区分建造的种类,BLZ为建造的全局变量设置了4类例子:建造单位build_type[build_length]的数量为build_qty[build_length]
integer array build_qty //建造的数量 设置建造单位的数量
integer array build_type //建造的种类 普通单位类(包括单位和建筑)
integer array build_item //购买物品的种类 物品类
integer array build_town //建造基地的种类 基地类
integer build_length = 0
设置采集工人的数量
integer campaign_gold_peons = 5 //主基地采集金子的人数
integer campaign_wood_peons = 3 //住基地采集木材的人数
integer campaign_basics_speed = 5
integer min_creeps = -1 integer max_creeps = -1
游戏初试设置的全局变量
boolean harvest_town1 = true
boolean harvest_town2 = true
boolean harvest_town3 = true
boolean do_campaign_farms = true
//自动制造战役AI 提供人口的建筑
boolean two_heroes = false
//双英雄
boolean allow_air_creeps = false
//空中的中立单位(假如有,AI会针对性的发展)
boolean take_exp = false //<





一般战役AI常用函数


CampaignAI takes integer farms, code heroes returns nothing
是BLZ在COMMON.AI中设置的一个简便函数,直接初始化战役AI
farms - 提供AI玩家人口的建筑类型
heroes - 指向设置英雄的ID的CODE

SetReplacements takes integer easy, integer med, integer hard returns nothing
尚未研究的函数,一般用法为:
call SetReplacement( 2, 2, 4 )
easy - 游戏难度为简单所赋的值
med - 游戏难度为普通所赋的值
hard - 游戏难度为困难所赋的值

native SetCaptainHome takes integer which, real x, real y returns nothing
设置攻击组队长在家的点?
which - 什么的聚集点,参数有:
constant integer ATTACK_CAPTAIN = 1 进攻的点
constant integer DEFENSE_CAPTAIN = 2 防御的点
constant integer BOTH_CAPTAINS = 3 进攻和防御的点
x - X坐标
Y - Y坐标

SetBuildUnit takes integer qty, integer unitid returns nothing
指令AI玩家制造(所有类型的建造,包括升级)
qty - 数量
unitid - 单位类型

SetBuildUnitEx takes integer easy, integer med, integer hard, integer unitid returns nothing
指令AI玩家制造单位按照游戏难度
easy - 简单时候的数量
med - 普通时候的数量
hard - 困难时候的数量
unitid - 单位类型

CampaignDefender takes integer level, integer qty, integer unitid returns nothing
设置防守单位的数量

CampaignDefenderEx takes integer easy, integer med, integer hard, integer unitid returns nothing
设置防守单位的数量,
按难度 call WaitForSignal() 等待TRIGGER发出AI指令,当堆积的指令数量不等于0时,继续进行AI线程


进攻组的指令
function InitAssaultGroup takes nothing returns nothing
初始化进攻组,设置全局变量harass_length = 0


CampaignAttacker takes integer level, integer qty, integer unitid returns nothing
增加进攻组的进攻单位
qty - 数量
unitid - 单位类型

CampaignAttackerEx takes integer easy, integer med, integer hard, integer unitid returns nothing
增加进攻组的进攻单位,按游戏难度
easy - 简单时候的数量
med - 普通时候的数量
hard - 困难时候的数量
unitid - 单位类型

SuicideOnPoint takes integer seconds, player p, integer x, integer y returns nothing
设置进攻的玩家的点seconds 发起进攻等待的时间,
常量参数有
constant integer M1 = 60
constant integer M2 = 260
constant integer M3 = 360
constant integer M4 = 460
constant integer M5 = 560
constant integer M6 = 660
constant integer M7 = 760
constant integer M8 = 860
constant integer M9 = 960
constant integer M10 = 1060
constant integer M11 = 1160
constant integer M12 = 1260
constant integer M13 = 1360
constant integer M14 = 1460
constant integer M15 = 1560
p - 目标玩家

SuicideOnUnits takes integer seconds, player p returns nothing
设置进攻的目标玩家seconds






一般战役AI的制作方案
参看BLZ在战役中所制作的AI脚本“n08_green.ai”来了解怎么制作AI
//变量 进攻的目标玩家
globals
player user = Player(1)
endglobals function main takes nothing returns nothing //AI初试设置
call CampaignAI(ZIGGURAT_1,null)
call SetReplacements(2,2,4)
call SetCaptainHome(ATTACK_CAPTAIN,4161,-4668 )
call SetCaptainHome(DEFENSE_CAPTAIN,5218,-6441 )
set campaign_wood_peons = 2 //需要建造的建筑以及单位
call SetBuildUnit( 1, NECROPOLIS_1 )
call SetBuildUnit( 1, ACOLYTE )
call SetBuildUnit( 1, UNDEAD_MINE )
call SetBuildUnit( 1, GRAVEYARD )
call SetBuildUnit( 8, ZIGGURAT_1 )
call SetBuildUnit( 2, CRYPT )
call SetBuildUnit( 1, UNDEAD_ALTAR )
call SetBuildUnit( 1, NECROPOLIS_2 )
call SetBuildUnit( 2, SLAUGHTERHOUSE )
call SetBuildUnit( 1, DAMNED_TEMPLE )
call SetBuildUnit( 1, NECROPOLIS_3 )
call SetBuildUnit( 2, BONEYARD )
call SetBuildUnit( 5, ACOLYTE )
call SetBuildUnit( 8, ZIGGURAT_2 ) //防守单位
call CampaignDefenderEx( 1,1,2, ABOMINATION )
call CampaignDefenderEx( 1,1,1, GARGOYLE )
call CampaignDefenderEx( 1,1,1, FROST_WYRM )
call CampaignDefenderEx( 1,1,1, CRYPT_FIEND )
call CampaignDefenderEx( 1,1,1, DREAD_LORD ) //TRIGGER发出指令就开始进行下面的AI进程
call WaitForSignal() //进攻,制造
//WAVE 1
call InitAssaultGroup()
call CampaignAttackerEx( 6,6,8, GARGOYLE )
call SuicideOnPlayerEx(M5,M5,M3,user)
call SetBuildUpgrEx( 1,1,1, UPG_FIEND_WEB )
call SetBuildUpgrEx( 1,1,1, UPG_CANNIBALIZE )
call SetBuildUpgrEx( 1,1,1, UPG_UNHOLY_STR )
call SetBuildUpgrEx( 1,1,1, UPG_CR_ATTACK )
//WAVE 2
call InitAssaultGroup()
call CampaignAttackerEx( 6,6,8, GARGOYLE )
call CampaignAttackerEx( 2,2,4, CRYPT_FIEND )
call CampaignAttackerEx( 1,1,2, FROST_WYRM )
call SuicideOnPlayerEx(M6,M6,M4,user)
//WAVE 3
call InitAssaultGroup()
call CampaignAttackerEx( 6,6,8, GARGOYLE )
call CampaignAttackerEx( 2,2,4, CRYPT_FIEND )
call CampaignAttackerEx( 1,1,2, FROST_WYRM )
call CampaignAttackerEx( 1,1,1, DREAD_LORD )
call SuicideOnPlayerEx(M6,M6,M4,user)
call SetBuildUpgrEx( 2,2,2, UPG_UNHOLY_STR )
call SetBuildUpgrEx( 2,2,2, UPG_CR_ATTACK )
call SetBuildUpgrEx( 1,1,1, UPG_SKEL_LIFE )
call SetBuildUpgrEx( 1,1,1, UPG_WYRM_BREATH )
//WAVE 4
call InitAssaultGroup()
call CampaignAttackerEx( 2,2,3, GARGOYLE )
call CampaignAttackerEx( 5,5,6, CRYPT_FIEND )
call CampaignAttackerEx( 1,1,2,
发表于 2009-11-13 16:44:44 | 显示全部楼层
native DebugFI           带 入 S 和 数 字 X , 除 错 第 X 个 字 符 串

native DisplayText      播 放 字 符

native DisplayTextI     播 放 字 符

native DisplayTextII    播 放 字 符

native DisplayTextIII   播 放 字 符

这些我猜想的效果是类似于printf或者format这样的……
回复

使用道具 举报

 楼主| 发表于 2009-11-13 16:58:14 | 显示全部楼层
都是出自GA的东西
现在GA里却没有了……
后面的部分是蚊子叮的教程……
不过似乎不是完整版的
找不到了
回复

使用道具 举报

发表于 2009-11-13 21:07:40 | 显示全部楼层
没有英语基础真是可怕
回复

使用道具 举报

发表于 2009-12-27 14:35:44 | 显示全部楼层
半年多前脑子发热也曾经学过AI啊。。。
下面的东西是远古年代注释的,当时对we的了解还太浅,可能会有很多错误= =

[jass]
//==================================================================================================
//  $Id: common.ai,v 1.68 2003/05/12 02:34:18 bfitch Exp $
//  common.ai文件附部分函数解释
//**********************************
//                   By -zxcf
//               --2009.04.19
//**********************************
//  仅作参考还有很多东西无法理解
//  所有函数解释都未作测试,有错误的话见谅了
//==================================================================================================
//文本测试类函数
//输出整数时在字符串中写占位符'%d'与之后的整数对应
//显示的文本行末要加换行符'\\n'
//例如:
//call DisplayTextIII(GetAiPlayer(),"String test on %d.%d.%d\\n",2009,4,19)
//显示结果是String test on 2009.4.19
//最重要的,上面都是理论而已,我至今没能正常显示出文本- -
//显示文本
native DebugS               takes string str                            returns nothing
//显示文本带1个数字
native DebugFI              takes string str, integer val               returns nothing
//显示文本带1个单位ID
native DebugUnitID          takes string str, integer val               returns nothing
//按玩家显示文本
native DisplayText          takes integer p, string str                 returns nothing
//按玩家显示文本带1个数字
native DisplayTextI         takes integer p, string str, integer val    returns nothing
//按玩家显示文本带2个数字
native DisplayTextII        takes integer p, string str, integer v1, integer v2 returns nothing
//按玩家显示文本带3个数字
native DisplayTextIII       takes integer p, string str, integer v1, integer v2, integer v3 returns nothing
//AI脚本是否允许显示文本(没试出过返回true)
native DoAiScriptDebug      takes nothing                               returns boolean
//当前脚本控制的AI玩家
native GetAiPlayer          takes nothing                               returns integer
//英雄升级技能函数内获得升级的英雄类型
native GetHeroId            takes nothing                               returns integer
//英雄升级技能函数内获得英雄等级
native GetHeroLevelAI       takes nothing                               returns integer
//获得AI玩家的该类型单位个数,包括建造中单位
native GetUnitCount         takes integer unitid                        returns integer
//获得某玩家的该类型单位个数
native GetPlayerUnitTypeCount takes player p, integer unitid            returns integer
//获得AI玩家的已完成的该类型单位个数
native GetUnitCountDone     takes integer unitid                        returns integer
//获得AI玩家该类型单位在指定基地的个数(dn指是否只获取已完成的单位)
native GetTownUnitCount     takes integer id, integer tn, boolean dn    returns intege
//获取单位类型造价金钱
native GetUnitGoldCost      takes integer unitid                        returns integer
//获取单位类型造价木材
native GetUnitWoodCost      takes integer unitid                        returns integer
//获取单位类型建造时间(以秒为单位)
native GetUnitBuildTime     takes integer unitid                        returns integer
//获取AI玩家所拥有的金矿个数
native GetMinesOwned        takes nothing                               returns integer
//获得地图中未被采集保持满储金量的金矿数量
native GetGoldOwned         takes nothing                               returns integer
//获得编号最靠前的有金矿的基地编号
native TownWithMine         takes nothing                               returns integer
//基地有金矿可开采
native TownHasMine          takes integer townid                        returns boolean
//基地有城镇大厅
native TownHasHall          takes integer townid                        returns boolean
//获得AI玩家当前科技等级
native GetUpgradeLevel      takes integer id                            returns integer
//科技升级花费黄金
native GetUpgradeGoldCost   takes integer id                            returns integer
//科技升级花费木材
native GetUpgradeWoodCost   takes integer id                            returns integer
//获得下一个扩张点编号
native GetNextExpansion     takes nothing                               returns integer
//AI玩家当前目标单位?
native GetMegaTarget        takes nothing                               returns unit
//AI玩家正在建造的单位
native GetBuilding          takes player p                              returns unit
//获得敌人强度?
native GetEnemyPower        takes nothing                               returns integer
//设置联盟目标?
native SetAllianceTarget    takes unit id                               returns nothing
//获取联盟目标?
native GetAllianceTarget    takes nothing                               returns unit
//命令训练/建造单位(数量,单位类型,基地)
native SetProduce           takes integer qty, integer id, integer town returns boolean
//取消建造单位
native Unsummon             takes unit unitid                           returns nothing
//建造建筑<建筑工人,建筑类型>
native SetExpansion         takes unit peon, integer id                 returns boolean
//升级科技
native SetUpgrade           takes integer id                            returns boolean
//设置英雄升级技能函数
//函数要求返回整数-即是需要学习的技能ID
//函数内使用GetHeroId()获得升级的英雄类型
//使用GetHeroLevelAI()获得英雄等级
native SetHeroLevels        takes code func                             returns nothing
native SetNewHeroes         takes boolean state                         returns nothing
//购买地精飞艇
native PurchaseZeppelin     takes nothing                               returns nothing
//单位合体<数量,单位A,单位B,合体后的单位>
native MergeUnits           takes integer qty, integer a, integer b, integer make returns boolean
//单位变身<数量,变身后的单位>
native ConvertUnits         takes integer qty, integer id               returns boolean
//战役AI初始化?
native SetCampaignAI        takes nothing                               returns nothing
//对战AI初始化?
native SetMeleeAI           takes nothing                               returns nothing
//锁定英雄
native SetTargetHeroes      takes boolean state                         returns nothing
//修理建筑
native SetPeonsRepair       takes boolean state                         returns nothing
//随机路径
native SetRandomPaths       takes boolean state                         returns nothing
//保护用户
native SetDefendPlayer      takes boolean state                         returns nothing
//允许英雄逃跑
native SetHeroesFlee        takes boolean state                         returns nothing
//英雄购买物品
native SetHeroesBuyItems    takes boolean state                         returns nothing
//没有怜悯
native SetWatchMegaTargets  takes boolean state                         returns nothing
//忽略受伤单位
native SetIgnoreInjured     takes boolean state                         returns nothing
//英雄拾取物品
native SetHeroesTakeItems   takes boolean state                         returns nothing
//允许单位逃跑
native SetUnitsFlee         takes boolean state                         returns nothing
//允许群组逃跑
native SetGroupsFlee        takes boolean state                         returns nothing
//慢速采矿
native SetSlowChopping      takes boolean state                         returns nothing
//允许基地变换
native SetCaptainChanges    takes boolean allow                         returns nothing
//主动攻城(聪明炮兵)
native SetSmartArtillery    takes boolean state                         returns nothing
//设置AI会替换几次被杀的防卫者?
native SetReplacementCount  takes integer qty                           returns nothing
native GroupTimedLife       takes boolean allow                         returns nothing
//把受伤者送回主基或泉水
native RemoveInjuries       takes nothing                               returns nothing
native RemoveSiege          takes nothing                               returns nothing
//初始化攻击组
native InitAssault          takes nothing                               returns nothing
//增加单位进攻击组
native AddAssault           takes integer qty, integer id               returns boolean
//增加单位进防守组
native AddDefenders         takes integer qty, integer id               returns boolean
//获取营地<最小总等级,最大总等级,是否包括飞行单位>
native GetCreepCamp         takes integer min, integer max, boolean flyers_ok returns unit
//开始寻找敌人基地
native StartGetEnemyBase    takes nothing                               returns nothing
//是否正在寻找敌人基地
native WaitGetEnemyBase     takes nothing                               returns boolean
//获取敌人基地
native GetEnemyBase         takes nothing                               returns unit
//下一个扩张点有威胁的单位?
native GetExpansionFoe      takes nothing                               returns unit
//获取敌人扩张点(分基地)
native GetEnemyExpansion    takes nothing                               returns unit
//获取可能的扩张点X坐标
native GetExpansionX        takes nothing                               returns integer
//获取可能的扩张点X坐标
native GetExpansionY        takes nothing                               returns integer
native SetStagePoint        takes real x, real y                        returns nothing
//攻击组攻击单位
native AttackMoveKill       takes unit target                           returns nothing
//攻击组攻击地点
native AttackMoveXY         takes integer x, integer y                  returns nothing
//在<X坐标,Y坐标>空投单位?
native LoadZepWave          takes integer x, integer y                  returns nothing
//玩家自杀?
native SuicidePlayer        takes player id, boolean check_full         returns boolean
//让玩家杀自己的单位?
native SuicidePlayerUnits   takes player id, boolean check_full         returns boolean
//组在战斗中<是否攻击组>
native CaptainInCombat      takes boolean attack_captain                returns boolean
//单位在基地中
native IsTowered            takes unit target                           returns boolean
//清空资源单位任务分配
native ClearHarvestAI       takes nothing                               returns nothing
//设置(增加)采集金矿单位<地点,人数>
native HarvestGold          takes integer town, integer peons           returns nothing
//设置(增加)采集树木单位<地点,人数>
native HarvestWood          takes integer town, integer peons           returns nothing
//寻找1个扩张工人?
native GetExpansionPeon     takes nothing                               returns unit
//停止聚集?
native StopGathering        takes nothing                               returns nothing
//增加防守岗位?
native AddGuardPost         takes integer id, real x, real y            returns nothing
//填满防守岗位?
native FillGuardPosts       takes nothing                               returns nothing
//返回防守岗位?
native ReturnGuardPosts     takes nothing                               returns nothing
//创建攻击组
native CreateCaptains       takes nothing                               returns nothing
//设置组在家的点<组,x坐标,y坐标>
//组的参数: ATTACK_CAPTAIN = 1; 攻击组 DEFENSE_CAPTAIN = 2; 防御组 BOTH_CAPTAINS = 3 攻击和防御组;
native SetCaptainHome       takes integer which, real x, real y         returns nothing
//命令攻击组复位?
native ResetCaptainLocs     takes nothing                               returns nothing
//转移基地?
native ShiftTownSpot        takes real x, real y                        returns nothing
//命令攻击组传送到点
native TeleportCaptain      takes real x, real y                        returns nothing
//撤销攻击组目标
native ClearCaptainTargets  takes nothing                               returns nothing
//攻击组攻击
native CaptainAttack        takes real x, real y                        returns nothing
//攻击组攻击玩家的部队?
native CaptainVsUnits       takes player id                             returns nothing
//攻击组攻击玩家?
native CaptainVsPlayer      takes player id                             returns nothing
//命令攻击组回家
native CaptainGoHome        takes nothing                               returns nothing
//攻击组在家
native CaptainIsHome        takes nothing                               returns boolean
//攻击组准备完毕
native CaptainIsFull        takes nothing                               returns boolean
//攻击组为空
native CaptainIsEmpty       takes nothing                               returns boolean
//攻击组当前人数
native CaptainGroupSize     takes nothing                               returns integer
//攻击组准备就绪
native CaptainReadiness     takes nothing                               returns integer
//攻击组撤退
native CaptainRetreating    takes nothing                               returns boolean
//攻击组生命级别,生命级别为所有攻击组单位的平均百分比血量,最高为100
native CaptainReadinessHP   takes nothing                               returns integer
//攻击组魔法级别,魔法级别为所有攻击组单位的平均百分比魔法值,最高为100
native CaptainReadinessMa   takes nothing                               returns integer
//攻击组到达目的地
native CaptainAtGoal        takes nothing                               returns boolean
//存在中立生物
native CreepsOnMap          takes nothing                               returns boolean
//让一群单位自杀?
native SuicideUnit          takes integer count, integer unitid         returns nothing
native SuicideUnitEx        takes integer ct, integer uid, integer pid  returns nothing
//开始新线程
native StartThread          takes code func                             returns nothing
//休眠
native Sleep                takes real seconds                          returns nothing
//单位存活
native UnitAlive            takes unit id                               returns boolean
//单位隐形
native UnitInvis            takes unit id                               returns boolean
//某一类型单位被攻击组忽略的人数?
native IgnoredUnits         takes integer unitid                        returns integer
//基地被攻击
native TownThreatened       takes nothing                               returns boolean
native DisablePathing       takes nothing                               returns nothing
//允许水上战斗?
native SetAmphibious        takes nothing                               returns nothing
//获得命令队列中的指令条数
//命令可以通过动作'AI - 发送AI命令'发送给AI玩家
native CommandsWaiting      takes nothing                               returns integer
//获得队列中最新一条指令
native GetLastCommand       takes nothing                               returns integer
//获得队列中最新一条指令的数据
native GetLastData          takes nothing                               returns integer
//将最新指令踢出队列
native PopLastCommand       takes nothing                               returns nothing
//对战难度等级
native MeleeDifficulty      takes nothing                               returns integer



[/jass]
回复

使用道具 举报

发表于 2009-12-27 14:37:24 | 显示全部楼层
[jass]
//============================================================================
//  Globals for all AI scripts
//  所有AI剧本所用到的变量表
//============================================================================
globals

    //--------------------------------------------------------------------
    //  HUMANS
    //  人族 //--------------------------------------------------------------------

    // human heroes
    // 人族英雄
    constant integer ARCHMAGE           = 'Hamg'
    constant integer PALADIN            = 'Hpal'
    constant integer MTN_KING           = 'Hmkg'
    constant integer BLOOD_MAGE         = 'Hblm'

    // human hero abilities
    // 人族英雄技能
    // 山丘之王
    constant integer AVATAR             = 'AHav'
    constant integer BASH               = 'AHbh'
    constant integer THUNDER_BOLT       = 'AHtb'
    constant integer THUNDER_CLAP       = 'AHtc'
    // 圣骑士
    constant integer DEVOTION_AURA      = 'AHad'
    constant integer DIVINE_SHIELD      = 'AHds'
    constant integer HOLY_BOLT          = 'AHhb'
    constant integer RESURRECTION       = 'AHre'
    // 大魔法师
    constant integer BLIZZARD           = 'AHbz'
    constant integer BRILLIANCE_AURA    = 'AHab'
    constant integer MASS_TELEPORT      = 'AHmt'
    constant integer WATER_ELEMENTAL    = 'AHwe'
    // 血魔法师
    constant integer BANISH             = 'AHbn'
    constant integer FLAME_STRIKE       = 'AHfs'
    constant integer SUMMON_PHOENIX     = 'AHpx'
    constant integer SIPHON_MANA        = 'AHdr'

    // special human heroes
    // 特殊人族英雄(战役)
    constant integer JAINA              = 'Hjai'
    constant integer MURADIN            = 'Hmbr'
    constant integer GARITHOS           = 'Hlgr'
    constant integer KAEL               = 'Hkal'

    // human units
    // 人族单位
    constant integer COPTER             = 'hgyr'
    constant integer GYRO               =  COPTER
    constant integer ELEMENTAL          = 'hwat'
    constant integer FOOTMAN            = 'hfoo'
    constant integer FOOTMEN            =  FOOTMAN
    constant integer GRYPHON            = 'hgry'
    constant integer KNIGHT             = 'hkni'
    constant integer MORTAR             = 'hmtm'
    constant integer PEASANT            = 'hpea'
    constant integer PRIEST             = 'hmpr'
    constant integer RIFLEMAN           = 'hrif'
    constant integer RIFLEMEN           =  RIFLEMAN
    constant integer SORCERESS          = 'hsor'
    constant integer TANK               = 'hmtt'
    constant integer STEAM_TANK         =  TANK
    constant integer ROCKET_TANK        = 'hrtt'
    constant integer MILITIA            = 'hmil'
    constant integer SPELL_BREAKER      = 'hspt'
    constant integer HUMAN_DRAGON_HAWK  = 'hdhw'

    // special human units
    // 特殊人族单位
    constant integer BLOOD_PRIEST       = 'hbep'
    constant integer BLOOD_SORCERESS    = 'hbes'
    constant integer BLOOD_PEASANT      = 'nhew'

    // human buildings
    // 人族建筑
    constant integer AVIARY             = 'hgra'
    constant integer BARRACKS           = 'hbar'
    constant integer BLACKSMITH         = 'hbla'
    constant integer CANNON_TOWER       = 'hctw'
    constant integer CASTLE             = 'hcas'
    constant integer CHURCH             = 'htws'
    constant integer MAGE_TOWER         =  CHURCH
    constant integer GUARD_TOWER        = 'hgtw'
    constant integer HOUSE              = 'hhou'
    constant integer HUMAN_ALTAR        = 'halt'
    constant integer KEEP               = 'hkee'
    constant integer LUMBER_MILL        = 'hlum'
    constant integer SANCTUM            = 'hars'
    constant integer ARCANE_SANCTUM     =  SANCTUM
    constant integer TOWN_HALL          = 'htow'
    constant integer WATCH_TOWER        = 'hwtw'
    constant integer WORKSHOP           = 'harm'
    constant integer ARCANE_VAULT       = 'hvlt'
    constant integer ARCANE_TOWER       = 'hatw'

    // human upgrades
    // 人族科技
    constant integer UPG_MELEE          = 'Rhme'
    constant integer UPG_RANGED         = 'Rhra'
    constant integer UPG_ARTILLERY      = 'Rhaa'
    constant integer UPG_ARMOR          = 'Rhar'
    constant integer UPG_GOLD           = 'Rhmi'
    constant integer UPG_MASONRY        = 'Rhac'
    constant integer UPG_SIGHT          = 'Rhss'
    constant integer UPG_DEFEND         = 'Rhde'
    constant integer UPG_BREEDING       = 'Rhan'
    constant integer UPG_PRAYING        = 'Rhpt'
    constant integer UPG_SORCERY        = 'Rhst'
    constant integer UPG_LEATHER        = 'Rhla'
    constant integer UPG_GUN_RANGE      = 'Rhri'
    constant integer UPG_WOOD           = 'Rhlh'
    constant integer UPG_SENTINEL       = 'Rhse'
    constant integer UPG_SCATTER        = 'Rhsr'
    constant integer UPG_BOMBS          = 'Rhgb'
    constant integer UPG_HAMMERS        = 'Rhhb'
    constant integer UPG_CONT_MAGIC     = 'Rhss'
    constant integer UPG_FRAGS          = 'Rhfs'
    constant integer UPG_TANK           = 'Rhrt'
    constant integer UPG_FLAK           = 'Rhfc'
    constant integer UPG_CLOUD          = 'Rhcd'

//--------------------------------------------------------------------
//其他种族参照人族吧
//--------------------------------------------------------------------
//          以下一段忽略
//
//--------------------------------------------------------------------


    //--------------------------------------------------------------------
    // 时间常量
    constant integer M1                 =    60
    constant integer M2                 =  2*60
    constant integer M3                 =  3*60
    constant integer M4                 =  4*60
    constant integer M5                 =  5*60
    constant integer M6                 =  6*60
    constant integer M7                 =  7*60
    constant integer M8                 =  8*60
    constant integer M9                 =  9*60
    constant integer M10                = 10*60
    constant integer M11                = 11*60
    constant integer M12                = 12*60
    constant integer M13                = 13*60
    constant integer M14                = 14*60
    constant integer M15                = 15*60
    // 难度
    constant integer EASY               = 1
    constant integer NORMAL             = 2
    constant integer HARD               = 3
    constant integer INSANE             = 4 // not used
    // AI等级?
    constant integer MELEE_NEWBIE       = 1
    constant integer MELEE_NORMAL       = 2
    constant integer MELEE_INSANE       = 3
    // 队长编号
    constant integer ATTACK_CAPTAIN     = 1
    constant integer DEFENSE_CAPTAIN    = 2
    constant integer BOTH_CAPTAINS      = 3
    // 建造类型
    constant integer BUILD_UNIT         = 1
    constant integer BUILD_UPGRADE      = 2
    constant integer BUILD_EXPAND       = 3

    constant integer UPKEEP_TIER1       = 50
    constant integer UPKEEP_TIER2       = 80

    //--------------------------------------------------------------------



player ai_player //当前AI玩家
integer sleep_seconds //等待时间
integer total_gold = 0 //金钱
integer total_wood = 0 //木材
integer gold_buffer = 0
// usually for potion money  预留金钱?
integer difficulty = NORMAL //难度,默认为中等难度
integer exp_seen = 0
integer racial_farm = 'hhou' //人口建筑类型
integer hero_id = 'Hamg'  //第一个英雄的ID,默认为圣骑士
integer hero_id2 = 'Hmkg' //第二个英雄的ID,默认为山丘之王
integer hero_id3 = 'Hpal' //第三个英雄的ID,默认为大法
//学习技能的变量
integer array skill
integer array skills1
integer array skills2
integer array skills3
integer max_hero_level = 0 //当前最高的英雄等级
//攻击单位的全局变量
integer array harass_qty   //攻击单位数量
integer array harass_max   //攻击单位最大数量
integer array harass_units //攻击单位类型
integer harass_length = 0  //攻击单位索引数
//防守单位的全局变量
integer array defense_qty   //防守单位的数量
integer array defense_units //防守单位类型
integer defense_length = 0  //防守单位索引数
//建造的全局变量
integer array build_qty  //建造的数量
integer array build_type //建造的种类,单位/建筑/升级
integer array build_item //建造的类型
integer array build_town //建造所在基地
integer build_length = 0 //建造工程索引数
integer campaign_gold_peons = 5 //主基地采集黄金的人数
integer campaign_wood_peons = 3 //主基地采集木材的人数
integer campaign_basics_speed = 5
integer min_creeps = -1
integer max_creeps = -1
//游戏初始设置的全局变量
boolean harvest_town1 = true
boolean harvest_town2 = true
boolean harvest_town3 = true
boolean do_campaign_farms = true //自动制造战役AI 提供人口的建筑
boolean two_heroes = false       //当前是否双英雄
boolean allow_air_creeps = false //空中的中立单位(假如有,AI会针对性的发展)
boolean take_exp = false //
//允许发送指令终止AI
    boolean allow_signal_abort      = false
//准备购买地精飞艇
    boolean ready_for_zeppelin      = true
//需要购买地精飞艇
    boolean get_zeppelin            = false
//建造战斗单位?
    boolean build_campaign_attackers = true

    boolean do_debug_cheats         = false //运行debug语句?
    boolean trace_on                = true  //测试状态
    boolean zep_next_wave           = false
    boolean form_group_timeouts     = true  //集结队伍超时
endglobals

[/jass]
回复

使用道具 举报

发表于 2009-12-27 14:38:14 | 显示全部楼层
[jass]
//============================================================================
//获取玩家,从1开始
function PlayerEx takes integer slot returns player
    return Player(slot-1)
endfunction

//============================================================================
//测试时发送文本?
function Trace takes string message returns nothing
    if trace_on then
        call DisplayText(GetAiPlayer(),message)
    endif
endfunction

//============================================================================
//测试时发送含1个数字的文本?
function TraceI takes string message, integer val returns nothing
    if trace_on then
        call DisplayTextI(GetAiPlayer(),message,val)
    endif
endfunction

//============================================================================
//测试时发送含2个数字的文本?
function TraceII takes string message, integer v1, integer v2 returns nothing
    if trace_on then
        call DisplayTextII(GetAiPlayer(),message,v1,v2)
    endif
endfunction

//============================================================================
//测试时发送含3个数字的文本?
function TraceIII takes string message, integer v1, integer v2, integer v3 returns nothing
    if trace_on then
        call DisplayTextIII(GetAiPlayer(),message,v1,v2,v3)
    endif
endfunction

//============================================================================
//初始化AI
function InitAI takes nothing returns nothing
    set ai_player = Player(GetAiPlayer())
    set sleep_seconds = 0
    call StopGathering()
endfunction

//============================================================================
//AI初始化<英雄技能函数,建造线程函数,战斗线程函数>
function StandardAI takes code heroes, code peons, code attacks returns nothing

    local boolean isNewbie = (MeleeDifficulty() == MELEE_NEWBIE)

    call InitAI()

    call SetMeleeAI()

    call SetDefendPlayer(true)
    call SetGroupsFlee(not isNewbie)
    call SetHeroesBuyItems(not isNewbie)
    call SetHeroesFlee(true)
    call SetHeroesTakeItems(true)
    call SetIgnoreInjured(true)
    call SetPeonsRepair(true)
    call SetSmartArtillery(not isNewbie)
    call SetTargetHeroes(not isNewbie)
    call SetUnitsFlee(not isNewbie)
    call SetWatchMegaTargets(true)

    call CreateCaptains()

    call SetHeroLevels(heroes)

    call Sleep(0.1)
    call StartThread(peons)
    call StartThread(attacks)
endfunction

//============================================================================
//  Utility Functions
//============================================================================
//取较小值
function Min takes integer A, integer B returns integer
    if A < B then
        return A
    else
        return B
    endif
endfunction
//取较大值
function Max takes integer A, integer B returns integer
    if A > B then
        return A
    else
        return B
    endif
endfunction


function SetZepNextWave takes nothing returns nothing
    set zep_next_wave = true
endfunction

//睡眠,改变会记录在sleep_seconds上,在睡眠中可以被打断
function SuicideSleep takes integer seconds returns nothing
    set sleep_seconds = sleep_seconds - seconds
    loop
        exitwhen seconds <= 0
        exitwhen allow_signal_abort and CommandsWaiting() != 0

        if seconds >= 5 then
            call Sleep(5)
            set seconds = seconds - 5
        else
            call Sleep(seconds)
            set seconds = 0
        endif
    endloop
endfunction

//============================================================================
//等待命令,销毁它,并传回命令值
function WaitForSignal takes nothing returns integer
    local integer cmd
    local boolean display = false //xxx
    loop
        exitwhen CommandsWaiting() != 0

        //xxx
        call Trace("waiting for a signal to begin AI script...\\n")
        set display = true
        call Sleep(2)
        exitwhen CommandsWaiting() != 0
        call Sleep(2)
        exitwhen CommandsWaiting() != 0
        call Sleep(2)
        exitwhen CommandsWaiting() != 0
        call Sleep(2)
        exitwhen CommandsWaiting() != 0
        call Sleep(2)
        //xxx

    endloop

    //xxx
    if display then
        call Trace("signal received, beginning AI script\\n")
    endif
    //xxx

    set cmd = GetLastCommand()
    call PopLastCommand()
    return cmd
endfunction

//============================================================================
//设置采集木材单位数
function SetWoodPeons takes integer count returns nothing
    set campaign_wood_peons = count
endfunction

//============================================================================
//设置采集黄金单位数
function SetGoldPeons takes integer count returns nothing
    set campaign_gold_peons = count
endfunction

//============================================================================
//让电脑按默认设置砍伐木材?(看上去不好用饿)
function SetHarvestLumber takes boolean harvest returns nothing
    if harvest then
        set campaign_wood_peons = 3
    else
        set campaign_wood_peons = 0
    endif
endfunction

//============================================================================
//设置集结部队是否超时
function SetFormGroupTimeouts takes boolean state returns nothing
    set form_group_timeouts = state
endfunction

//============================================================================
//设置是否建造人口建筑
function DoCampaignFarms takes boolean state returns nothing
    set do_campaign_farms = state
endfunction

//============================================================================
//小型营地目标
function GetMinorCreep takes nothing returns unit
    return GetCreepCamp(0,9,false)
endfunction

//============================================================================
//大型营地目标
function GetMajorCreep takes nothing returns unit
    return GetCreepCamp(10,100,allow_air_creeps)
endfunction

//============================================================================
//当前AI玩家拥有的黄金资源
function GetGold takes nothing returns integer
    return GetPlayerState(ai_player,PLAYER_STATE_RESOURCE_GOLD)
endfunction

//============================================================================
//当前AI玩家拥有的木材资源
function GetWood takes nothing returns integer
    return GetPlayerState(ai_player,PLAYER_STATE_RESOURCE_LUMBER)
endfunction

//============================================================================
//初始化建筑系统
function InitBuildArray takes nothing returns nothing
    set build_length = 0
endfunction

//============================================================================
//初始化进攻组
function InitAssaultGroup takes nothing returns nothing
    set harass_length = 0
endfunction

//============================================================================
//初始化防御组
function InitDefenseGroup takes nothing returns nothing
    set defense_length = 0
endfunction

//============================================================================
//初始化对战攻击组设置
function InitMeleeGroup takes nothing returns nothing
    call InitAssaultGroup()
    call RemoveInjuries()
    call RemoveSiege()
endfunction

//============================================================================
function PrepFullSuicide takes nothing returns nothing
    call InitAssaultGroup()
    call InitDefenseGroup()
    set campaign_gold_peons = 0
    set campaign_wood_peons = 0
endfunction

//============================================================================
//设置AI会替换几次被杀的防卫者。分难度
function SetReplacements takes integer easy, integer med, integer hard returns nothing
    if difficulty == EASY then
        call SetReplacementCount(easy)
    elseif difficulty == NORMAL then
        call SetReplacementCount(med)
    else
        call SetReplacementCount(hard)
    endif
endfunction

//============================================================================
//开始一个线程
function StartTownBuilder takes code func returns nothing
    call StartThread(func)
endfunction

//============================================================================
//所有类型发展操作
//t:训练/升级/建造 qty:数量 unitid:类型ID town:所在城镇
function SetBuildAll takes integer t, integer qty, integer unitid, integer town returns nothing
    if qty > 0 then
        set build_qty[build_length] = qty
        set build_type[build_length] = t
        set build_item[build_length] = unitid
        set build_town[build_length] = town
        set build_length = build_length + 1
    endif
endfunction

//============================================================================
//命令AI玩家训练单位<数量,单位类型>
function SetBuildUnit takes integer qty, integer unitid returns nothing
    call SetBuildAll(BUILD_UNIT,qty,unitid,-1)
endfunction

//============================================================================
//再建造一个单位?
function SetBuildNext takes integer qty, integer unitid returns nothing
    local integer has = GetUnitCount(unitid)
    if has >= qty then
        return
    endif
    call SetBuildAll(BUILD_UNIT,GetUnitCountDone(unitid)+1,unitid,-1)
endfunction

//============================================================================
//指令AI玩家制造单位按照游戏难度<数量,单位类型>
function SetBuildUnitEx takes integer easy, integer med, integer hard, integer unitid returns nothing
    if difficulty == EASY then
        call SetBuildAll(BUILD_UNIT,easy,unitid,-1)
    elseif difficulty == NORMAL then
        call SetBuildAll(BUILD_UNIT,med,unitid,-1)
    else
        call SetBuildAll(BUILD_UNIT,hard,unitid,-1)
    endif
endfunction

//============================================================================
//在某城镇建造单位
function SecondaryTown takes integer town, integer qty, integer unitid returns nothing
    call SetBuildAll(BUILD_UNIT,qty,unitid,town)
endfunction

//============================================================================
//和function SecondaryTown一样的= =
function SecTown takes integer town, integer qty, integer unitid returns nothing
    call SetBuildAll(BUILD_UNIT,qty,unitid,town)
endfunction

//============================================================================
//命令升级科技等级
function SetBuildUpgr takes integer qty, integer unitid returns nothing
//新手难度只能升1级科技
    if MeleeDifficulty() != MELEE_NEWBIE or qty == 1 then
        call SetBuildAll(BUILD_UPGRADE,qty,unitid,-1)
    endif
endfunction

//============================================================================
//命令升级科技等级在简单,普通,疯狂三种难度下。
function SetBuildUpgrEx takes integer easy, integer med, integer hard, integer unitid returns nothing
    if difficulty == EASY then
        call SetBuildAll(BUILD_UPGRADE,easy,unitid,-1)
    elseif difficulty == NORMAL then
        call SetBuildAll(BUILD_UPGRADE,med,unitid,-1)
    else
        call SetBuildAll(BUILD_UPGRADE,hard,unitid,-1)
    endif
endfunction

//============================================================================
//命令建造建筑
function SetBuildExpa takes integer qty, integer unitid returns nothing
    call SetBuildAll(BUILD_EXPAND,qty,unitid,-1)
endfunction

//============================================================================
//AI升级科技<提升到的等级,科技>,需要判断足量资源
function StartUpgrade takes integer level, integer upgid returns boolean
    local integer gold_cost
    local integer wood_cost

    if GetUpgradeLevel(upgid) >= level then
        return true
    endif

    set gold_cost = GetUpgradeGoldCost(upgid)
    if total_gold < gold_cost then
        return false
    endif

    set wood_cost = GetUpgradeWoodCost(upgid)
    if total_wood < wood_cost then
        return false
    endif

    return SetUpgrade(upgid)
endfunction

//============================================================================
//按金钱>1000和木材>500来决定制造1或2个单位(多用于区别双倍采集,MS没什么实用性)
function BuildFactory takes integer unitid returns nothing
    if GetGold() > 1000 and GetWood() > 500 then
        call SetBuildUnit( 2, unitid )
    else
        call SetBuildUnit( 1, unitid )
    endif
endfunction

//============================================================================
//是否该单位类型全部被制造出
function HallsCompleted takes integer unitid returns boolean
    return GetUnitCount(unitid) == GetUnitCountDone(unitid)
endfunction

//============================================================================
//有基地城镇大厅和金矿才建造单位
function GuardSecondary takes integer townid, integer qty, integer unitid returns nothing
    if TownHasHall(townid) and TownHasMine(townid) then
        call SecondaryTown( townid, qty, unitid )
    endif
endfunction

//============================================================================
//获取基地中单位数<单位类型,是否建造完毕,基地>
function GetUnitCountEx takes integer unitid, boolean only_done, integer townid returns integer
    if townid == -1 then
        if only_done then
            return GetUnitCountDone(unitid)
        else
            return GetUnitCount(unitid)
        endif
    else
        return GetTownUnitCount(unitid,townid,only_done)
    endif
endfunction

//============================================================================
//AI判断单位个数,返回的单位个数包括该类型单位的升级单位<单位类型,仅仅判断建造完毕的,城镇编号>
function TownCountEx takes integer unitid, boolean only_done, integer townid returns integer

    local integer have_qty = GetUnitCountEx(unitid,only_done,townid)
    //判断该类型升级类型单位(对战设置)
    if unitid == TOWN_HALL then
        set have_qty = have_qty + GetUnitCountEx(KEEP,false,townid) + GetUnitCountEx(CASTLE,false,townid)
    elseif unitid == KEEP then
        set have_qty = have_qty  + GetUnitCountEx(CASTLE,false,townid)

    elseif unitid == WATCH_TOWER then
        set have_qty = have_qty + GetUnitCountEx(GUARD_TOWER,false,townid) + GetUnitCountEx(CANNON_TOWER,false,townid) + GetUnitCountEx(ARCANE_TOWER,false,townid)

    elseif unitid == PEASANT then
        set have_qty = have_qty + GetUnitCountEx(MILITIA,false,townid)

    elseif unitid == GREAT_HALL then
        set have_qty = have_qty + GetUnitCountEx(STRONGHOLD,false,townid) + GetUnitCountEx(FORTRESS,false,townid)
    elseif unitid == STRONGHOLD then
        set have_qty = have_qty + GetUnitCountEx(FORTRESS,false,townid)

    elseif unitid == HEAD_HUNTER then
        set have_qty = have_qty + GetUnitCountEx(BERSERKER,false,townid)

    elseif unitid == SPIRIT_WALKER then
        set have_qty = have_qty + GetUnitCountEx(SPIRIT_WALKER_M,false,townid)
    elseif unitid == SPIRIT_WALKER_M then
        set have_qty = have_qty + GetUnitCountEx(SPIRIT_WALKER,only_done,townid)

    elseif unitid == NECROPOLIS_1 then
        set have_qty = have_qty + GetUnitCountEx(NECROPOLIS_2,false,townid) + GetUnitCountEx(NECROPOLIS_3,false,townid)
    elseif unitid == NECROPOLIS_2 then
        set have_qty = have_qty + GetUnitCountEx(NECROPOLIS_3,false,townid)

    elseif unitid == ZIGGURAT_1 then
        set have_qty = have_qty + GetUnitCountEx(ZIGGURAT_2,false,townid) + GetUnitCountEx(ZIGGURAT_FROST,false,townid)

    elseif unitid == GARGOYLE then
        set have_qty = have_qty + GetUnitCountEx(GARGOYLE_MORPH,false,townid)

    elseif unitid == TREE_LIFE then
        set have_qty = have_qty + GetUnitCountEx(TREE_AGES,false,townid) + GetUnitCountEx(TREE_ETERNITY,false,townid)
    elseif unitid == TREE_AGES then
        set have_qty = have_qty + GetUnitCountEx(TREE_ETERNITY,false,townid)

    elseif unitid == DRUID_TALON then
        set have_qty = have_qty + GetUnitCountEx(DRUID_TALON_M,false,townid)
    elseif unitid == DRUID_TALON_M then
        set have_qty = have_qty + GetUnitCountEx(DRUID_TALON,only_done,townid)

    elseif unitid == DRUID_CLAW then
        set have_qty = have_qty + GetUnitCountEx(DRUID_CLAW_M,false,townid)
    elseif unitid == DRUID_CLAW_M then
        set have_qty = have_qty + GetUnitCountEx(DRUID_CLAW,only_done,townid)

    elseif unitid == ILLIDAN then
        set have_qty = have_qty + GetUnitCountEx(ILLIDAN_DEMON,false,townid)

    endif
    return have_qty
endfunction

//============================================================================
//AI判断单位个数,返回的单位个数包括该类型单位的升级单位,不包括未建造完毕的
function TownCountDone takes integer base returns integer
    return TownCountEx(base,true,-1)
endfunction

//============================================================================
//AI判断单位个数,返回的单位个数包括该类型单位的升级单位,包括未建造完毕的
function TownCount takes integer base returns integer
    return TownCountEx(base,false,-1)
endfunction

//============================================================================
//如果建筑全部建造完毕则再造一个
function BasicExpansion takes boolean build_it, integer unitid returns nothing
    if build_it and HallsCompleted(unitid) then
        call SetBuildExpa( TownCount(unitid)+1, unitid )
    endif
endfunction

//============================================================================
//建造和baseid同数量的newid?什么用处那?
function UpgradeAll takes integer baseid, integer newid returns nothing
    call SetBuildUnit( TownCountDone(baseid), newid )
endfunction

//============================================================================
//AI判断某城镇内单位个数,返回的单位个数包括该类型单位的升级单位,包括未建造完毕的
function TownCountTown takes integer base, integer townid returns integer
    return TownCountEx(base,false,townid)
endfunction

//============================================================================
//  FoodPool
//============================================================================
//按传入的人口限制制造单位<人口限制,制造第一种,第一种单位,第一种单位每个所占人口,制造第二种(优先),下面两个同上>
function FoodPool takes integer food, boolean weak, integer id1, integer use1, boolean strong, integer id2, integer use2 returns nothing
    if strong then
        call SetBuildUnit( (food - use1 * TownCount(id1)) / use2, id2 )
    elseif weak then
        call SetBuildUnit( (food - use2 * TownCount(id2)) / use1, id1 )
    endif
endfunction
[/jass]
回复

使用道具 举报

发表于 2009-12-27 14:41:42 | 显示全部楼层
[jass]
//============================================================================
//  MeleeTownHall
//============================================================================
//建造城镇大厅
function MeleeTownHall takes integer townid, integer unitid returns nothing
    //有金矿但无城镇大厅
    if TownHasMine(townid) and not TownHasHall(townid) then
        call SecondaryTown ( townid, 1, unitid )
    endif
endfunction

//============================================================================
//等待单位数量满足条件
function WaitForUnits takes integer unitid, integer qty returns nothing
    loop
        exitwhen TownCountDone(unitid) == qty
        call Sleep(2)
    endloop
endfunction

//============================================================================
//AI建造单位<数量,单位类型,城镇编号>
function StartUnit takes integer ask_qty, integer unitid, integer town returns boolean
    local integer have_qty
    local integer need_qty
    local integer afford_gold
    local integer afford_wood
    local integer afford_qty
    local integer gold_cost
    local integer wood_cost

    //------------------------------------------------------------------------
    // if we have all we're asking for then make nothing
    //如果我们已经有了所有我们要AI制造的单位,那么什么也不做
    //
    if town == -1 then
        set have_qty = TownCount(unitid)
    else
        set have_qty = TownCountTown(unitid,town)
    endif

    if have_qty >= ask_qty then
        return true
    endif
    set need_qty = ask_qty - have_qty

    //------------------------------------------------------------------------
    // limit the qty we're requesting to the amount of resources available
    //
    set gold_cost = GetUnitGoldCost(unitid)
    set wood_cost = GetUnitWoodCost(unitid)

    if gold_cost == 0 then
        set afford_gold = need_qty
    else
        set afford_gold = total_gold / gold_cost
    endif
    if afford_gold < need_qty then
        set afford_qty = afford_gold
    else
        set afford_qty = need_qty
    endif

    if wood_cost == 0 then
        set afford_wood = need_qty
    else
        set afford_wood = total_wood / wood_cost
    endif
    if afford_wood < afford_qty then
        set afford_qty = afford_wood
    endif

    // if we're waiting on gold/wood; pause build orders
    //如果我们的金钱/木材还不够,那么停止建造命令
    if afford_qty < 1 then
        return false
    endif

    //------------------------------------------------------------------------
    // whether we make right now what we're requesting or not, assume we will
    // and deduct the cost of the units from our fake gold total right away
    //
    set total_gold = total_gold - gold_cost * need_qty
    set total_wood = total_wood - wood_cost * need_qty

    if total_gold < 0 then
        set total_gold = 0
    endif
    if total_wood < 0 then
        set total_wood = 0
    endif

    //------------------------------------------------------------------------
    // give the AI a chance to make the units (it may not be able to right now
    // but that doesn't stop us from trying other units after this as long
    // as we have enough money to make this AND the needed, unbuilt ones)
    //
    return SetProduce(afford_qty,unitid,town)
endfunction

//============================================================================
//等待建筑数量达到条件?最多120秒
function WaitForTown takes integer towns, integer townid returns nothing
    local integer i = 0
    loop
        call Sleep(10)
        exitwhen TownCount(townid) >= towns
        set i = i + 1
        exitwhen i == 12
    endloop
endfunction

//============================================================================
//AI建造建筑<数量,单位类型>
function StartExpansion takes integer qty, integer hall returns boolean
    local integer count
    local integer town
    local unit    peon
    local integer gold_cost

    set count = TownCount(hall)
    if count >= qty then
        return true
    endif

    set town = GetNextExpansion()
    if town == -1 then
        return true
    endif

    set take_exp = true

    set gold_cost = GetUnitGoldCost(hall)
    if gold_cost > total_gold then
        return false
    endif
    set total_gold = total_gold - gold_cost

    if GetExpansionFoe() != null then
        return true
    endif

    set peon = GetExpansionPeon()
    if peon != null then
        return SetExpansion(peon,hall)
    endif

    return true
endfunction

//============================================================================
//建造发展的判断,动作函数
function OneBuildLoop takes nothing returns nothing
    local integer index = 0
    local integer qty
    local integer id
    local integer tp

    set total_gold = GetGold() - gold_buffer
    set total_wood = GetWood()
//开始此轮训练建造升级的循环
    loop
        exitwhen index == build_length

        set qty = build_qty [index]
        set id  = build_item[index]
        set tp  = build_type[index]

        //--------------------------------------------------------------------
//建造单位
        if tp == BUILD_UNIT then
            if not StartUnit(qty,id,build_town[index]) then
                return
            endif

        //--------------------------------------------------------------------
//升级科技
        elseif tp == BUILD_UPGRADE then
            call StartUpgrade(qty,id)

        //--------------------------------------------------------------------
//建筑
        else // tp == BUILD_EXPAND
            if not StartExpansion(qty,id) then
                return
            endif
        endif

        set index = index + 1
    endloop
endfunction

//============================================================================
//交错睡眠,大概是为了效率吧
function StaggerSleep takes real base, real spread returns nothing
    call Sleep(base + spread * I2R(GetAiPlayer()) / I2R(GetPlayers()))
//AI玩家的玩家号/地图编辑器中开启的玩家数量?
endfunction

//============================================================================
//发展的线程函数
function BuildLoop takes nothing returns nothing
    call OneBuildLoop()
    call StaggerSleep(1,2)
    loop
        call OneBuildLoop()
        call Sleep(2)
    endloop
endfunction

//============================================================================
//开始发展的线程
function StartBuildLoop takes nothing returns nothing
    call StartThread(function BuildLoop)
endfunction

//============================================================================
//设置 sleep_seconds
function SetInitialWave takes integer seconds returns nothing
    set sleep_seconds = seconds
endfunction

//============================================================================
//增加 sleep_seconds
function AddSleepSeconds takes integer seconds returns nothing
    set sleep_seconds = sleep_seconds + seconds
endfunction

//============================================================================
//(相当于)永久停止一个线程
function SleepForever takes nothing returns nothing
    call Trace("going to sleep forever\\n") //xxx
    loop
        call Sleep(100)
    endloop
endfunction

//============================================================================
//对战开始发展的函数
function PlayGame takes nothing returns nothing
    call StartBuildLoop()
    call SleepForever()
endfunction

//============================================================================
//变化基础单位,至少要有一个
function ConvertNeeds takes integer unitid returns nothing
    if GetUnitCount(unitid) < 1 then
        call StartUnit(1,unitid,-1)
    endif
endfunction

//============================================================================
//判断制造单位从属变化函数
function Conversions takes integer desire, integer unitid returns nothing

    if GetUnitCount(unitid) >= desire then
        return
    endif
    //角鹰兽射手
    if unitid == HIPPO_RIDER then
        call ConvertNeeds(ARCHER)
        call ConvertNeeds(HIPPO)
        call MergeUnits(desire,ARCHER,HIPPO,HIPPO_RIDER)
    //毁灭者(十胜石雕像-黑曜石雕像)
    elseif unitid == BLK_SPHINX then
        call ConvertNeeds(OBS_STATUE)
        call ConvertUnits(desire,OBS_STATUE)
    endif
endfunction

//============================================================================
//增加单位记录进攻击组
function SetAssaultGroup takes integer qty, integer max, integer unitid returns nothing
    call Conversions(max,unitid)

    if qty <= 0 and TownCountDone(unitid) == 0 then
        return
    endif
    set harass_qty[harass_length] = qty
    set harass_max[harass_length] = max
    set harass_units[harass_length] = unitid
    set harass_length = harass_length + 1
endfunction

//============================================================================
//按难度增加三类单位记录进攻击组
function Interleave3 takes integer e1, integer m1, integer h1, integer u1, integer e2, integer m2, integer h2, integer u2, integer e3, integer m3, integer h3, integer u3 returns nothing
    local integer i1 = 1
    local integer i2 = 1
    local integer i3 = 1
    local integer q1
    local integer q2
    local integer q3

    if difficulty == EASY then
        set q1 = e1
        set q2 = e2
        set q3 = e3
    elseif difficulty == NORMAL then
        set q1 = m1
        set q2 = m2
        set q3 = m3
    else // difficulty == HARD
        set q1 = h1
        set q2 = h2
        set q3 = h3
    endif

    loop
        exitwhen q1<=0 and q2<=0 and q3<=0

        if q1 > 0 then
            call SetAssaultGroup(i1,i1,u1)
            set q1 = q1 - 1
            set i1 = i1 + 1
        endif

        if q2 > 0 then
            call SetAssaultGroup(i2,i2,u2)
            set q2 = q2 - 1
            set i2 = i2 + 1
        endif

        if q3 > 0 then
            call SetAssaultGroup(i3,i3,u3)
            set q3 = q3 - 1
            set i3 = i3 + 1
        endif
    endloop
endfunction

//============================================================================
//对战添加攻击组单位
function SetMeleeGroup takes integer unitid returns nothing
    if unitid == hero_id then
        call SetAssaultGroup(1,9,unitid)
    else
        call SetAssaultGroup((TownCountDone(unitid)*3)/4,20,unitid)
    endif
endfunction

//============================================================================
//设置防守单位的数量
function CampaignDefender takes integer level, integer qty, integer unitid returns nothing
    if qty > 0 and difficulty >= level then
        set defense_qty[defense_length] = qty
        set defense_units[defense_length] = unitid
        set defense_length = defense_length + 1
        call Conversions(qty,unitid)
        call SetBuildUnit(qty,unitid)
    endif
endfunction

//============================================================================
//按难度设置防守单位的数量
function CampaignDefenderEx takes integer easy, integer med, integer hard, integer unitid returns nothing
    if difficulty == EASY then
        call CampaignDefender(EASY,easy,unitid)
    elseif difficulty == NORMAL then
        call CampaignDefender(NORMAL,med,unitid)
    else
        call CampaignDefender(HARD,hard,unitid)
    endif
endfunction

//============================================================================
//增加进攻组的进攻单位
function CampaignAttacker takes integer level, integer qty, integer unitid returns nothing
    if qty > 0 and difficulty >= level then
        call SetAssaultGroup(qty,qty,unitid)
    endif
endfunction

//============================================================================
//增加进攻组的进攻单位,按游戏难度
function CampaignAttackerEx takes integer easy, integer med, integer hard, integer unitid returns nothing
    if difficulty == EASY then
        call CampaignAttacker(EASY,easy,unitid)
    elseif difficulty == NORMAL then
        call CampaignAttacker(NORMAL,med,unitid)
    else
        call CampaignAttacker(HARD,hard,unitid)
    endif
endfunction

//============================================================================
//集结队伍
function FormGroup takes integer seconds, boolean testReady returns nothing
    local integer index
    local integer count
    local integer unitid
    local integer desire
    local integer readyPercent
    //通常testReady是用CaptainReadiness()判断的
    // normally test for CaptainReadiness() of 50%
    if testReady == true then
        set readyPercent = 50
        call Trace("forming group, requiring healthy guys\\n") //xxx
    else
        set readyPercent = 0
        call Trace("forming group, unit health not important\\n") //xxx
    endif

    call Trace("trying to gather forces\\n") //xxx

    loop
        call SuicideSleep(seconds)
        call InitAssault()

        set index = 0
        loop
            exitwhen index == harass_length

            set unitid = harass_units[index]
            set desire = harass_max[index]
            set count  = TownCountDone(unitid)

            call Conversions(desire,unitid)

            if count >= desire then
                call AddAssault(desire,unitid)
            else
                set desire = harass_qty[index]

                if count < desire then
                    call AddAssault(desire,unitid)
                else
                    call AddAssault(count,unitid)
                endif
            endif

            set index = index + 1
        endloop

        //xxx
        if form_group_timeouts and (sleep_seconds < -60) then
            //超过时限
            call Trace("exit form group -- timeout\\n")
        elseif CaptainInCombat(true) then
            //攻击组战斗中
            call Trace("exit form group -- can't form while already in combat\\n")
        elseif CaptainIsFull() and CaptainReadiness() >= readyPercent then
            //准备好
            call Trace("exit form group -- ready\\n")
        endif
        //xxx

        // time out and send group anyway if time has already expired
        exitwhen form_group_timeouts and (sleep_seconds < -60)
        exitwhen CaptainInCombat(true)
        exitwhen CaptainIsFull() and CaptainReadiness() >= readyPercent
    endloop
endfunction

//============================================================================
//单位需要的准备时间(就是建造时间)
function WavePrepare takes integer unitid returns integer
    return GetUnitBuildTime(unitid)
endfunction

//============================================================================
//一切攻击单位准备好所至少需要的时间
function PrepTime takes nothing returns integer
    local integer unitid
    local integer missing
    local integer prep
    local integer count
    local integer largest = 30
    local integer index = 0

    loop
        exitwhen index == harass_length

        set unitid  = harass_units[index]
        set missing = harass_qty[index] + IgnoredUnits(unitid) - TownCount(unitid)
        set prep    = WavePrepare(unitid) * missing

        if prep > largest then
            set largest = prep
        endif

        set index = index + 1
    endloop
    call TraceI("next wave will require around %d seconds to build and gather\\n",largest) //xxx

    return largest
endfunction

//============================================================================
//判断并等待直至攻击组准备好
function PrepSuicideOnPlayer takes integer seconds returns boolean
    local integer wave_prep   = PrepTime()
    local integer save_length

    set save_length = harass_length
    set harass_length = 0

    call AddSleepSeconds(seconds)
    if sleep_seconds-wave_prep > 0 then
        call TraceI("going to sleep for %d seconds before gathering next attack wave\\n",sleep_seconds-wave_prep) //xxx
        call SuicideSleep(sleep_seconds-wave_prep)
    endif

    call Trace("preparing suicide attack wave\\n") //xxx

    set harass_length = save_length
    if harass_length < 1 then
        call Trace("ERROR - no units specificed, exiting early\\n") //xxx
        return false
    endif

    return true
endfunction

//============================================================================
//等待直至攻击组抵达目的地
function SleepUntilAtGoal takes nothing returns nothing
    loop
        exitwhen CaptainRetreating()
        exitwhen CaptainAtGoal()        // 到目的地了
        exitwhen CaptainIsHome()        // 找不到路回家了
        exitwhen CaptainIsEmpty()       // 全军覆没- -
        call SuicideSleep(3)
    endloop
endfunction

//============================================================================
//等待直至攻击组结束战斗
function SleepInCombat takes nothing returns nothing
    local integer count = 0
    debug call Trace("SleepInCombat\\n")
    loop
        loop
            exitwhen not CaptainInCombat(true)  // 敌人被杀光了
            exitwhen CaptainIsEmpty()           // 我们被杀光了
            call SuicideSleep(1)
        endloop

        set count = count + 1
        exitwhen count >= 8

        //xxx 这里原定还有些东西暴雪准备在以后的补丁里做的,但结果是显然的
        //call SuicideSleep(1)
    endloop
    debug call Trace("exit SleepInCombat\\n")
endfunction

//============================================================================
//攻击组攻击点
function AttackMoveXYA takes integer x, integer y returns nothing

    if zep_next_wave then
        call LoadZepWave(x,y)
        set zep_next_wave = false
    endif

    call AttackMoveXY(x,y)
    call SleepUntilAtGoal()
    call SleepInCombat()
endfunction

//============================================================================
//等待攻击组可以攻击
function SuicideOnPlayerWave takes nothing returns nothing
    call Trace("waiting for attack wave to enter combat\\n") //xxx
    //排除一切异常状态
    loop
        //xxx
        if allow_signal_abort and CommandsWaiting() != 0 then
            call Trace("ABORT -- attack wave override\\n")
        endif

        if CaptainInCombat(true) then
            call Trace("done - captain has entered combat\\n")
        endif

        if CaptainIsEmpty() then
            call Trace("done - all units are dead\\n")
        endif

        if sleep_seconds < -300 then
            call Trace("done - timeout, took too long to reach engage the enemy\\n")
        endif
        //xxx

        exitwhen allow_signal_abort and CommandsWaiting() != 0

        exitwhen CaptainInCombat(true)
        exitwhen CaptainIsEmpty()
        call SuicideSleep(10)
        exitwhen sleep_seconds < -300
    endloop

    call Trace("waiting for attack wave to die\\n") //xxx
    loop
        //xxx
        if allow_signal_abort and CommandsWaiting() != 0 then
            call Trace("ABORT - attack wave override\\n")
        endif

        if CaptainIsEmpty() then
            call Trace("done - all units are dead\\n")
        endif

        if sleep_seconds < -300 then
            call Trace("done - timeout, took too long to reach engage the enemy\\n")
        endif
        //xxx

        exitwhen allow_signal_abort and CommandsWaiting() != 0

        exitwhen CaptainIsEmpty()
        call SuicideSleep(10)
        exitwhen sleep_seconds < -300
    endloop
endfunction

//--------------------------------------------------------------------------------------------------
//攻击玩家?
function CommonSuicideOnPlayer takes boolean standard, boolean bldgs, integer seconds, player p, integer x, integer y returns nothing
    local integer save_peons

    if not PrepSuicideOnPlayer(seconds) then
        return
    endif

    set save_peons = campaign_wood_peons
    set campaign_wood_peons = 0

    loop
        //xxx
        if allow_signal_abort and CommandsWaiting() != 0 then
            call Trace("ABORT -- attack wave override\\n")
        endif
        //xxx

        exitwhen allow_signal_abort and CommandsWaiting() != 0

        loop
            exitwhen allow_signal_abort and CommandsWaiting() != 0

            call FormGroup(5,true)
            exitwhen sleep_seconds <= 0
            call TraceI("waiting %d seconds before suicide\\n",sleep_seconds) //xxx
        endloop

        if standard then
            if bldgs then
                exitwhen SuicidePlayer(p,sleep_seconds >= -60)
            else
                exitwhen SuicidePlayerUnits(p,sleep_seconds >= -60)
            endif
        else
            call AttackMoveXYA(x,y)
        endif

        call TraceI("waiting %d seconds before timeout\\n",60+sleep_seconds) //xxx
        call SuicideSleep(5)
    endloop

    set campaign_wood_peons = save_peons
    set harass_length = 0

    call SuicideOnPlayerWave()
endfunction

//--------------------------------------------------------------------------------------------------
//等待秒数攻击指定玩家
function SuicideOnPlayer takes integer seconds, player p returns nothing
    call CommonSuicideOnPlayer(true,true,seconds,p,0,0)
endfunction

//--------------------------------------------------------------------------------------------------
//设置进攻的目标玩家<等待时间,目标玩家>
function SuicideOnUnits takes integer seconds, player p returns nothing
    call CommonSuicideOnPlayer(true,false,seconds,p,0,0)
endfunction

//--------------------------------------------------------------------------------------------------
//设置进攻的玩家的点<发起进攻等待的时间,目标玩家,x坐标,y坐标>
function SuicideOnPoint takes integer seconds, player p, integer x, integer y returns nothing
    call CommonSuicideOnPlayer(false,false,seconds,p,x,y)
endfunction

//============================================================================
//等待信号进攻?
function SuicideUntilSignal takes integer seconds, player p returns nothing
    local integer save
    local integer wave_prep = PrepTime()

    loop
        call AddSleepSeconds(seconds)
        if sleep_seconds-wave_prep > 0 then
            call SuicideSleep(sleep_seconds-wave_prep)
        endif

        set save = campaign_wood_peons
        set campaign_wood_peons = 0
        loop
            loop
                call FormGroup(5, true)
                exitwhen sleep_seconds <= 0
                exitwhen CommandsWaiting() != 0
            endloop
            exitwhen SuicidePlayer(p,sleep_seconds >= -60)
            exitwhen CommandsWaiting() != 0
            call SuicideSleep(3)
        endloop
        set campaign_wood_peons = save

        loop
            exitwhen CaptainIsEmpty()
            exitwhen CommandsWaiting() != 0
            call SuicideSleep(5)
        endloop
        exitwhen CommandsWaiting() != 0
    endloop
endfunction

//--------------------------------------------------------------------------------------------------
function SuicideOnce takes integer easy, integer med, integer hard, integer unitid returns nothing
    if difficulty == EASY then
        call SuicideUnit(easy,unitid)
    elseif difficulty == NORMAL then
        call SuicideUnit(med,unitid)
    else
        call SuicideUnit(hard,unitid)
    endif
endfunction

//--------------------------------------------------------------------------------------------------
function SuicideUnitA takes integer unitid returns nothing
    if unitid != 0 then
        call SuicideUnit(1,unitid)
    endif
    call Sleep(0.1)
endfunction

//--------------------------------------------------------------------------------------------------
function SuicideUnitB takes integer unitid, integer playerid returns nothing
    if unitid != 0 then
        call SuicideUnitEx(1,unitid,playerid)
    endif
    call Sleep(0.1)
endfunction

//--------------------------------------------------------------------------------------------------
function SuicideUnits takes integer u1, integer u2, integer u3, integer u4, integer u5, integer u6, integer u7, integer u8, integer u9, integer uA returns nothing
    call Trace("MASS SUICIDE - this script is now technically done\\n") //xxx

    call PrepFullSuicide()
    loop
        call SuicideUnitA(u1)
        call SuicideUnitA(u2)
        call SuicideUnitA(u3)
        call SuicideUnitA(u4)
        call SuicideUnitA(u5)
        call SuicideUnitA(u6)
        call SuicideUnitA(u7)
        call SuicideUnitA(u8)
        call SuicideUnitA(u9)
        call SuicideUnitA(uA)
    endloop
endfunction

//--------------------------------------------------------------------------------------------------
function SuicideUnitsEx takes integer playerid, integer u1, integer u2, integer u3, integer u4, integer u5, integer u6, integer u7, integer u8, integer u9, integer uA returns nothing
    call Trace("MASS SUICIDE - this script is now technically done\\n") //xxx

    call PrepFullSuicide()
    loop
        call SuicideUnitB(u1,playerid)
        call SuicideUnitB(u2,playerid)
        call SuicideUnitB(u3,playerid)
        call SuicideUnitB(u4,playerid)
        call SuicideUnitB(u5,playerid)
        call SuicideUnitB(u6,playerid)
        call SuicideUnitB(u7,playerid)
        call SuicideUnitB(u8,playerid)
        call SuicideUnitB(u9,playerid)
        call SuicideUnitB(uA,playerid)
    endloop
endfunction

//--------------------------------------------------------------------------------------------------
//分难度等待秒数攻击指定玩家
function SuicideOnPlayerEx takes integer easy, integer med, integer hard, player p returns nothing
    if difficulty == EASY then
        call SuicideOnPlayer(easy,p)
    elseif difficulty == NORMAL then
        call SuicideOnPlayer(med,p)
    else
        call SuicideOnPlayer(hard,p)
    endif
endfunction

//--------------------------------------------------------------------------------------------------
function SuicideOnUnitsEx takes integer easy, integer med, integer hard, player p returns nothing
    if difficulty == EASY then
        call SuicideOnUnits(easy,p)
    elseif difficulty == NORMAL then
        call SuicideOnUnits(med,p)
    else
        call SuicideOnUnits(hard,p)
    endif
endfunction

//--------------------------------------------------------------------------------------------------
function SuicideOnPointEx takes integer easy, integer med, integer hard, player p, integer x, integer y returns nothing
    if difficulty == EASY then
        call SuicideOnPoint(easy,p,x,y)
    elseif difficulty == NORMAL then
        call SuicideOnPoint(med,p,x,y)
    else
        call SuicideOnPoint(hard,p,x,y)
    endif
endfunction

//============================================================================
function ForeverSuicideOnPlayer takes integer seconds, player p returns nothing
    local integer length = harass_length
    loop
        exitwhen allow_signal_abort and CommandsWaiting() != 0
        call SuicideOnPlayer(seconds,p)
        set harass_length = length
    endloop
endfunction
[/jass]
回复

使用道具 举报

发表于 2009-12-27 14:43:28 | 显示全部楼层
[jass]

//============================================================================
//等待一直攻击直到目标死亡?
function CommonSleepUntilTargetDead takes unit target, boolean reform returns nothing
    loop
        exitwhen CaptainRetreating()
        exitwhen CaptainReadinessHP() <= 40

        exitwhen not UnitAlive(target)
        exitwhen UnitInvis(target) and not IsUnitDetected(target,ai_player)

        if not TownThreatened() then
            call AttackMoveKill(target)
        endif

        call SuicideSleep(3)

        if reform and sleep_seconds < -40 then
            if CaptainInCombat(true) then
                set sleep_seconds = sleep_seconds + 5
            else
                set sleep_seconds = 0
                call FormGroup(1,false)
            endif
        endif
    endloop
endfunction

//============================================================================
//等待一直攻击直到目标死亡?
function SleepUntilTargetDead takes unit target returns nothing
    call CommonSleepUntilTargetDead(target,false)
endfunction

//============================================================================
//等待一直攻击直到目标死亡(改良版- -似乎是撤退的时间智能+了)?
function ReformUntilTargetDead takes unit target returns nothing
    debug call Trace("ReformUntilTargetDead\\n")
    call CommonSleepUntilTargetDead(target,true)
endfunction

//============================================================================
//攻击组攻击单位
function AttackMoveKillA takes unit target returns nothing
    if target == null then
        call SuicideSleep(3)
        return
    endif

    debug call Trace("AttackMoveKillA\\n")
    call AttackMoveKill(target)
    call ReformUntilTargetDead(target)
    call SleepInCombat()
endfunction

//============================================================================
//攻击小型野怪营地
function MinorCreepAttack takes nothing returns nothing
    local unit target = GetMinorCreep()
    call SetAllianceTarget(target)
    call FormGroup(3, true)
    call AttackMoveKillA(target)
endfunction

//============================================================================
//攻击大型野怪营地
function MajorCreepAttack takes nothing returns nothing
    local unit target = GetMajorCreep()
    call SetAllianceTarget(target)
    call FormGroup(3,true)
    call AttackMoveKillA(target)
endfunction

//============================================================================
//攻击野怪营地+智能版
function CreepAttackEx takes nothing returns nothing
    local unit target = GetCreepCamp(min_creeps,max_creeps,allow_air_creeps)
    call SetAllianceTarget(target)
    call FormGroup(3,true)
    call AttackMoveKillA(target)
endfunction

//============================================================================
//联合攻击?
function AnyPlayerAttack takes nothing returns nothing
    local unit hall

    set hall = GetEnemyExpansion()
    if hall == null then
        call StartGetEnemyBase()
        loop
            exitwhen not WaitGetEnemyBase()
            call SuicideSleep(1)
        endloop
        set hall = GetEnemyBase()
    endif

    call SetAllianceTarget(hall)
    call FormGroup(3,true)
    call AttackMoveKillA(hall)
endfunction

//============================================================================
//清除扩张障碍单位?
function ExpansionAttack takes nothing returns nothing
    local unit creep = GetExpansionFoe()
    local integer x

    call FormGroup(3, true)
    if creep == null then
        set x = GetExpansionX()
        if x != -1 then
            call AttackMoveXYA(x,GetExpansionY())
        endif
    else
        call AttackMoveKillA(creep)
    endif
endfunction

//============================================================================
//  AddSiege
//============================================================================
function AddSiege takes nothing returns nothing
    call SetAssaultGroup( 0, 9, SHADE       )
    call SetAssaultGroup( 0, 9, MEAT_WAGON  )
    call SetAssaultGroup( 0, 9, MORTAR      )
    call SetAssaultGroup( 0, 9, TANK        )
    call SetAssaultGroup( 0, 9, BALLISTA    )
    call SetAssaultGroup( 0, 9, CATAPULT    )
endfunction

//===========================================================================
//  GetAllyCount
//============================================================================
//此玩家的盟友个数
function GetAllyCount takes player whichPlayer returns integer
    local integer    playerIndex = 0
    local integer    count = 0
    local player     indexPlayer

    loop
        set indexPlayer = Player(playerIndex)

        if whichPlayer != indexPlayer then
            if GetPlayerAlliance(whichPlayer,indexPlayer,ALLIANCE_PASSIVE) then
                if GetPlayerAlliance(indexPlayer,whichPlayer,ALLIANCE_PASSIVE) then
                    if GetPlayerStructureCount(indexPlayer,true) > 0 then
                        set count = count + 1
                    endif
                endif
            endif
        endif
         
        set playerIndex = playerIndex + 1
        exitwhen playerIndex == 12
    endloop

    return count
endfunction

//============================================================================
//  SingleMeleeAttack
//============================================================================
//对战攻击组下达指令函数
function SingleMeleeAttack takes boolean needs_exp, boolean has_siege, boolean major_ok, boolean air_units returns nothing
    local boolean   can_siege
    local real      daytime
    local unit      hall
    local unit      mega
    local unit      creep
    local unit      common
    local integer   minimum
    local boolean   allies

    call Trace("===SingleMeleeAttack===\\n") //xxx

    if TownThreatened() then
        call Trace("sleep 2, town threatened\\n") //xxx
        call Sleep(2)
        return
    endif

    // purchase zeppelins
    // 买一个地精飞艇
    if get_zeppelin and GetGold() > 300 and GetWood() > 100 then
        call Trace("purchase zep\\n") //xxx
        call PurchaseZeppelin()
        set get_zeppelin = false
        set ready_for_zeppelin = false
        return
    endif
    set ready_for_zeppelin = true

    // coordinate with allies
    // 联盟攻击
    set allies = GetAllyCount(ai_player) > 0
    if allies and MeleeDifficulty() != MELEE_NEWBIE then
        set common = GetAllianceTarget()
        if common != null then
            call Trace("join ally force\\n") //xxx
            if GetMegaTarget() != null then
                call AddSiege()
            endif
            call FormGroup(3,true)
            call AttackMoveKillA(common)
            call SetAllianceTarget(null)
            return
        endif
    endif

    // take expansions as needed
    // 扩张?
    if needs_exp then
        call Trace("needs exp\\n") //xxx
        set creep = GetExpansionFoe()
        if creep != null then
            call Trace("attack exp\\n") //xxx
            call SetAllianceTarget(creep)
            call FormGroup(3,true)
            call AttackMoveKillA(creep)
            call Sleep(20)
            set take_exp = false
            return
        endif
    endif

    // all-out attack if the player is weak
    // 全力攻击?
    if MeleeDifficulty() != MELEE_NEWBIE then
        set mega = GetMegaTarget()
        if mega != null then
            call Trace("MEGA TARGET!!!\\n") //xxx
            call AddSiege()
            call FormGroup(3,true)
            call AttackMoveKillA(mega)
            return
        endif
    endif

    // deny player an expansion
    // 攻击敌人分基地
    set hall = GetEnemyExpansion()
    set daytime = GetFloatGameState(GAME_STATE_TIME_OF_DAY)
    set can_siege = has_siege and (air_units or (daytime>=4 and daytime<=12))

    if hall!=null and (can_siege or not IsTowered(hall)) then

        call Trace("test player town attack\\n") //xxx

        if MeleeDifficulty() == MELEE_NEWBIE then
            set minimum = 3
        elseif allies and MeleeDifficulty() == MELEE_NORMAL then
            set minimum = 1
        else
            set minimum = 0 // HARD, INSANE, and NORMAL with no allies
        endif

        if exp_seen >= minimum then
            call Trace("do player town attack\\n") //xxx
            set exp_seen = 0
            call AddSiege()
            call SetAllianceTarget(hall)
            call FormGroup(3,true)
            call AttackMoveKillA(hall)
            return
        endif

        set exp_seen = exp_seen + 1
    endif

    // attack player's main base when siege is available
    // 可能的话攻击敌人的主基地
    if can_siege then
        call Trace("attack player's town\\n") //xxx
        call AddSiege()
        call AnyPlayerAttack()
        return
    endif

    // extended, more specific method of determining creep levels
    // 扩张?
    if min_creeps != -1 then
        call TraceI("custom creep attack %d\\n",max_creeps) //xxx
        call CreepAttackEx()
        return
    endif

    // nothing better to do, so kill a creep camp
    //打野?
    if major_ok then
        call Trace("major creep attack\\n") //xxx
        call MajorCreepAttack()
        return
    endif

    call Trace("minor creep attack\\n") //xxx
    call MinorCreepAttack()
endfunction

//============================================================================
//买一个地精飞艇
function GetZeppelin takes nothing returns nothing
    if ready_for_zeppelin then
        set get_zeppelin = true
    endif
endfunction

//============================================================================
//AI玩家的当前人口数
function FoodUsed takes nothing returns integer
    return GetPlayerState(ai_player,PLAYER_STATE_RESOURCE_FOOD_USED)
endfunction

//============================================================================
//AI玩家的当前人口上限
function FoodCap takes nothing returns integer
    return GetPlayerState(ai_player,PLAYER_STATE_RESOURCE_FOOD_CAP)
endfunction

//============================================================================
//AI玩家的剩余人口
function FoodSpace takes nothing returns integer
    return FoodCap() - FoodUsed()
endfunction

//============================================================================
//没看出什么作用
function FoodAvail takes integer base returns integer
    return GetFoodMade(racial_farm) * TownCount(racial_farm) + GetFoodMade(base) * TownCount(base)
endfunction

//============================================================================
//添加进攻单位线程函数
function BuildAttackers takes nothing returns nothing
    local integer index = 0
    local integer unitid
    local integer desire
    local integer count

    loop
        exitwhen index == harass_length

        set unitid = harass_units[index]
        set desire = harass_qty[index] + IgnoredUnits(unitid)
        set count  = TownCount(unitid)

        if count != desire then
            if not StartUnit(desire,unitid,-1) then
                return
            endif
        endif

        set index = index + 1
    endloop
endfunction

//============================================================================
//添加防守单位线程函数
function BuildDefenders takes nothing returns nothing
    local integer index = 0
    local integer unitid
    local integer qty
    loop
        exitwhen index == defense_length

        set unitid = defense_units[index]
        set qty = defense_qty[index]

        call Conversions(qty,unitid)
        call AddDefenders(qty,unitid)

        set index = index + 1
    endloop
endfunction

//============================================================================
//基础战役AI线程函数
function CampaignBasicsA takes nothing returns nothing
    local integer food_each = GetFoodMade(racial_farm)
    local integer on_wood
//清空资源分配,资源部分开始
    call ClearHarvestAI()

    if CaptainInCombat(false) then
        set on_wood = 0
    else
        set on_wood = campaign_wood_peons
    endif

    call HarvestGold(0,campaign_gold_peons)
    call HarvestWood(0,on_wood)

    if harvest_town1 then
        call HarvestGold(1,campaign_gold_peons)
        call HarvestWood(1,on_wood)
    endif

    if harvest_town2 then
        call HarvestGold(2,campaign_gold_peons)
        call HarvestWood(2,on_wood)
    endif

    if harvest_town3 then
        call HarvestGold(3,campaign_gold_peons)
        call HarvestWood(3,on_wood)
    endif
//建造人口建筑
    if do_campaign_farms and FoodUsed()+food_each-1 > food_each*(TownCount(racial_farm)+1) then
        call StartUnit(TownCount(racial_farm)+1,racial_farm,-1)
    endif

    if build_campaign_attackers then
        call BuildAttackers()
    endif

    if not CaptainInCombat(false) then
        call BuildDefenders()
    endif

    call FillGuardPosts()
    call ReturnGuardPosts()
endfunction

//============================================================================
//基础战役AI线程
function CampaignBasics takes nothing returns nothing
    call Sleep(1)
    call CampaignBasicsA()
    call StaggerSleep(1,5)
    loop
        call CampaignBasicsA()
        call Sleep(campaign_basics_speed)
    endloop
endfunction

//============================================================================
//自动初始化战役AI<人口建筑类型,设置英雄升级技能的函数>
function CampaignAI takes integer farms, code heroes returns nothing
    if GetGameDifficulty() == MAP_DIFFICULTY_EASY then
        set difficulty = EASY

        call SetTargetHeroes(false)
        call SetUnitsFlee(false)

    elseif GetGameDifficulty() == MAP_DIFFICULTY_NORMAL then
        set difficulty = NORMAL

        call SetTargetHeroes(false)
        call SetUnitsFlee(false)

    elseif GetGameDifficulty() == MAP_DIFFICULTY_HARD then
        set difficulty = HARD

        call SetPeonsRepair(true)
    else
        set difficulty = INSANE
    endif

    call InitAI()
    call InitBuildArray()
    call InitAssaultGroup()
    call CreateCaptains()

    call SetNewHeroes(false)
    if heroes != null then
        call SetHeroLevels(heroes)
    endif

    call SetHeroesFlee(false)
    call SetGroupsFlee(false)
    call SetSlowChopping(true)
    call GroupTimedLife(false)
    call SetCampaignAI()
    call Sleep(0.1)

    set racial_farm = farms
    call StartThread(function CampaignBasics)
    call StartBuildLoop()
endfunction

//============================================================================
//取消所有建造中单位
function UnsummonAll takes nothing returns nothing
    local unit bldg
    loop
        set bldg = GetBuilding(ai_player)
        exitwhen bldg==null
        call Unsummon(bldg)
        call Sleep(2)
    endloop
endfunction

//============================================================================
//  SkillArrays
//============================================================================
//对战技能升级函数
function SkillArrays takes nothing returns integer
    local integer level = GetHeroLevelAI()

    if level > max_hero_level then
        set max_hero_level = level
    endif

    if GetHeroId() == hero_id then
        return skills1[level]
    elseif GetHeroId() == hero_id2 then
        return skills2[level]
    else
        return skills3[level]
    endif
endfunction

//--------------------------------------------------------------------------------------------------
//  SetSkillArray
//--------------------------------------------------------------------------------------------------
//设置对战技能函数<第几位英雄,英雄类型>
function SetSkillArray takes integer index, integer id returns nothing
    local integer i = 1

    if index == 1 then
        if hero_id != id then
            return
        endif
        loop
            set skills1 = skill
            exitwhen i == 10
            set i = i + 1
        endloop
    elseif index == 2 then
        if hero_id2 != id then
            return
        endif
        loop
            set skills2 = skill
            exitwhen i == 10
            set i = i + 1
        endloop
    else
        if hero_id3 != id then
            return
        endif
        loop
            set skills3 = skill
            exitwhen i == 10
            set i = i + 1
        endloop
    endif
endfunction

//============================================================================
//  AwaitMeleeHeroes
//============================================================================
function AwaitMeleeHeroes takes nothing returns nothing
    if GetUnitCountDone(hero_id2) > 0 then
        set two_heroes = true
    endif
    loop
        exitwhen GetUnitCountDone(hero_id)>0 and (take_exp or (not two_heroes or GetUnitCountDone(hero_id2)>0))
        call Sleep(1)
    endloop
endfunction

//============================================================================
//  PickMeleeHero
//============================================================================
//分配标准对战英雄顺序
function PickMeleeHero takes race raceid returns integer
    local integer first
    local integer second
    local integer third
    local integer last
    local integer array heroes

    //------------------------------------------------------------------------
    if raceid == RACE_HUMAN then
    //------------------------------------------------------------------------
        set heroes[1] = ARCHMAGE
        set heroes[2] = MTN_KING
        set heroes[3] = PALADIN
        set heroes[4] = BLOOD_MAGE

    //------------------------------------------------------------------------
    elseif raceid == RACE_ORC then
    //------------------------------------------------------------------------
        set heroes[1] = BLADE_MASTER
        set heroes[2] = FAR_SEER
        set heroes[3] = TAUREN_CHIEF
        set heroes[4] = SHADOW_HUNTER

    //------------------------------------------------------------------------
    elseif raceid == RACE_NIGHTELF then
    //------------------------------------------------------------------------
        set heroes[1] = DEMON_HUNTER
        set heroes[2] = KEEPER
        set heroes[3] = MOON_BABE
        set heroes[4] = WARDEN

    //------------------------------------------------------------------------
    elseif raceid == RACE_UNDEAD then
    //------------------------------------------------------------------------
        set heroes[1] = DEATH_KNIGHT
        set heroes[2] = DREAD_LORD
        set heroes[3] = LICH
        set heroes[4] = CRYPT_LORD
    else
        set hero_id = 0
    endif

    if VersionCompatible(VERSION_FROZEN_THRONE) then // 游戏版本为冰封王座
        set last = 4
    else // 游戏版本为为混乱之治
        set last = 3
    endif
    // 使用随机数+移位进行随机排位
    set first  = GetRandomInt(1,last)
    set second = GetRandomInt(1,last-1)
    set third  = GetRandomInt(1,last-2)

    set hero_id        = heroes[first]
    set heroes[first]  = heroes[last]
    set hero_id2       = heroes[second]
    set heroes[second] = heroes[last-1]
    set hero_id3       = heroes[third]

    return hero_id
endfunction
[/jass]
回复

使用道具 举报

发表于 2009-12-27 14:44:16 | 显示全部楼层
GA娘不准发文件
  1. lalala
复制代码
回复

使用道具 举报

 楼主| 发表于 2009-12-27 14:51:42 | 显示全部楼层
rar打包……
就行了
回复

使用道具 举报

发表于 2010-5-30 09:09:49 | 显示全部楼层
苦于没有教程学,现在基本上很少关于AI的东西了,请问还有其他的演示吗?
回复

使用道具 举报

发表于 2011-1-26 11:35:51 | 显示全部楼层
太强大了!很有用
回复

使用道具 举报

发表于 2012-7-24 15:31:24 | 显示全部楼层
这个的确庞大,看完之后更加眩晕了。

再发个其他种族的准AI就更好了, 网上 AL JASS 的中文资料很少啊。
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 点一下

本版积分规则

Archiver|移动端|小黑屋|地精研究院

GMT+8, 2024-5-22 19:45 , Processed in 0.069708 second(s), 18 queries .

Powered by Discuz! X3.5

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表