documentation.txt 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. Quest scripts define the complete or near-complete flow of events that take place from the
  2. start to the end of a quest. This includes, but is not limitted to, starting and accepting
  3. the quest, following complex dialogs, attacking or killing mobs, finding quest items, spawning
  4. quest mobs, completing a quest, and getting rewards.
  5. In order for quest scripts to function properly, at least 3 classes must be imported from Java:
  6. from net.sf.l2j.gameserver.model.quest import State
  7. from net.sf.l2j.gameserver.model.quest import QuestState
  8. from net.sf.l2j.gameserver.model.quest.jython import QuestJython as JQuest
  9. In addition, the jython library "sys" is generally imported for convinience. More classes can
  10. be imported as needed, in order to access other components of the core and enhance the abilities
  11. of the script.
  12. Jython quest scripts essentially inherit from the Java class net.sf.l2j.gameserver.model.quest.Quest
  13. Developers who are comfortable with Java may read the source code for this class, as provided by the
  14. l2jserver project (www.l2jserver.com) for full details and available functions. Alternatively, one
  15. may read forward in this documentation.
  16. AVAILABLE TRIGGER FUNCTIONS:
  17. There exist a set of functions that are predefined for quests and are triggered from various actions.
  18. These functions, their triggers, and the parameters passed into the script are defined below:
  19. 1) onAdvEvent(self, event, npc, player)
  20. This function is called whenever a player clicks on a link in a quest dialog and whenever
  21. a timer fires. Also, should other functions (see below - onTalk, onKill, etc) are not
  22. implemented, this function will be called in their place.
  23. The parameter "npc" contains a reference to the instance of NPC associated with this event.
  24. This may be the NPC registered in a timer, or the NPC with whom a player is speaking, etc.
  25. This parameter may be NULL in certain circumstances.
  26. The parameter "player" contains a reference to the player participating in this function. It
  27. may be the player speaking to the NPC, or the player who caused a timer to start (and owns
  28. that timer).
  29. This parameter may be NULL in certain circumstances.
  30. The parameter "event" contains a string identifier for the event. Generally, this string
  31. is passed directly via the link. For example:
  32. <a action="bypass -h Quest 626_ADarkTwilight 31517-1.htm">hello</a>
  33. The above link sets the event variable to "31517-1.htm" for the quest 626_ADarkTwilight
  34. In the case of timers, this will be the name of the timer. This parameter serves as a
  35. sort of identifier.
  36. The parameter "self" is a reference to the quest itself. You may use self.XXXX where XXXX is
  37. any function defined in the parent class of your quest.
  38. 2) onEvent(self, event, st)
  39. This function is called in place of onAdvEvent if the former is not implemented. If a script
  40. contains BOTH onAdvEvent AND onEvent, then onEvent will never be called unless the script's
  41. onAdvEvent explicitely calls onEvent within.
  42. The parameter "st" contains a reference to the QuestState of the player who used the link or
  43. started the timer.
  44. The parameters "event" and "self" are same as in onAdvEvent.
  45. 3) onAttack(self, npc, player, damage, isPet)
  46. This function is called whenever a player attacks an NPC that is registered for the quest
  47. The parameter "npc" contains a reference to the exact instance of the NPC that got attacked
  48. The parameter "player" contains a reference to the exact instance of the player who attacked.
  49. The parameter "damage" is a number, representing the total damage that this attack has inflicted to the NPC.
  50. The parameter "isPet" is a boolean. When false it denotes that the attacker was indeed the player.
  51. If true it specifies that the damage was actually dealt by the player's pet.
  52. The parameter "self" works the same as in onEvent.
  53. 4) onKill(self, npc, player, isPet)
  54. This function is called whenever a player kills an NPC that is registered for the quest
  55. All parameters are the same as in onAttack, lacking the damage parameter, of course.
  56. 5) onSkillSee(self, npc, caster, skill, targets, isPet)
  57. This function is called whenever a player casts a skill near a registered NPC (1000 distance)
  58. The "npc" and "isPet" parameters are same as with onAttack.
  59. the "caster" parameter references the actual instance of the player who cast the spell.
  60. The "skill" parameter is a referece to the actual skill that was used (from which info about the id and level
  61. of the skill can be obtained).
  62. The "targets" parameter is an array of all objects (can be any type of object, including mobs and players) that
  63. are affected by the skill.
  64. NOTE: if a skill does damage, BOTH onSkillSee AND onAttack will be triggered for the damaged NPC! However,
  65. only onSkillSee will be triggered if the skill does no damage, or if it damages an NPC who has no onAttack
  66. registration while near another NPC who has an onSkillSee registration.
  67. 6) onTalk(self,npc, player)
  68. This function is called whenever a player clicks to the "Quest" link of an NPC that is registered
  69. for the quest.
  70. All parameters are the same as in onAttack
  71. 7) onFirstTalk(self,npc, player)
  72. This function is called whenever a player talks to an NPC that is registered for the quest. That is,
  73. it is triggered from the very first click on the NPC, not via another dialog.
  74. NOTE 1: Each NPC can be registered to at most one quest for triggering this function. In other words,
  75. the same one NPC cannot respond to an "onFirstTalk" request from two different quests.
  76. Attempting to register an NPC in two different quests for this function will result in one of the
  77. two registration being ignored.
  78. NOTE 2: Since a Quest link isn't clicked in order to reach this, a quest state can be invalid within this
  79. function. The coder of the script may need to create a new quest state (if necessary), by using:
  80. st = self.newQuestState(player)
  81. NOTE 3: the returned value of onFirstTalk replaces the default html that would have otherwise been loaded
  82. from a subfolder of .../gameserver/data/html/...
  83. If you wish to show the default html, within onFirstTalk do npc.showChatWindow(player) and then return ""
  84. All parameters are the same as in onAttack.
  85. 8) onDeath (self, npc, character, st)
  86. This function is called whenever an exact INSTANCE of a character who was previously registered for this
  87. event dies. The registration for onDeath events is NOT done via the quest itself, but it is instead handled
  88. by the QuestState of a particular player.
  89. The parameter "npc" contains a reference to the exact instance of the NPC that KILLED the character.
  90. The parameter "character" contains a reference to the exact instance of the character that got killed.
  91. The parameter "st" contains a reference to the QuestState of whomever was interested (waiting) for this kill
  92. The parameter "self" works the same as in onEvent.
  93. 9) onSpawn (self, npc)
  94. Currently the only function that has no reference to a player. It is triggered whenever an NPC spawns or
  95. respawns and passes a reference to the newly (re)spawned NPC. It is useful for initializations, starting
  96. quest timers, displaying chat (NpcSay), and more.
  97. The parameter "npc" contains a reference to the exact instance of the NPC who just (re)spawned.
  98. 10) onFactionCall(self, npc, caller, attacker, isPet)
  99. Triggered when an npc is called by another npc in the same faction.
  100. The parameter "npc" contains a reference to the exact instance of the NPC who is being asked for help
  101. The parameter "caller" contains a reference to the exact instance of the NPC who is asking for help
  102. The parameter "attacker" contains a reference to the exact instance of the player who attacked the caller
  103. The "isPet" parameters works same as with onAttack.
  104. 11) onAggroRangeEnter(self, npc, player, isPet)
  105. This is currently here only as a place-holder. This function is NOT yet called from any part of the code.
  106. The idea is it will be called whenever a player or pet come near an aggro mob (enter its aggro range).
  107. The parameters "npc", "player", and "isPet" work similar to onAttack.
  108. 12) REGISTRATION FUNCTIONS:
  109. The functions described below have a single purpose: To register an NPC for event triggers. Simply put,
  110. an NPC must be registered in a quest for a particular event in order for the NPC to respond to the occurence
  111. of thatevent.
  112. Registration Functions are NOT called automatically. Instead, they should be added at the bottom of your
  113. quest script.
  114. Descriptions of all registration functions follow:
  115. a) addStartNpc(npcId)
  116. Registers an NPC for onTalk events. These NPCs are considered start NPCs. Therefore, the player
  117. does NOT need to have started the quest in order to access the onTalk section of the quest.
  118. Instead, when a player talks to NPCs registered by addStartNpc, then the players automatically get
  119. a CREATED state for this quest.
  120. The parameter "npcId" contains the id of the template for this NPC.
  121. b) addTalkId(npcId)
  122. Registers an NPC for onTalk events. These NPCs are not considered start NPCs (unless they are also
  123. registered with addStartNpc). NPCs registered using addTalkId will only respond to the player's
  124. click of a "Quest" link if and only if the player has alredy started the quest.
  125. The parameter "npcId" contains the id of the template for this NPC.
  126. c) addAttackId(npcId)
  127. Registers an NPC for onAttack events. The players do NOT need to be registered for the quest in order
  128. to trigger a response to an attack against this NPC.
  129. The parameter "npcId" contains the id of the template for this NPC.
  130. d) addKillId(npcId)
  131. Registers an NPC for onKill events. The players do NOT need to be registered for the quest in order
  132. to trigger a response to this NPC getting killed.
  133. The parameter "npcId" contains the id of the template for this NPC.
  134. e) addFirstTalkId(npc)
  135. Registers an NPC for onFirstTalk events.
  136. f) addSpawnId(npc)
  137. Registers an NPC for onSpawn events.
  138. g) addSkillSeeId(npc)
  139. Registers an NPC for onSkillSee events.
  140. f) addFactionCallId(npc)
  141. Registers an NPC for onFactionCall events.
  142. h) addAggroRangeEnterId(npc)
  143. Registers an NPC for onAggroRangeEnter events (remember, those events aren't yet supported and will not be
  144. triggered by core yet).
  145. 13) OVERRIDABLE QUEST FUNCTIONS:
  146. There exist a few functions which are automatically called by the core (and optionally manually called within
  147. your script) which meant to be overriden by your quest script for special needs.
  148. a) saveGlobalData()
  149. Optional function. If a quest (or ai) needs to keep track of variables that are persistant beyond a reboot
  150. but are not tied to any particular character (for example, count how many players have finished a quest in
  151. order to do some special action when the count reaches 1000), then you can override this function and add
  152. code for saving your special variabls. This function will automatically be called when the server is shutting
  153. down as well as when you attempt to reload the quest using GM commands. The actual functions used to save
  154. those variables are described in section 11, below.
  155. b) init_LoadGlobalData()
  156. Optional function. If a quest has saved global data, you will also need to implement a way to load the data
  157. back. Here, you can define everything from which variables to look for and load to the structure in which
  158. you wish to store the data (for example, you may want to format things as an array, or a dictionary, or some
  159. other structure). The actual functions used to load those variables are described in section 11, below.
  160. 14) OTHER QUEST FUNCTIONS:
  161. In addition to the trigger functions (which are called automatically by the core when certain actions take place)
  162. there also exist a variety of functions that belong to the Quest class and can be accessed within each script.
  163. To call those functions one needs to prefix the call with "self." and provide the appropriate parameters. For
  164. example, if the Quest class had a function named "foo" and it expected an integer parameter, you could call this
  165. function from anywhere within the "class Quest" segment of your script, using the code
  166. self.foo(5)
  167. The full listing of available functions can be seen in the class net.sf.l2j.gameserver.model.quest.Quest, which
  168. is located at net/sf/l2j/gameserver/model/quest/Quest.java from the l2jserver project. However, a few of the
  169. more common functions are summarized here:
  170. a) startQuestTimer(name, time, npc, player, repeating)
  171. Starts a timer. The timer will call onAdvEvent(name, npc, player) each time that it fires.
  172. The parameter name is a string which serves as the name of the timer (or "event").
  173. time refers to the number of milliseconds that the timer will wait before it fires.
  174. npc is the actual instance of an NPC or Mob assosiated with the timer. If this timer is not tied to
  175. any particular instance, you should pass None as the parameter.
  176. player is the actual instance of a player assosiated with the timer. If this timer is not tied to
  177. any particular instance, you should pass None as the parameter.
  178. repeating is a boolean (True or False) variable. If this is False, the timer will only fire once. If
  179. this is True, the timer will keep firing at equal periods (equal to the "time" parameter) for ever,
  180. unless you explicitely cancel the timer, reboot the server, or reload the quest (assuming that your
  181. script doesn't automatically start the timer again when it loads).
  182. b) getQuestTimer(name, npc, player)
  183. Allows you to grab an instance of the timer. This is primarily used behind the scenes (from core) and
  184. you would rarely really need it, but I am describing as it is somewhat important in understanding timers.
  185. The parameter name refers to the name with which a timer was added. It is the first and most important
  186. key in identifying the timer. Two timers with different name are always considered unrelated to each
  187. other. If the name is the same, the timers are further identified by the npc and player associated.
  188. npc is the second key for identifying timers. A null (None) npc acts as a wildcard! That is, if you call
  189. this function with a null npc parameter, it will consider as matches any timers with the correct
  190. name and player, regardless of which NPC was used. On the other hand, if you use a non-null npc, this
  191. function will consider as matches any timers with the same name, npc, and player as well as timers
  192. with the same name and player but null NPC.
  193. player is the last key for identifying timers. A null (None) player acts as a wildcard! That is, if you call
  194. this function with a null player parameter, it will consider as matches any timers with the correct
  195. name and npc, regardless of which player was used. On the other hand, if you use a non-null player, this
  196. function will consider as matches any timers with the same name, npc, and player as well as timers
  197. with the same name and npc but null player.
  198. Naturally, if both player and npc are null, matches are identified based solely on the name.
  199. c) cancelQuestTimer(name, npc, player)
  200. Cancels a timer. See getQuestTimer for details on how this call will match and discover which timer to
  201. cancel. If multiple timers match the parameters, only the first timer that the code discovers will be cancelled.
  202. d) saveGlobalQuestVar(var, value)
  203. This permanently (until explicit deletion) saves a variable that is global for this quest. Here, global
  204. refers to the fact that it is not tied to any particular player! It does not mean global for all quest.
  205. var is a string denoting the variable name.
  206. value is a string denoting the value of the variable "var".
  207. For storing complex structures, your script must provide code which properly breaks down the data into
  208. var/value pairs and saves it in a way that can be read back.
  209. e) loadGlobalQuestVar(var)
  210. This loads a permanently saved variable that is global for all players within this quest. It then
  211. returns the result as a string. It returns an empty string if the specified variable does not exist
  212. for this quest.
  213. var is the name of the variable you wish to load
  214. f) deleteGlobalQuestVar(var)
  215. Deletes a global quest variable for this quest (same definition of "global" as in #d).
  216. var is the name of the variable you wish to delete
  217. g) deleteAllGlobalQuestVars()
  218. Deletes ALL global quest variables for this quest (same definition of "global" as in #d).
  219. h) getRandomPartyMember(player)
  220. Returns a random player who is in the same party as the referenced parameter. If the referenced
  221. parameter is a player who is not in a party, it returns the player reference itself automatically.
  222. If the reference is null, it returns null.
  223. player is a reference to a player instance.
  224. i) getRandomPartyMember(player, var, value)
  225. Similar to #h but only returns a result among those players who have the specified var and value pair
  226. among their QuestState variables (see QuestState description below). If no party member matches these
  227. conditions, a null (None) is returned.
  228. The reference player may also be among the possible results if it also matches the conditions.
  229. Similarly to #h, if the reference player is not in a party, then only the var/value pairs are checked.
  230. In that case, if the player matches the conditions, the same player is returned, else null is returned.
  231. player: The reference player whose party will be checked
  232. var: The variable name necessary for a match. If this is null (None), then a completely random
  233. party member is returned regardless of the value (same as in #h).
  234. value: the required value of the "var" before a party member is considered for the random poll.
  235. Example usage:
  236. self.getRandomPartyMemberState(player, "cond","5")
  237. This will return a random party member among those members who have cond=5.
  238. j) getRandomPartyMemberState(player, state)
  239. Similar to #h but only returns a result among those players whose QuestState has a state matching the
  240. passed "state" variable (see QuestState description below). If no party member matches the state
  241. condition a null (None) is returned.
  242. If the reference player is not in a party, either the reference is return if it matches the state
  243. condition, or a null (None) is returned if the reference player doesn't match the state condition.
  244. For example,
  245. self.getRandomPartyMemberState(player, State.COMPLETED)
  246. will return a random party member among those members who have completed the quest.
  247. Quest:
  248. After writing your script, in order to initialize your quest and register it with the game server, near the
  249. bottom of your script, you must use:
  250. QUEST = Quest(id,"12345_QuestName","Description")
  251. For example:
  252. QUEST = Quest(626,"626_ADarkTwilight","A Dark Twilight")
  253. It is often useful to define the quest name in a variable at the top of the script (typically called "qn").
  254. In that case, you may register your quest using:
  255. QUEST = Quest(626,qn,"A Dark Twilight")
  256. In addition, you can register quest items with this quest. All registered items will be DELETED from the
  257. player's inventory, as soon as the quest is aborted or completed. Many quests reward items that are not
  258. meant to be deleted upon quest completion, even if the items appear in the quest inventory (example: star of destiny).
  259. Such items should NOT be registered as questItems. To register items with a quest, you need to modify the __init__
  260. function of the quest. For a simple quest with no registrations, the __init__ function will look something like :
  261. def __init__(self,id,name,descr): JQuest.__init__(self,id,name,descr)
  262. For a quest with registered items, this should be:
  263. def __init__(self,id,name,descr):
  264. JQuest.__init__(self,id,name,descr)
  265. self.questItemIds = [1234,5678]
  266. In this example, the items with id 1234 and 5678 are registered.
  267. QuestState:
  268. A QuestState is not part of the quest definition itself, but it contains the information that tracks the
  269. progress of a particular player for this quest. Given a player instance, the quest-state of that player for
  270. this quest can be found using:
  271. st = player.getQuestState("12345_questname")
  272. If the player does NOT have a quest-state for this quest (i.e. the player is not currently doing this quest), then
  273. st will be null.
  274. In addition, the queststate of a random party member who has a particular variable and value stored for this quest,
  275. can be discovered using:
  276. partyMember = self.getRandomPartyMember(player,"variable","value")
  277. st = partyMember.getQuestState("12345_questname")
  278. Similarly, the queststate of a random party member who has reached a particular STATE for this quest,
  279. can be discovered using:
  280. partyMember = self.getRandomPartyMemberState(player,STATE)
  281. st = partyMember.getQuestState("12345_questname")
  282. For example, instead of "variable" and "value" in the first sample, one may use "cond" and "1". Instead of STATE in
  283. the second sample, one may use State.STARTED
  284. While a QuestState can be discovered from a player, it can also access the player instance back when needed, using
  285. st.getPlayer()
  286. All other public methods of implementation of QuestState are accessible from jython. Similarly, objects
  287. reachable from st can be further used in order to reach deeper. For example, one may do something like:
  288. st.getPlayer().getClan().getLeader().getPlayerInstance().getPet()
  289. (this example may not be fully accurate or may get deprecated in the future...it is only meant as a
  290. little demonstation of how one may reach deeper into a chain of objects that are accessible. In this
  291. case, from the QuestState, we get the player whose QuestState this is, then get that player's clan,
  292. the clan's leader, the leader's actual player instance, and from there, we find the leader's summonned pet!)
  293. State:
  294. States are used to track segments of the quest for players. States are not a property of any particular quest and
  295. States Types cannot be added or removed dynamically. They are a mere enumeration used universally by all quests.
  296. Each QuestState carries a State value which can be compared to the available (global) state types in order to check
  297. the progress that the given character has with regards to the given QuestState (see QuestState info above).
  298. The available state types are:
  299. CREATED
  300. STARTED
  301. COMPLETED
  302. For example, given an instance of a QuestState (st) for a given player doing some quest, one can check if the
  303. player has completed the quest by doing the comparison:
  304. st.getState() == State.COMPLETED