Adventure Land - The Code MMORPG

Adventure Land - The Code MMORPG

Not enough ratings
Descriptive default code
By Ankagara
A more descriptive version of the default code that comes with the game for people with little to no background in scripting/coding.
   
Award
Favorite
Favorited
Unfavorite
Intro
Current guide version: First draft

I got this game because it seemed like a fun and practical way to dust off and upkeep my rusty coding skills and to learn JavaScript at the same time. I imagine this is the case for a lot of people that get this game.

So the first thing I did after I logged in and made my character, was finding out where the coding part of the game actually happened. I stumbled upon the code that comes with the game.

As I said my scripting skills are a rusty and thus I can't remember all the guidelines for good practice, but I could tell whoever wrote the default code either didn't bother with it at all or wrote it like that on purpose.

So the second thing I started doing is adding descriptive comments to various sections of code so it clearer what the intention was of each part.

Lastly I rewrote small bits for the sake of optimization and readability.

Then I figured that, even if the code is very basic, it must still be pretty daunting for anyone who has little to no background in programming and maybe even a bit off-putting. So in an effort to make it more inviting and understandable I expanded my commentary in order to give those who have little experience a general idea of what it means to write code and how it is structured.

It is by no means meant as an actual tutorial on how to program, since it doesn't even begin to cover all the facets of real programming, but it might provide an easier starting point.

I'm aware that might look a bit amateurish for more experience programmers (actual comments aren't usually so expansive, so it is a bit bloated), so I invite anyone who can help me me improve it to do so, as long as it is done as proper constructive feedback.
Before getting to the actual syntax
Following this section is my rewritten version of the default CODE. Just copy-paste the entire section to the CODE window. Below that the original CODE as a reference.

I recommend going through it carefully to verify that it does not contain any malicious code before actually using it. This might require getting some JavaScript experience in before you can actually understand what is going on functionally, but you've got to start somewhere anyway.

Unless provided officially and/or vetted and vouched for by the community, this is always a good practice. Blindy copy-pasting code and running it can be cause for serious security risks.
Descriptive default CODE
/* Learn Javascript: https://www.codecademy.com/learn/learn-javascript Write your own CODE: https://github.com/kaansoral/adventureland NOTE: If the tab isn't focused, browsers slow down the game NOTE: Use the performance_trick() function as a workaround */ //Toggle for allowing the combat part of the code to execute whilst running. var attack_mode = true /* Everything within the braces of the function combatScript() is the syntax that will make your character do things, in this case fight. */ function combatScript(){ /* To stop your character from using a potion everytime you lose a little bit of health or mana we need to make sure it only happens when you are below a minimum amount of health or mana. Note that once it executes it will always cause your character to use both HP and MP regardless of the amount of health or mana lost. */ //Current treshold for using a Health Potion var useHPTreshold = character.max_hp / 2; //Current treshold for using a Mana Potion var useMPTreshold = character.max_mp / 2; /* If your mana or health is below its treshold use a HP and MP if applicable */ if ((character.hp < useHPTreshold) || (character.mp < useMPTreshold)) { use_hp_or_mp(); } //Loot everything in your current proximity loot(); /* If you are moving, dead or not attacking don't execute the rest of the script. */ if (is_moving(character) || character.rip || !attack_mode) return; //Declare your current target var target = get_targeted_monster(); //If you currently have no target. if (!target) { //Aquire a new target and output it to the console. set_message("Targeting monster!"); target = get_nearest_monster({min_xp:100, max_att:120}); if (target) change_target(target); else { //If there are no targets available output it to the console. set_message("Nothing to target!"); return; } } //If not in attack range of current target. if (!in_attack_range(target)) { //Move half the distance towards the target. move( character.x + ((target.x - character.x) / 2), character.y + ((target.y - character.y) / 2) ); } //If in attack range, attack and output to the console. else if (can_attack(target)) { set_message("Attacking"); attack(target); } } /* To prevent the communication between the server and your gameclient from choking we need to limit how often our code runs, so the server has time to process things. Below we will determine the amount of milliseconds that need to pass before the script is allowed to execute again (this is called a cycletime). */ // Loops every 1/4 seconds a.k.a. a cycletime of 250ms var cycleTime = (1000/4); /* Everything within the braces of setInterval() is executed at the chosen interval (through the "cycletime" parameter). This is where we call the combatScript function so it starts running. */ setInterval(combatScript, cycleTime);
Original default CODE
// Hey there! // This is CODE, lets you control your character with code. // If you don't know how to code, don't worry, It's easy. // Just set attack_mode to true and ENGAGE! ​ var attack_mode=true ​ setInterval(function(){ ​ use_hp_or_mp(); loot(); ​ if(!attack_mode || character.rip || is_moving(character)) return; ​ var target=get_targeted_monster(); if(!target) { target=get_nearest_monster({min_xp:100,max_att:120}); if(target) change_target(target); else { set_message("No Monsters"); return; } } if(!in_attack_range(target)) { move( character.x+(target.x-character.x)/2, character.y+(target.y-character.y)/2 ); // Walk half the distance } else if(can_attack(target)) { set_message("Attacking"); attack(target); }
End note
I know it isn't much, but I hope it might be helpful for anyone looking to get into this game who does not have (alot) of experience in programming.

As I said, if you have any good suggestions or reworks that might help out, feel free to let me know in the comments. Especially concerning good practice and efficiency. I realise the naming scheme currently isn't consistent, since I prefer camel case, but the original used underscores and next to that I just started learning JavaScript so I don't know its full functionality, so there might be better ways to do things.
2 Comments
Ancilla Jun 4, 2020 @ 4:36am 
Hey, thanks that was really helpful!
NT Perdurabo May 13, 2020 @ 4:51pm 
how to use potions automatically not continuously.
this specific code works for tier 0 potions


//
//
// this uses hp/mp potions according to current hp. currently set to 50 above the
// amount of resource given from tier 0 potions
{
if(safeties && mssince(last_potion)<min(200,character.ping*3)) return;
var used=false;
if(new Date()<parent.next_skill.use_hp) return;
if(character.mp<character.max_mp-350) use('use_mp'),used=true;
if(character.hp<character.max_hp-250) use('use_hp'),used=true;
if(used) last_potion=new Date();
}