1 --[[工具准备
2 1.一个支持UTF8无BOM编码的工具,例如:notepad++
3 2.一个多文件搜索关键字的工具,例如:File Seeker
4 3.Eluna对应端的源码
5 ]]--
6
7 --[[网站相关
8 Eluna源码
9 https://github.com/eluna-dev-mangos/ElunaCoreWotlk
10 https://github.com/ElunaLuaEngine/Eluna-TC-Wotlk
11
12 示例源码
13 https://github.com/ElunaLuaEngine/Scripts
14 ]]--
15
16 --[[lua基础语法-注释
17 单行注释:--
18 多行注释:--[[注释内容]]-- --[=[注释内容可以有[]这样的符号]=]--
19 ]]--
20
21 --[==[lua基础语法-变量
22 关键字(不能做变量):
23 and break do else elseif
24 end false for function if
25 in local nil not or
26 repeat return then true until while
27 变量类型:
28 nil 空值
29 boolean 就2种,真 true / 假 false
30 number 数值,可以是带小数,十六进制0x10
31 string 字符串,如果你愿意的话,字符串是可以 包含'\0'字符的
32 table 表格,类似数组,一般用{}符号,表格可以包含多个表格,lua所以下标是从1开始
33 function 函数
34 userdata player,object,item,map,quest...
35
36 local A=10 (loacal)局部变量,只在当前文件有效
37 A=10 (默认)全局变量,在所有lua有效,会覆盖
38
39 数值:
40 local a=123456
41 local b=0x10
42 local c=3.14159265358
43 字符串:
44 local str1="123"
45 local str2="你好"
46 local str3=[[这是多行字符串
47 这是多行字符串
48 ]]
49 local str4=[=[这是多行[字符串]
50 这是多行[字符串]
51 ]=]
52 表格:
53 local T={
54 "a",
55 123,
56 {"a","b"},
57 {123,"c"},
58 b=456,
59 ["10"]=123,
60 ["asd"]="hi",
61 }
62 获取表的内容:
63 T[1]="a"
64 T[2]=123
65 T.b=456
66 T["10"]=123
67 T.asd="hi"
68 T[3][1]="a"
69 T[4][2]="c"
70 用for循环获取表
71
72 --只适用于没有用[]表明的表
73 for k,v in pairs(T) do
74 k=1,v=T[1]
75 k=2,v=T[2]
76 end
77
78 函数:
79 在lua,函数最好用loacal,减少服务端当机概率
80 local function Fun1()
81
82 end
83
84 local T={}
85
86 function T.Fun2()--因为T属于局部变量,所以T的Fun2函数也是局部变量
87
88 end
89
90 ]==]--
91
92 --[[lua基础语法-基本函数库
93 字符串:
94 local str="123456"
95 字符串连接:local str2=str.."789" = "123456789"
96 方式1:
97 string.find(str,"1")=1 查找指定字符出现的位置
98 string.sub(str,2,4)="234" 裁剪字符串
99 string.sub(str,2)="23456" 裁剪字符串
100 string.len(str)=6 字符串长度
101 string.format("%s是%d个孩子", "小明",1) ="小明是1个小孩" 字符串格式化
102 方式2:
103 str:find("1")=1
104 str:sub(2,4)="234"
105 str:sub(2)="23456"
106 str:len()=6
107 "%s是%d个孩子":format("小明",1) ="小明是1个小孩"
108
109 数值:
110 local a,b=math.modf(1/3) a=0, b=0.3333333 a是商,b是余数
111
112 时间:
113 local secs=os.time() 秒数,详细可以直接百度
114 日期:
115 local ts=os.date("*t",time)
116 local t=string.format("%d年%d月%d天%d时%d分%d秒",ts.year,ts.mon,ts.day,ts.hour,ts.min,ts.sec)
117 ]]--
118
119 --[[lua基础语法-运算
120 +-*/%^ not
121 not 一般用于真假取反
122
123 ]]--
124
125 --[[lua基础语法-判断,循环
126 判断if
127 local a=nil
128 local b=0
129 local c=false
130
131 if(表达式)then --表达式等于nil或者false为假,其他为真。如果为真,则执行then的内容,否则else内容
132
133 else
134
135 end
136
137 循环for
138 for i=1,10 do --从1到10,每次默认+1
139
140 end
141
142 for i=1,10,2 do --从1到10,每次+2
143
144 end
145
146 for i=10,1,-1 do --从10到1,每次-1
147
148 end
149
150 ]]--
151
152 --[[eluna开始
153 LuaFunctions.cpp 所有函数
154 HookMgr.h 所有event事件
155 在LuaFunctions.cpp里面的Register...函数就是给指定的游戏对象添加监视器
156 (event, function)
157 当指定的游戏对象发生你需要的event(在HookMgr.h文件查找),就会调用function
158 部分的Register需要指明的物品或者其他entry
159
160 ]]--
161
162 --eluna例子-给物品添加菜单或者功能
163 --注意:创建lua,记得在notepad++的格式菜单,选择UTF-8无BOM编码
164 --1.首先在LuaFunctions.cpp,找到物品的Register函数
165 RegisterItemEvent(entry, event, function)
166 RegisterItemGossipEvent(entry, event, function)
167 --2.然后在HookMgr.h找到菜单相关的event
168 --[[只能在RegisterItemEvent(entry, event, function)的event
169 enum ItemEvents
170 {
171 ITEM_EVENT_ON_DUMMY_EFFECT = 1, // (event, caster, spellid, effindex, item)
172 ITEM_EVENT_ON_USE = 2, // (event, player, item, target) 物品使用
173 ITEM_EVENT_ON_QUEST_ACCEPT = 3, // (event, player, item, quest) 物品接受任务
174 ITEM_EVENT_ON_EXPIRE = 4, // (event, player, itemid)
175 ITEM_EVENT_COUNT
176 };]]--
177 --只能在RegisterItemGossipEvent(entry, event, function)的event
178 --[[enum GossipEvents
179 {
180 显示菜单
181 GOSSIP_EVENT_ON_HELLO = 1, // (event, player, object) - Object is the Creature/GameObject/Item
182 选择菜单
183 GOSSIP_EVENT_ON_SELECT = 2, // (event, player, object, sender, intid, code, menu_id) - Object is the Creature/GameObject/Item/Player, menu_id is only for player gossip
184 GOSSIP_EVENT_COUNT
185 };]]--
186 --3.找到3个相关的
187 --ITEM_EVENT_ON_USE和GOSSIP_EVENT_ON_HELLO是一样的,为了方便,一般采用GOSSIP_EVENT_ON_HELLO
188 --GOSSIP_EVENT_ON_SELECT
189 --4.现在需要指定物品的entry,物品必须是能使用的。
190 local itemEntry=6948 --炉石
191 --5.创建函数
192 --GOSSIP_EVENT_ON_HELLO对应的函数参数有(event, player, object)
193 --GOSSIP_EVENT_ON_SELECT对应的函数参数有(event, player, object, sender, intid, code, menu_id)
194 --所以我们创建2个局部函数
195 local function Book(event, player, item)
196
197 end
198 local function Select(event, player, item, sender, intid, code, menu_id)
199 --根据点击的菜单的sender,intid和输入的code(是字符串),进行处理
200 end
201 --6.查找相关函数
202 --我们需要添加菜单,还有把菜单显示出来的函数,用“GOSSIP”搜索函数
203 player:GossipMenuAddItem(icon, msg, sender, intid[, code, popup, money])--添加菜单
204 player:GossipSendMenu(npc_text, unit[, menu_id])--发送菜单(显示)
205
206 --7.函数分析
207 --注意:[参数]是说明[]里面的参数可以不需要
208 player:GossipMenuAddItem(icon, msg, sender, intid)
209 --(菜单图标号,菜单项文字,sender,intid,code是否需要输入(真-需要/假-不需要),确认提示文字,提示花费铜币
210 player:GossipMenuAddItem(icon, msg, sender, intid, code, popup, money)
211 --(菜单页文字号,菜单的所有者,在这里就是item)
212 player:GossipSendMenu(npc_text, unit[, menu_id])--只有在unit=玩家的时候,menu_id才需要给个数值
213 player:GossipComplete()--关闭菜单,一般放到Select函数,菜单在点击后,你再点击也是不会触发Select函数,所以需要关闭
214 player:GossipClearMenu()--清除菜单,一般在添加菜单前使用
215 --8.查找函数内置的常量
216 --[[我们不知道icon,npc_text有哪些。
217 这个时候就是用File Seeker或者同样工具,
218 a.选择文件夹 服务端的源码(因为eluna可能调用端的常量)
219 b.选择文件类型 h cpp
220 c.输入关键字,搜索
221 ]]--
222 --GOSSIP_ICON 菜单图标
223 local GOSSIP_ICON_CHAT = 0 -- 对话
224 local GOSSIP_ICON_VENDOR = 1 -- 货物
225 local GOSSIP_ICON_TAXI = 2 -- 传送
226 local GOSSIP_ICON_TRAINER = 3 -- 训练(书)
227 local GOSSIP_ICON_INTERACT_1 = 4 -- 复活
228 local GOSSIP_ICON_INTERACT_2 = 5 -- 设为我的家
229 local GOSSIP_ICON_MONEY_BAG = 6 -- 钱袋
230 local GOSSIP_ICON_TALK = 7 -- 申请 说话+黑色点
231 local GOSSIP_ICON_TABARD = 8 -- 工会(战袍)
232 local GOSSIP_ICON_BATTLE = 9 -- 加入战场 双剑交叉
233 local GOSSIP_ICON_DOT = 10 -- 加入战场
234 --GOSSIP_OPTION
235 local GOSSIP_OPTION_NONE = 0 --UNIT_NPC_FLAG_NONE
236 local GOSSIP_OPTION_GOSSIP = 1 -- UNIT_NPC_FLAG_GOSSIP
237
238 --使用
239 player:GossipSendMenu(0, unit[, menu_id])
240 --如果声明了变量
241 player:GossipSendMenu(GOSSIP_OPTION_NONE, unit)
242 --9.注册监视函数
243 RegisterItemGossipEvent(itemEntry, 1, Book)
244 RegisterItemGossipEvent(itemEntry, 2, Select)
245 --注意:
246 --lua是从上到下的,如果下面的2个Register函数放在Book,Select上面,就会失败。