Vanilla 1455 Posted February 27, 2014 Share Posted February 27, 2014 Hello and welcome to the first guide for beginners (and even those who have more experience but still need some small hints and ideas). This series of guides will be about "how can I edit the source code?". While this thread is nothing you'd visit when you're starting to edit the source code for your first time, It'll help you greatly when you want to customize your source code or - as I did in vanilla - make more options for people using your core. If there's further interesting, I'll create more guides like these and even some who help people with the basic editing of the source code. At first we need our source code. It'd be working of course! All we need to edit are two files: config.h and config.cpp In this guide you'll learn how to declare a extern variable which will depend on your CONFIG-setting and can be used everywhere. At first we need to think about a new variable. Let's just start with something easy. We'll create a new gold-limit for players! That'll depend on your CONFIG so if you don't want to have such high gold limits, you can tune it down as you want. So. We've got an idea now what we want to do. Let's start with the real guide. declaring our new variable so we can use it as a new limit! You simply need to open config.cpp and there you'll find a lot of code. As you can see there are many variables declared: BYTE g_bChannel = 0; WORD mother_port = 50080; int passes_per_sec = 25; WORD db_port = 0; WORD p2p_port = 50900; ... ...and so on. At this point we can figure out how we declare our variable: We want to have it globally so the whole script can use it. That's why we declare our variable here at this point. Since it's in no function it'll be used as a global variable. Let's think.. What should we write? For this step we need to know the data types in c++. I won't go in that deep so you'll have to look on your own since it's really easy to find what datatypes can store which sizes. For your gold limit we'll use unsigned long long. long long can store very huge numbers while unsigned lets you store even higher numbers AND since no one shouldn't be able to have - gold it's the best to keep it over 0. So our line should look like this: long long yang_max; Now we should declare a standard value for our yang maximum. Why? Because if we don't edit the variable by using CONFIG (e. g. if you just don't want to set your yang limit by your CONFIG file) it'd turn out to be 0 (so: If we don't declare a specific number for long long the compile will use 0). So we set a new limit: long long yang_max: 2000000000; Next we'll edit the config.h file. There we just scroll down and add our new variable. We can just put it where the other variables (for example extern bool bXTrapEnabled;) are. But this time we won't give it a standard value and we'll add something at the beginning: extern long long yang_max; The extern will allow other scripts to use this variable. At this point we'd simply use the variable anywhere! editing the variable by using the CONFIG-file But we want to let people edit the variable by using the CONFIG-file. So back to config.cpp There we scroll down and wait! What do we have here? There are other CONFIG-options listed (e. g. TOKEN("max_level") and TOKEN("pk_protect_level"), if you don't find them just search for one of them). So maybe we'd just simply add one of these procedures for our variable, hm? Okay, let's try it. We copy the whole TOKEN-statement from "max_level" and paste it just under it. TOKEN("max_level") { str_to_number(gPlayerMaxLevel, value_string); gPlayerMaxLevel = MINMAX(1, gPlayerMaxLevel, PLAYER_MAX_LEVEL_CONST); fprintf(stderr, "PLAYER_MAX_LEVEL: %dn", gPlayerMaxLevel); } This should be there twice now. Let's edit this so we can use it for our own variable. At first we change the TOKEN. It's the thing the gamefile will look for when it processes your CONFIG-file. We change it to: TOKEN("max_yang") or whatever you want so the gamefile will fire up the following code when it reads yang_max in your CONFIG-file (now matter of lower- or uppercases). Next we change the variable name. In this case they're putting the value you specify in your CONFIG-file into the variable "gPlayerMaxLevel". But we don't want this! We want it to be in yang_max, our variable! So change it everywhere. It'd look now like this: TOKEN("yang_max") { str_to_number(yang_max, value_string); yang_max = MINMAX(1, yang_max, PLAYER_MAX_LEVEL_CONST); fprintf(stderr, "PLAYER_MAX_LEVEL: %dn", yang_max); } So now let's analyze the rest of the code so we can finish our work. In the first line (str_to_number(yang_max, value_string) the value you specify in your CONFIG-file will be written into your variable "yang_max". The second code line uses the MINMAX-function. This trims the value up or down to a specified value. So in this case, "yang_max" can't be lower than 1 (if it is, it'd be set to 1) and not higher than PLAYER_MAX_LEVEL_CONST which is a global constant. Since we don't want to have our limits like this we need to adjust at least the MAX-value. Let's change it! TOKEN("yang_max") { str_to_number(yang_max, value_string); yang_max = MINMAX(1, yang_max, 9999999999ULL); fprintf(stderr, "PLAYER_MAX_LEVEL: %dn", yang_max); } So in this scenario the yang_limit could only be a number between 1 and 9.999.999.999 I set ULL after our huge number so the compiler won't generate a warning and read it properly as unsigned long long. You can play with these numbers but make sure you don't go higher than your variable can store, so pay attention to your data type!!! Now we need to edit the last line. You may look up for the fprintf-command and you'll find out about the mysterious %d. But I'll tell it to you: %d represents the value of an integer variable you give to the function at a later time (in this case yang_max will be used). But wait! We don't just use a normal integer variable. We have unsigned long long (or to be exactly: unsigned long long int). So we need to adjust it: fprintf(stderr, "PLAYER_MAX_LEVEL: %ulldn", yang_max); %ulld stands for "unsigned long long digit". If we don't adjust this statement, the core will immediately crash when accessing this instruction. So be careful and pay attention to the warnings you get! Ad last, PLAYER_MAX_LEVEL isn't an appropriate name. fprintf prints out a message so the user can read that his CONFIG-option will be used properly. You can change it to YANG_MAX. Our command will now look like this: fprintf(stderr, "YANG_MAX: %ulldn", yang_max); And now we're finished with our config.cpp using our variable in other scripts Welcome to the last part of this guide. It's much shorter than the other parts, so you may be happy now! We're almost done. At this point we need to open the source-code we want to change. In this example we just want to use it in another source file, let's say we use it in char_battle.cpp. You can - of course - use your variable in every file, but in this example I'll just add it there. So let's open char_battle.cpp At first we need to make sure our config.h (that's where we defined our variable to be extern, you remember?) is included in this files so we can use our variable. Have a look at the first lines, there are many #include-statements. We'll look for our config.h and here we got it: In line 3. If it's not there, you need to add it! Just add the line at the end of the #include-section: #include "config.h" Next we can jump to the end of the include-section. At this point we simply tell the script to use our extern variable globally in this file (you can use the following statement only in the functions you'll use your variable too, but in this example I'll just make it global so you can use the variable everywhere in the script). So we simply add the following line under the #include-section: extern long long yang_max; Since it's extern it'll look for externally declared variables and there it is: In our config.h which uses the variable modified in config.cpp! Now we can simply use it everywhere. We can replace limitations with our new variable or even use it in another way. Try to experiment with it. I hope you enjoy the first part of the guide-series. If you want me to keep up with this, just leave a comment below. Oh, and if you have any questions, feel free to ask. 2 27 We are the tortured. We're not your friends. As long as we're not visible. We are unfixable. Link to comment Share on other sites More sharing options...
Premium Sanchez 2467 Posted February 27, 2014 Premium Share Posted February 27, 2014 Glad to see you writing tuts, it's really helpful. 1 Link to comment Share on other sites More sharing options...
Originale 134 Posted February 27, 2014 Share Posted February 27, 2014 Thank you to you VanillaIt is true that for beginners cella is a very useful thingHoping to find another tutorial Link to comment Share on other sites More sharing options...
JachuPL 309 Posted March 8, 2014 Share Posted March 8, 2014 okay, solved. Thanks for this great tutorial Link to comment Share on other sites More sharing options...
Chun-Chun 60 Posted March 8, 2014 Share Posted March 8, 2014 Thanks Vanilla. Really useful and completly new for me Link to comment Share on other sites More sharing options...
JachuPL 309 Posted March 8, 2014 Share Posted March 8, 2014 I have one problem with it: when I try to set a new value, the max number I can set is 1.410.065.407. Why? Here's what I have in src: http://pastebin.com/MBZsMDie What do I do wrong? Link to comment Share on other sites More sharing options...
Dennis 167 Posted April 1, 2014 Share Posted April 1, 2014 hm i used ingame the command /set [DEV]Name gold 3000000000 come this: whats wrong ? Link to comment Share on other sites More sharing options...
Former Staff .InyaProduction 1124 Posted April 1, 2014 Former Staff Share Posted April 1, 2014 This number is the max of int. You have to change all yang references and set it to long Link to comment Share on other sites More sharing options...
Dennis 167 Posted April 1, 2014 Share Posted April 1, 2014 i have use unsigned long long in the soruce of game config.cpp : unsigned long long gPlayerMaxYang = 2000000000; TOKEN("max_yang") { str_to_number(gPlayerMaxYang, value_string); gPlayerMaxYang = MINMAXYANG(1, gPlayerMaxYang, PLAYER_MAX_YANG_CONST); fprintf(stderr, "PLAYER_MAX_YANG: %llun", gPlayerMaxYang); } config.h: extern unsigned long long gPlayerMaxYang; length.h: PLAYER_MAX_YANG_CONST = 100000000000LLU, utils.h: extern unsigned long long MINMAXYANG(unsigned long long min, unsigned long long value, unsigned long long max); utils.c: unsigned long long MINMAXYANG(unsigned long long min, unsigned long long value, unsigned long long max) { register unsigned long long tvv; tvv = (min > value ? min : value); return (max < tvv) ? max : tvv; } when start the server Server come this: but he dosent work ingame Link to comment Share on other sites More sharing options...
Think 117 Posted April 5, 2014 Share Posted April 5, 2014 Dennis, you have to change all the packets as well as many use DWORD (unsigned long) if you want to go over the UL limit (I actually advise you against changing this, it's a lot of work, and I'm sure you don't need so much gold). 1 Link to comment Share on other sites More sharing options...
Dennis 167 Posted April 5, 2014 Share Posted April 5, 2014 Dennis, you have to change all the packets as well as many use DWORD (unsigned long) if you want to go over the UL limit (I actually advise you against changing this, it's a lot of work, and I'm sure you don't need so much gold). Thx Think but i need MORE MORE MORE trolololol DWORD in game source or client ? i think in game or ? Link to comment Share on other sites More sharing options...
Sinval 9 Posted April 14, 2014 Share Posted April 14, 2014 i have use unsigned long long in the soruce of game config.cpp : unsigned long long gPlayerMaxYang = 2000000000; TOKEN("max_yang") { str_to_number(gPlayerMaxYang, value_string); gPlayerMaxYang = MINMAXYANG(1, gPlayerMaxYang, PLAYER_MAX_YANG_CONST); fprintf(stderr, "PLAYER_MAX_YANG: %llun", gPlayerMaxYang); } config.h: extern unsigned long long gPlayerMaxYang; length.h: PLAYER_MAX_YANG_CONST = 100000000000LLU, utils.h: extern unsigned long long MINMAXYANG(unsigned long long min, unsigned long long value, unsigned long long max); utils.c: unsigned long long MINMAXYANG(unsigned long long min, unsigned long long value, unsigned long long max) { register unsigned long long tvv; tvv = (min > value ? min : value); return (max < tvv) ? max : tvv; } when start the server Server come this: but he dosent work ingame Thx Think but i need MORE MORE MORE trolololol DWORD in game source or client ? i think in game or ? "Some come to see you fall, others come pick you upSome come make you smile, others will make you crySome lie by lie, lie to deceive others " Link to comment Share on other sites More sharing options...
★ j0ĸ3rнd ★ 3 Posted April 16, 2014 Share Posted April 16, 2014 Helpful stuff. Link to comment Share on other sites More sharing options...
loyein 18 Posted January 3, 2016 Share Posted January 3, 2016 Thx useful. Link to comment Share on other sites More sharing options...
Recommended Posts