![]() |
|
|
#1 |
|
Архивный участник
Join Date: Apr 2006
Posts: 65
|
[glow=red:39f92fb459]Warning: Use this tutorial at your own risk. There MAY or MAY NOT be spelling errors, terminology errors, or other errors within it. I will try my best to keep it updated. Thanks.[/glow:39f92fb459]
I was asked by someone (who won't be named) *cough*Enishi*cough* to write a tutorial on how to write a quest in NASC. First off, a little disclaimer: This tutorial is designed for beginners with NASC. It is NOT designed for people with a knowledge of stack or anything similar, so posts declaring "OMG U R NUB, Y R U POSTEENG DIS?" will be ignored. I will provide help within the topic (and so should others). If you are having issues, please post the entire problem in the topic. For the most part, this tutorial will be written with the idea in mind that the reader has absolutely no coding background whatsoever. Now, the complex quests (more of the 70-80 level stuff) are too complex (duh) and too long to be done in the span of an hour or so. We'll start off with something easy, in this case, the quest "Yoke of the Past", which is used for giving "Blank Scrolls" to players for use with Mammon's trader. Let's get started! In NASC, you need what's called a "class" definition. A class definition gives the AI you're about to write a name for the server to recognize it by. A class definition generally looks like this: Code:
class ziggurat_gatekeeper A superclass is defined by using a colon, seen below: Code:
class ziggurat_gatekeeper : default_npc Anyways, we've now learned the classing/superclassing idea, sort of. Now, if you have any experience with MultiSell (and if you're reading this, by God you better have), you'll know that brackets are needed to start/stop where a line begins and ends, correct? Same applies to AI. So, let's add some beginning and ending brackets to our class. Code:
class ziggurat_gatekeeper : default_npc
{
}
There are two (in this case we'll use two), mm, "sets" we'll use to determine each part of the AI. I use the term "sets" as not to confuse people. They really are nothing but certain parts of AI split up into different sections depending on their content. Anyways, first off is the "parameter" set. It defines things such as HTML's the NPC will use, certain number statistics (all constant, meaning you cannot change them later within the AI), and other various jargon. For now, we'll need three HTML's: 1) The "Hello" HTML, or the HTML that is shown to the user when the target is first talked to. It has the teleport and quest links. 2) The "Quest Description" HTML, which describes the blank scroll quest to the user, and provides the quest accept button. 3) The "Quest Accepted" HTML, which tells the user the quest was accepted. 4) A page that tells the player that they already have the quest. 5) An HTML if the player tries to trade 0 scrolls 6) An HTML if the player successfully traders scrolls. Seeing as HTML's are "strings", which is a nerd/coder term for "text" (in a nutshell), we will have to define each parameters as strings. So our code looks like this: Code:
class ziggurat_gatekeeper : default_npc
{
parameter:
string fnHi = "ziggurat_hi.htm";
string fnQD = "ziggurat_quest.htm";
string fnQA = "ziggurat_quest2.htm";
string fnQAl = "ziggurat_already.htm";
string fnQNo = "ziggurat_none.htm";
string fnQThx = "ziggurat_thanks.htm";
}
Let's do a quick break down of each line I just added: Code:
parameter: Code:
string Code:
fnHi Code:
= "ziggurat_hi.htm"; Alright, enough of that. Now we need to add another 3 parameters, the teleport params. These will tell the NPC which X,Y, and Z coordinate to port the player to. It's quite simple, and I'm not going to break this one down for you. The term "int" is short for integer, or for you idiots, a NON-decimal number. Code:
class ziggurat_gatekeeper : default_npc
{
parameter:
string fnHi = "ziggurat_hi.htm";
string fnQD = "ziggurat_quest.htm";
string fnQA = "ziggurat_quest2.htm";
string fnQAl = "ziggurat_already.htm";
string fnQNo = "ziggurat_none.htm";
string fnQThx = "ziggurat_thanks.htm";
int port_x = 12345;
int port_y = 12345;
int port_z = 12345;
}
So, we have to define the "set", just as we did with parameter. It's the same case, just add the word "handler:" to your AI. Done? Good. Now the fun stuff begins. In this case, we are going to be using THREE handlers. The "MENU_SELECTED", "QUEST_ACCEPTED", and "TALKED" handlers. They will tell the NPC what to do when a certain event occurs. Ok! Let's go! So, a handler generally looks like this: Code:
EventHandler TALKED() {
}
In this case, the "TALKED" handler will tell the NPC what to do once the person, or "talker" (remember this word), talks to it. Remember the "talker" word? That's a variable we're going to be using. Why? Because I said so, and because the NPC will have to talk to multiple people, so a variable fits perfectly. So, remember where variables go? That's right, in the parentheses. So, add it. Code:
EventHandler TALKED(talker) {
}
So, we've got an empty handler. What's that? What do we do in it? Well, I'd thought you'd never ask. We need to add the actual code that tells the NPC what to do! Let's get started. We're going to want to show the player the "Hello" page. Remember how we defined the hello page back up in the parameter section? Well, this is why. To show a player an HTML page, you need to use the "ShowPage();" function. The ShowPage function will tell the NPC to show the player the page that you want them to see. The ShowPage(); has two "parameters" to itself. One being who the page is directed two, and the other being which page to show. In this case, our person is "talker", or the person talking to the NPC, and our page is "fnHi", which we defined in the parameter set. So, we have "ShowPage(talker, fnHi);". What to do with it? Well, dork, add it into the handler! Code:
EventHandler TALKED(talker) {
ShowPage(talker, fnHi);
}
Oh, what's that you say? The semicolon, what's that doing there? In a lot of languages, the semicolon, or ";", tells the parser (or in this case compiler) that I'm done with this function or definition, please move onto the next. Ok, one handler down, two to go! We have the handlers to finish, the compiling to do, and finally the mob AI to work with (which will be your homework). Don't worry, I'll give you some help at the end. The next handler we'll be using is the QUEST_ACCEPTED handler. It will tell the NPC what to do when the user accepts the "Yoke of the Past" quest. So, write the new handler out. Code:
EventHandler QUEST_ACCEPTED() {
}
Now, with quests, in order to keep track of which player is at what point in the quest, L2NPC has a nice little set of functions all relating to "memo's". Think of them as little notes your mother or father pinned onto you as you were going to school, to tell your teacher's something important. They stick on you and help the NPC's to remember who the hell you are, and why you should bother talking to them. Here's a quick rundown of the memo functions: Code:
SetMemo(character, memoname); SetMemoState(character, memoname, number); GetMemoState(character, memoname); HaveMemo(character, memoname); SetMemoState, sets the memo's "state", or "message" if you will, on the character (in this case talker). GetMemoState, gets the memo's state, on the character (in this case talker). And finally, HaveMemo, which proves true (or 1) if the character even has a memo for the quest. The "memoname" is the name of the quest. We'll use "@yoke_of_the_past" for this. Yes, there is an "@" sign in front. Don't question it. So, after the player clicks the link that says "Yes, I'll do the quest", we'll need to set a memo on him so the NPC's can see that, hey, he's got the quest active, and they can do stuff to him. No, not that kind of stuff. Perverts. First, we need to check if he already has the quest. We can do this by using the "HaveMemo(talker, @yoke_of_the_past);" function, and checking to see if it equals one. We'll need to setup the if clause. Inside the parentheses, we'll need the operand and the variables. Code:
if (HaveMemo(talker, @yoke_of_the_past) == 1) {
ShowPage(talker, fnQAl);
}
Anyways, he clicked the link, and he doens't have the quest. We'll now need to set a memo on his ass. Second, we need to check the quest_id against the one he clicked on in the HTML. We'll do this by using an "if-then-else" statement, and seeing if it checks out. For this, we'll need to first declare the else if statement. The else is added to tell the NPC that, if the original "if" clause (the one that's up above checking to see if he already has the quest), to go ahead and run another check, and not to do both if one comes up true. Code:
else if () {
}
Code:
else if (quest_id == 385) {
}
If you did it right, you're handler SHOULD look something like this: Code:
EventHandler QUEST_ACCEPTED(quest_id, talker) {
if (HaveMemo(talker, @yoke_of_the_past) == 1) {
ShowPage(talker, fnQAl);
}
else if (quest_id == 385) {
SetMemo(talker, @yoke_of_the_past);
SetMemoState(talker, @yoke_of_the_past, 1);
ShowPage(talker, fnQA);
}
}
Woohoo! Two down, one to go. The next handler is called "MENU_SELECTED". It has two variables of it's own, "ask" and "reply". These are defined in the NPC HTML's, which we'll get to later. Set the handler up. No, I'm not going to explain how to do it again, you should know by now. Great, handler all setup. This one is a bit more tricky, as we have to do multiple if-then-else statements. We'll need one to manage teleportation, and another to manage the quest description page. As such, we'll use this structure: Ask = 1 Replies: - 1 = "I want info on the quest". - 2 = "I want teleported into the catacomb". - 3 = "I want to trade my scrolls for blanks". So, make four if-statements. They should look like this: Code:
if (ask == 1) {
if (reply == 1) {
}
if (reply == 2) {
}
if (reply == 3) {
}
}
As for the second, we have to teleport the player into the catacomb. This is where we use the "InstantTeleport();" function. It teleports a player to a specific X,Y,Z coordinate. Remember why we declared the port_x, y, and z variables at the beginning? So this class can be copied, renamed, and used multiple times, so you don't have to write a new class for each teleporter. More on that later. The "InstantTeleport();" function has four "parameters", or things that go inside of it (perverts). They are: InstantTeleport(character, x, y, z); X,Y, and Z must be integers (remember, non-decimal numbers), and the character, well, a character (or "actor", as it's called in the Unreal Engine). For character, we'll use talker, and for x, y, z, we'll use port_x, port_y, and port_z, respectively. As for the third, in order to trade the "Scroll of Ancient Magic" items for "Blank Scrolls", we'll first need to count them and trade the items. This can be done with two functions, "DeleteItem1" and "GiveItem1". They have the same parameters, only one deletes items and the other one gives. The parameters are below: GiveItem1(character, @item, number); DeleteItem1(character, @item, number); Where @item corresponds to the name of the item in your itemdata, and matching PCH. We will also make use of one more function, "OwnItemCount". It will count the number of items a player has on them and return them to another variable, called "i0", which stands for "integer" variable number "0". This is a little differnt from what we've done, so I'll walk you through. OwnItemCount has two parameters within it. They are: OwnItemCount(character, @item); Where @item corresponds to the name of the item in your itemdata, and matching PCH. First, we'll need to get the item count. We'll do this by "setting" i0 to whatever OwnItemCount comes up to. We'll then take that item count, delete the items off of the character, and give the same amount of scrolls to the person. To do this, i0 will have to be used in THREE, yes, THREE functions. It's ok, it can handle that. If you're smart, your code will look something like this: Code:
if (ask == 1) {
if (reply == 1) {
ShowPage(talker, fnQD);
}
if (reply == 2) {
InstantTeleport(talker, port_x, port_y, port_z);
}
if (reply == 3) {
i0 = OwnItemCount(talker, @q_scroll_of_ancient_magic);
if (i0 > 0) {
DeleteItem1(talker, @q_scroll_of_ancient_magic, i0);
GiveItem1(talker, @blank_scroll, i0);
ShowPage(talker, fnQThx);
}
else if (i0 == 0) {
ShowPage(talker, fnQNo);
}
}
}
Code:
EventHandler MENU_SELECTED(talker, ask, reply) {
if (ask == 1) {
if (reply == 1) {
ShowPage(talker, fnQD);
}
if (reply == 2) {
InstantTeleport(talker, port_x, port_y, port_z);
}
if (reply == 3) {
i0 = OwnItemCount(talker, @q_scroll_of_ancient_magic);
if (i0 > 0) {
DeleteItem1(talker, @q_scroll_of_ancient_magic, i0);
GiveItem1(talker, @blank_scroll, i0);
ShowPage(talker, fnQThx);
}
else if (i0 == 0) {
ShowPage(talker, fnQNo);
}
}
}
}
Congratulations. You just wrote yourself your first piece of AI. If everything went as planned, if should look like this: Code:
class ziggurat_gatekeeper : default_npc
{
parameter:
string fnHi = "ziggurat_hi.htm";
string fnQD = "ziggurat_quest.htm";
string fnQA = "ziggurat_quest2.htm";
string fnQAl = "ziggurat_already.htm";
string fnQNo = "ziggurat_none.htm";
string fnQThx = "ziggurat_thanks.htm";
int port_x = 12345;
int port_y = 12345;
int port_z = 12345;
handler:
EventHandler TALKED(talker) {
ShowPage(talker, fnHi);
}
EventHandler QUEST_ACCEPTED(quest_id, talker) {
if (HaveMemo(talker, @yoke_of_the_past) == 1) {
ShowPage(talker, fnQAl);
}
else if (quest_id == 385) {
SetMemo(talker, @yoke_of_the_past);
SetMemoState(talker, @yoke_of_the_past, 1);
ShowPage(talker, fnQA);
}
}
EventHandler MENU_SELECTED(talker, ask, reply, i0) {
if (ask == 1) {
if (reply == 1) {
ShowPage(talker, fnQD);
}
if (reply == 2) {
InstantTeleport(talker, port_x, port_y, port_z);
}
if (reply == 3) {
i0 = OwnItemCount(talker, @q_scroll_of_ancient_magic);
if (i0 > 0) {
DeleteItem1(talker, @q_scroll_of_ancient_magic, i0);
GiveItem1(talker, @blank_scroll, i0);
ShowPage(talker, fnQThx);
}
else if (i0 == 0) {
ShowPage(talker, fnQNo);
}
}
}
}
}
There are these annoying files in your "Scripts" dir called PCH's, which link the quests and such to the AI parser. In the questpch, you'll need to add: Code:
[yoke_of_the_past] = 385 Code:
385 1 5902 Code:
item_begin questitem 5902 [q_scroll_of_ancient_magic] item_type=questitem slot_bit_type={none} armor_type=none etcitem_type=none blessed=0 weight=0 default_action=action_none consume_type=consume_type_normal initial_count=1 maximum_count=1 soulshot_count=0 spiritshot_count=0 immediate_effect=1 price=0 default_price=0 item_skill=[none] critical_attack_skill=[none] material_type=paper crystal_type=none crystal_count=0 is_trade=0 is_drop=0 is_destruct=1 physical_damage=0 random_damage=0 weapon_type=none can_penetrate=0 critical=0 hit_modify=0 avoid_modify=0 dual_fhit_rate=0 shield_defense=0 shield_defense_rate=0 attack_range=0 effective_range=0 damage_range={} attack_speed=0 reuse_delay=0 mp_consume=0 magical_damage=0 durability=100 damaged=0 physical_defense=0 magical_defense=0 mp_bonus=0 category={} enchanted=0 html=[item_default.htm] equip_pet={0} magic_weapon=0 item_end
item_begin etcitem 5965 [blank_scroll] item_type=etcitem slot_bit_type={none} armor_type=none etcitem_type=none blessed=0 weight=100 default_action=action_none consume_type=consume_type_normal initial_count=1 maximum_count=1 soulshot_count=0 spiritshot_count=0 immediate_effect=1 price=0 default_price=1000 item_skill=[none] critical_attack_skill=[none] material_type=wood crystal_type=none crystal_count=0 is_trade=1 is_drop=1 is_destruct=1 physical_damage=0 random_damage=0 weapon_type=none can_penetrate=0 critical=0 hit_modify=0 avoid_modify=0 dual_fhit_rate=0 shield_defense=0 shield_defense_rate=0 attack_range=0 effective_range=0 damage_range={} attack_speed=0 reuse_delay=0 mp_consume=0 magical_damage=0 durability=90 damaged=0 physical_defense=0 magical_defense=0 mp_bonus=0 category={} enchanted=0 html=[item_default.htm] equip_pet={0} magic_weapon=0 item_end
Code:
[q_scroll_of_ancient_magic] = 5902 [blank_scroll] = 5965 Why do you need to add your AI to the BOTTOM of the text file I provided? Well, remember how we used a default_npc superclass? The compiler won't compile unless it sees the superclass there. Also, be SURE to make a backup of your current AI, because NASC will overwrite it!!! Also, be sure to save in UNICODE. The compiler won't compile if you save in ANSI! To compile, put NASC, the text file with the AI, and all your other scripts into ONE folder. Then, pull up the "command" prompt (by going to Start>Run>"cmd"). Navigate to the folder with your AI and NASC, and type the following: "nasc.exe myai.txt" If all went according to plan, you should have a text file coming up and saying "Success!!!". You can now view your neatly compiled AI in your AI.obj. If it has a bunch of "push_const", "fetch_i", and similar funky looking things, you were successful! Append the AI to your AI.obj on your server, change an NPCDATA to use the AI (npc_ai={[ziggurat_gatekeeper]}). If you got errors while compiling, POST your code and the error that it's giving. We now need to write the HTML's. We have six HTML's to write. "ziggurat_hi.htm" "ziggurat_quest.htm" "ziggurat_quest2.htm" "ziggurat_already.htm" "ziggurat_none.htm" "ziggurat_thanks.htm" For each, let's start off with the basic HTML (if you don't know HTML, buzz off.). Note: These WILL NOT BE SIMILAR TO RETAIL. If you want retail HTML's, go make them yourself. Each should contain at least: Code:
<html><head><body> Ziggurat GateKeeper: </body></html> Code:
<html><head><body> Ziggurat GateKeeper: Welcome to the <insert necropolis here>. Would you like to teleport, or help me with my quest? <a action="bypass -h menu_select?ask=1&reply=1">Whats the quest about?</a> <a action="bypass -h menu_select?ask=1&reply=3">I need to trade my scrolls.</a> <a action="bypass -h menu_select?ask=1&reply=2">I'd like to port please.</a> </body></html> Code:
<html><head><body> Ziggurat GateKeeper: I need to be human. Blah blah blah. Would you like to help me in my quest by collecting ancient scrolls of magic? Thx! <a action="bypass -h quest_accept?quest_id=385">Accept the quest</a> </body></html> Code:
<html><head><body> Ziggurat GateKeeper: Thx. Go hunt for the scrolls! </body></html> Code:
<html><head><body> Ziggurat GateKeeper: Noob, you already have the quest. </body></html> Code:
<html><head><body> Ziggurat GateKeeper: I need scrolls. What are you trying to do, scam me? </body></html> Code:
<html><head><body> Ziggurat GateKeeper: Thanks, here, take these blank scrolls. I have no usage for them. </body></html> CONCLUSION: You have just had a small taste at using NASC to make AI. However, I DO NOT suggest you use this AI on your server, as there are still problems with it as well. There is no level check, quest amount check, and various other tidbits. I hope this will inspire you to learn more about AI and NASC, and to eventually help the community grow. If you found this tutorial useful, send 10 or so "Cash" (PP Cash) to me, so I know you like it. If you use this tutorial to jumpstart you into coding AI, please let me know, and please help the community grow by sharing knowledge and experience as I have just done. A community who works together is a community who grows together. If you refuse to share, you are only helping to demolish the private community's spirit. Credits go to: - Enishi, for making me stay up 2 hours and type this. - Eanae, for not staying up 2 hours and typing this. - kvoxi, without whom we wouldn't have a patched NASC - hami, who inspired me to start on AI in the first place - The wonderful makers of Dr. Pepper, who kept me awake during this. If you have trouble understanding the syntax or are completely new to it altogether, I sincerely suggest going to http://www.php.net and learning PHP. Not only can it help with L2 and understanding similar language syntax, but it has wonderful opportunities if you learn it and a great community supporting it! I also suggest downloading kvoxi's patched NASC, located here and reading through the list of functions. Other references: - RipperX's NASC documentation, which includes parameters for all the functions, located here - Aikeleer's ziggurat AI, which I suggest you look at and learn from as well. It's located here ------------------------------------------------- Included files: Please note, the NASC INFO file is NOT MY WORK! ALL credit goes to RipperX!!!!!! Вложения из исходного сообщения (сами файлы в копии отсутствуют): nasc_info_289_173.rar — 3083 байт thestuff_182_195.rar — 311914 байт |
|
|
|
|