• There is NO official Otland's Discord server and NO official Otland's server list. The Otland's Staff does not manage any Discord server or server list. Moderators or administrator of any Discord server or server lists have NO connection to the Otland's Staff. Do not get scammed!

Learning C/C++ - Tutorial

Fallen

Freelancer
Senator
Joined
Aug 21, 2009
Messages
3,712
Reaction score
249
Location
Egypt
After years of studying how OpenTibia works and how the community look like, I finally decided to write tutorials on how to code and understand the structure of OpenTibia
using C NOT C++. Since there's not many people over here who know how to edit source code or fix a simple bug themselves (This tutorial does not explain that ofcourse, but
it should give you a start on how to). I used C because it's a good start to programming.

Programming in C (basics)- Part 1.
After reading this tutorial, you'll be able to create simple console programs that's like any other console program (i.e get output/input from a user, etc.).

Some QA anyone would like to know before learning (Psuedo-code-like):
Q: How does a C program look like?
A: a simple C program would like this:
C++:
// A Comment.
/* a
multi
line
comment
*/
include "somefile.h"
int main() {
    // ...
    return 0;
}

Q: How do I compile a C program?
A: By downloading any C/C++ IDE (i.e DEV-C++, Code::Blocks, etc.) or if you prefer using command line, download MinGW. (protip: stick to IDEs for now if youre not using linux)
Please read Evan's post, he wrote a nice tutorial on how to get started with Microsoft Visual C++ which is the best IDE for C++ : Learning C/C++ - Tutorial

Now, let's begin:
A simple console program to start with
C++:
#include <stdio.h>
int main() {
    printf("Hello world!\n");
    return 0;
}

Explanation of each line of the previous program:
C++:
#include <stdio.h>
This line tells the compiler that we'll be using the functions from the file "stdio.h" which is input/output library in C.
C++:
int main()
A C program contains functions and variables. This is the main function of our program (basically the overall logic of the program), if this function is not found, the program will fail to compile, ofcourse there are command line parameter's passed but I don't want to make it complicated yet.
C++:
{
This is a paranthesis, it's important to have opening paranthesis at every start of a function.
C++:
printf("Hello world!\n");
This function is from the included file "stdio.h" prints out "Hello world!" to our console. the \n prints out new line.
C++:
return 0;
Exits the program with code 0.
C++:
}
Function paranthesis end.

Now, that was a good start how to create a simple console program that outputs stuff on console, yes you can call this function many times and test it out ;-)
Variables
A variable is just a named area of storage that can hold a single value (numeric or character). The C language demands that you declare the name of each variable that you are going to use and its type, or class, before you actually try to do anything with it.
NOTE: the variable name is case-sensitive, that means, i is NOT the same as I, and k is NOT the same as K.
Psuedo code example:
data_type var_name;


Data Types
C++:
int (integer numbers) -> int i = 5;
float (floating point numbers) -> float f = 5.0;
double (big floating numbers) -> double d = 500000000;
char (character) -> char c = 'c';

Every data type of the has have modifiers which are: short, long, signed, unsigned. The modifier defines the storage allocated to the variable:
Ranges:
C++:
Type      ->  from    -> to
short int ->  -32,768 -> +32,767
unsigned short int -> 0 -> +65,535
unsigned int -> 0 -> +4,294,967,295
int -> -2,147,483,648 -> +2,147,483,647
long int -> -2,147,483,648 -> +2,147,483,647

signed char -> -128 -> +127
unsigned char -> 0 -> +255
float ->
double ->
long double ->

Every data type of the above has qualifier(s) which is const (constant), const can be written in many ways i.e:
C++:
const int i = 5;
int const i = 5;
The const qualifier tells the compiler that the variable will NOT be modified after the initialization, that means you cannot do:
i = 10;

Functions in C
Full functions explanation, how to make your own function and make use of it.

A Psuedo function would look like:
C++:
data_type function_name(parameter1, parameter2)
{
    function_body, you write stuff here.
}

A real function would look like:
C++:
int add(int first, int second)
{
    int _add = first + second;
    return _add;
}

A function has a return value depends on what data type it uses, for example, a function declared with datatype int, then it must return int or the compiler will complain.

Using the function you've made in the main routine (calling it!):
C++:
#include <stdio.h>
int add(int first, int second)
{
    return first + second;
}

int main()
{
    int a = add(5, 6); // a now is 11.
    printf("Adding 5 to 6: %d\n", a); // Try reading documentation of these C library functions to understand how exactly they work ;-)
    return 0;
}

Operators in C
C Operators are (That's not everything, just what you need for now!):

Comparison operators/relational operators
C++:
a == b -> if a is equal to b.
a != b -> if a is not equal to b.
a > b -> if a is greater than b.
a < b -> if a is smaller than b.
a >= b -> if a is greater than or equal to b.
a <= b -> if a is smaller than or equal to b.

Arithmetic operators:
C++:
a =[/color] b -> basic assignment.
++a -> increment a
--a -> decrement a
a + b -> add
a - b -> substract

Compound assignment operators:
C++:
a +=[/color]b -> same as a = a + b;
a -= b -> same as a = a - b;
a *= b -> same as a = a * b;
a /= b -> same as a = a / b;

Pointers and Strings in C
First i'm going to discuss what a pointer is, as a string depends on pointers.
Pointer is 2 things:
1. Reference:
A reference is represented by the (&) operator.
Say we have the following psuedo code:
C++:
first = &second;
"first" now is the address of "second" that means we are no longer talking about the content of "first" itself but about its reference,
(i.e, it's address in memory) - Don't worry if you don't understand yet.
2. Dereference:
A dereference is represented by the (*) operator.
Say we have the following psuedo code:
C++:
second = *first;
And say we have the following real code:
C++:
int a;
int *deref;

deref = &a; // store the address of a in a deref pointer.
*deref = 1; // a = 1; how? we stored the address before in the dereference pointer.
printf("a = %d\n", a); // outputs "a = 1"
Yet again, don't worry if you don't understand it fully.

Strings in C:
A String type-name is "char". I explained before in the data types:
char c = 'c'; // that's only a single char! not a full string.
To assign a single character, you must use: ' and not " just like what I did above.
To assign a string, you must use " and not '.
A String in C looks like this:
C++:
char *string = "hello world"; // able to reassign it
can also be const:
C++:
const char *string = "hello world"; // cannot be re-assigned
For more information on strings, see: C Tutorial - Strings and Text Handling

Using the "if", "else if", "else", "for", "do", "break", "continue", "switch", and "while"
The "if", "else", and "else if" statements:
Let's see some psuedo-code:
C++:
if (I didnt write this tutorial) {
    some people wouldnt know how to do their own programs.
}
Real code:
C++:
int main() {
int num = 2;
if (num == 1) {
    // false!
} else if (num == 0) {
    // false!
} else {
    // true.
}
return 0;
}

The "for", "do", "while", and "while" statements;
These statements are used for looping with conditions.
Let's see examples:

The for loop:
C++:
int main() {
    int i;
    for (i = 0; i < 5; i++) { // increment i by 1, each time it loops.
    printf("i now is: %d\n", i);
    }

    // let's try something else to do with i:
    for (i = 0; i < 100; i += 10) { // increment i by 10 each time it loops.
        printf("i now is: %d\n", i);
    }

    // set i's value to 100, and check everytime it loops if i reached 0 as well as decrease i by 10 each time.
    for (i = 100; i == 0; i -= 10) {
        printf("i now is: %d\n", i);
    }
    return 0;
}
Basically the for loop psuedo would look like this:
C++:
for (start value; condition; increase/decrease value) {
    // do work
}

an infinite loop would look like this:
C++:
for (;;) {
    // do work
}

The while loop:
C++:
int main() {
    int i = 100;
    while (i > 0) {
        --i;
        printf("i now is: %d\n", i);
    }

    i = 100; // reassign i, since we changed it in the previous loop!
    int j = 200;
    while (j > i) {
        ++i;
        printf("i: %d, j: %d\n", i, j);
    }

    return 0;
}

Basically the while loop psuedo would look like this:
C++:
while (condition) {
    // code.
}

an infinite loop would look like this:
C++:
while (1) {
    // code.
}

The do-while loop:
C++:
int main() {
    int i = 0;
    do {
        ++i;
        printf("i now is: %d\n", i);
    } while (i < 100);
}

The break and continue statements:
C++:
int main() {
    int i;
    for (i = 0; i < 20; ++i) {
        if (i == 19) {
            break; // break loop here, makes it not continue anymore.
        } else if (i == 17) {
            ++i; // increment i
            continue; // stop it here and then re-run the loop, so i now is 19 because we incremented it manually and the loop will do the same aswell.
        }

        // here we write some useless stuff that will be excuted if i < 17 and < 19.
        printf("i is: %d\n", i);
    }
}

The switch statement:
C++:
int main() {
    int i = 5;

    switch (i) {
    case 1:
        printf("i is 1.");
        break;
    case 2:
        printf("i is 2.");
        break;
    default:
        printf("i is not 1 or 2.");
        break;
    }
    return 0;
}

Basically the switch statement psuedo would look like this:
C++:
switch (variable) {
    incase var is 1:
        // do work.
        break;
    default: // var is not any of the cases above.
        // do work.
        break;
}

Getting input from user in a C program.
Using the function scanf is the best way for C beginners to get inputted stuff from user, Let's see an example:
C++:
#include <stdio.h>
int main() {
    int input = 0;

    // loop until the user has guessed the right number which is 5.
    while (input != 5) {
        printf("Write a random number between 1 and 10: ");
        scanf("%d", &input);
        printf("\n"); // write a newline to seperate text written on console.  printf does not write it by default.
    }

    printf("You guessed it right!\n");
    return 0;
}

Logical Operators
In order to learn how logical operations work, have a look at these tables:
Considering A and B are inputs and out is the output:

1. operator AND (&)
A B out
1 1 1
0 1 0
1 0 0
0 0 0

That may have looked alittle weird, but to make it simple the AND operator is more like a multiplication operation.
Example usage in C or C++:
C++:
int i = 1;
printf("%d\n", i & 0); // 0

2. operator OR (|):

A B out
1 1 1
0 1 1
1 0 1
0 0 0

Therefore, operator OR loves ones, so whenever it finds a 1 it steals it.
Example usage:
C++:
int i = 1;
printf("%d\n", i | 0); // 1

Some excersies to do based on what you have learned:
Excersie 1: Write a program that asks the user for his name, age, city, etc. and output them on console!
Excersie 2: Write a program that asks the user for your 5 favourite drinks (coca-cola, beer, ...), note that he must write his choice by entering a number between 1 and 5.
If your program uses if statements, try and use switch.
Modify your program so that if the user enters a choice other than 1 and 5 then it will output some error.
Excersie 3: Write a program that asks the user the numbers he want to calculate & using what operation (+, -, /, *, ...) - can be done with switch statements and characters.

I am open to questions and suggestions. Everyone is welcome to post his opinion.
 
Learning C/C++ - Tutorial (Advanced) - Part 2

Programming in C (advanced) - Part 2.

Note: Try to read the comments I make on my example codes, they're useful ;-)
Contents:
1. Data Structures
2. Using Preprecessors
3. Data Structure arrays (using data types)
4. Creating own data type (using typedef)
5. String-plays, tips & tricks
6. Getting familiar with C Standard library (opening files, creating files, etc.)

1. Data Structures
A data structure is an arrangement of data in a computer's memory or even disk storage. An example of several common data structures are arrays, linked lists, queues, stacks, binary trees, and hash tables.
A data structure is used in C by the keyword "struct", ex:
[cpp]struct mydata {
// data...
}[/cpp]

Assuming you have read and understood the basics of C, let's check out an example of storing data in your structure named "person":
C++:
struct person {
    int age;
    char *name;
    char *city;
};

Using your structure in a real code would be something like this:
C++:
// define our structure
struct person {
    int age;
    char *name;
    char *city;
};

int main() {
    struct person axel; // creating new person
    // asign data to it:
    // the "." operator is a new one here, we use it for setting/getting values in a data structure, like this:
    axel.age = 16;
    axel.name = strdup("Axel");
    axel.city = strdup("Some city");

    printf("age: %d, name: %s, city: %s\n", axel.age, axel.name, axel.city); // "age: 16, name: Axel, city: Some city"
    return 0;
}

Using structure with a dereferencing operator "*" as described in the previous tutorial.
C++:
#include <stdlib> // new header! this header allows us to allocate memory to our structure, so that the pointer can be valid.
#include <stdio.h>
#include <string.h>
// define out structure
struct person {
    int age;
    char *name;
    char *city;
};

int main() {
    struct person *axel;
    axel = (struct person *)malloc(sizeof(struct person)); // Read more about malloc by hovering on the function!

    // the "->" operator is a new one here, we use it for setting/getting values in a data structure, like this:
    axel->age = 16;
    axel->name = strdup("Axel");
    axel->city = strdup("Some city");

    printf("age: %d, name: %s, city: %s\n", axel->age, axel->name, axel->city); // "age: 16, name: Axel, city: Some city"
    return 0;
}

2. Using preprecessors
Preprecessors in C begin with "#" and are: #define, #if, #ifdef, #endif, #ifndef, #if !defined, #pragma, etc (I believe there are more but I don't remember any at the moment as they're not used alot)

#if and #ifdef must always end with #endif, i.e:

The following code checks if the linker has TESTING_DIRECTIVES = 1
C++:
#if TESTING_DIRECTIVES
#define USING_DIRECTIVES 1 // constant value.
#endif

The following code checks if the linker has TESTING_DIRECTIVES defined, doesn't matter what it's value is:
C++:
#ifdef TESTING_DIRECTIVES
#define USING_DIRECTIVES // constant value.
#endif

Checking if the linker has something undefined is by simply using #ifndef (which is if not defined or #if !defined(VARIABLE)). i.e:
C++:
#ifndef CROX_H
#define CROX_H
// ...
#endif
or:
C++:
#if !defined(CROX_H)
#define CROX_H
// ...
#endif

using #pragma:
C++:
#pragma message("Hai there") // Outputs "hai there" during compilation.

Using #define - the most effecient way
#define can be used for defining constant values and also defining constant functions
Some examples:

The following code defines "some_value" as an integer constant that cannot be edited later on:
C++:
#define some_value 15

The following code defines a function:
C++:
#define add(x, y, z)    x + y + z;

The following code defines usage of strings in a define macro:
C++:
#define output(string)    puts(##string);

The following code defines how to get variable name in a define macro:
C++:
#define get_var_name(var)    #var;

Still don't get it? Let's see a real example:
C++:
#include <stdio.h>

#define getVarNamePassed(var)    #var
#define DEBUG_PRINT(msg)    puts("Debug: "); puts(##msg)

#ifndef USE_SOME_LIB
#define NO_LIB
#endif

void _(const char *variable) { DEBUG_PRINT(getVarNamePassed(variable)); }

int main() {
#ifdef _WIN32 // builtin variable defined for windows.
    DEBUG_PRINT("Windowsfag detected");
#elif defined(__APPLE__) // builtin variable defined for OS X
    DEBUG_PRINT("Youre using OS X");
#else
    DEBUG_PRINT("Youre using linux");
#endif

#ifdef NO_LIB
    DEBUG_PRINT("Not using lib X");
#endif
    _("ha");
    return 0;
}

3. Data Structure arrays (using data types)
A Data array is declared like this:
type_name variable_name[array_size_is_int];

Let's have a look at a real example:
C++:
#include <stdio.h>

int main() {
    char names[10];
    // Let's assign some values to our array "names".
    // Syntax is:
    // array[index] = key;
    // Samething as LUA but here index starts at 0 and not 1.
    names[0] = "Fallen";
    names[1] = "Talaturen";
    names[2] = "Three Magic";
    names[3] = "Cykotitan";
    names[4] = "Chojrak";
    names[5] = "Korrex";
    names[6] = "NewCFag";
    names[7] = "Someone";
    names[8] = "Hello.png";

   
    int i = 0;
    for (/* exclude the i = 0; since we already initialised it, just put a semicolon */; i < 8; ++i)
        puts(names[I]);

    int numbers[5];
    numbers[0] = 1;
    numbers[1] = 2;
    numbers[2] = 3;
    numbers[3] = 4;
    numbers[4] = 5;

    i = 0; // reset i's value.
    for (; i < 5; ++i)
        printf("%d\n", i);

    return 0;
}
[/I]
4. Creating own data type (using typedef)
Creating own data types is very easy. The syntax of typedef is:
typedef existing_type_name new_type_name_of_your_choice;

In C, typedef is mostly used for structures, just to shorten the name from using it, i.e:
instead of:
C++:
struct data d;
you do:
C++:
typedef struct data data;
data d;

A real example for using typedef would be:
C++:
typedef unsigned int u_int;
So instead of typing such a long typename, "unsigned int var;", you do: "u_int var;".

5. String-plays, tips & tricks
Now for some tips and tricks with strings, playing around with strings is very easy and fun at the same time. Let's get to code!

The following code replaces all the spaces in a string with an empty char:
C++:
#include <stdio.h>
#include <ctype.h>

int main() {
    char *str = "hello, my nick name is fall en";
    char *tmp = str;
    char *tmp1 = str;
    tmp = str;

    puts(str);
    while (*tmp) { // loop each character in the string and search for spaces.
        // you can also use "if (*str == 'c') { where 'c' is the character, in this situation ' ' and ','. 
        if (ispunct(*tmp) || isspace(*tmp)) // punct/space? 
            ++tmp;
        else
            *tmp1++ = *tmp++;
    }

    *tmp1 = 0;
    puts(str);
}

Will be adding more as soon as I come up with some ideas ;-)

6. Getting familiar with C Standard library (opening files, creating files, etc.)
C Standard library has very good & useful libraries, i.e time, IO (input/output) etc. Read more here: The C Standard Library
In this section, I'll explain how to use important functions and how to make a good use of them.

C++:
#include <assert.h>
assert(expr):
Assert performs a check on the expression expr, if debug mode is enabled, the program immediately exits with a message like "Asseration failed <expression>, file: somefile.c, line: 10".

Ex:
C++:
assert(pointer != NULL);

C++:
#include <signal.h>
signal(signal_name, handler)
signal is a very useful function. Usage of the function:
* Handling segmentation faults
* Handling abnormal termination
and alot more that are not used alot.

Usage in real code:
C++:
#include <signal.h>
#include <stdio.h>

void handler(int sig)
{
    puts("Seg fault received.");
    // handle it here.
}

int main() {
    // try some bugged code that raises a seg fault (can use raise(SIGSEGV) aswell for testing)
    struct buggedstruct { int x; };
    struct buggedstruct *unallocated;

    // setup signal.
    if (signal(SIGSEGV, handler) == SIG_ERR)
        puts("Failed to setup signal SIGSEGV!");

    unallocated->x = 5; // "Seg fault received".
    return 0;
}

C++:
#include <stdarg.h>
void va_start(va_list ap, ...);
void va_end(va_list ap);

This library is useful for making your own string formatter function, like printf() from stdio.h and such

C++:
#include <stdarg.h>
#include <stdio.h>

void strformat(const char *format, ...) {
    va_list arg;
    va_start(arg, format);
    char buffer[1024]; // random size, try and make an exact size ;-)
    vsprintf(buffer, format, arg);
    va_end(arg);
}

int main() {
    char *str[1024]; // random size, try and make an exact size ;-)
    strformat("Hi, this is a %s string, learn how to format %s", "Test", "String");
    puts(str);
    return 0;
}
tip: go and read more about the "%" stuff in strings.

C++:
#include <stdio.h>
Now for the input/output header library. I'll explain the important parts you should know only.
FILE *fopen(const char *filename, const char *mode)
filename: string, file name to open.
mode: i.e, read only ("r"), write only ("w"), etc...


int fclose(FILE *stream);
close and free memory of "stream"


int fprintf(FILE *stream, const char *format, ...);
int rename(const char *oldname, const char *newname);
int remove(const char *filename);
size_t fread(void *ptr, size_t size, size_t nobj, FILE *stream); binary reading.
char *fgets(char *s, int n, FILE *stream);


Usage:
C++:
#include <stdio.h>

int main() {
    FILE *f;
    char buffer[1024];

    f = fopen("file.txt", "r");
    if (!f)
        // failed to open file, handle error.

    while (fgets(buffer, sizeof(buffer), f))
        // read new stuff, parse it.

    // close file (free memory)
    fclose(f);
    return 0;
}

C++:
#include <errno.h>
Useful for errors (error numbers).
Tip: Have a look at "strerror(int errno)" in string.h for error strings.
Tip: Have a look at "perror()" in stdio.h for printing out error strings with custom messsages.

C++:
#include <string.h>
Now this is the most useful header library after stdio.h to study, it's too long to explain here! read a documentation instead.

C++:
#include <stdlib.h>
One more useful library file, for allocating memory etc. Too long to explain aswell.
 
Learning C/C++ - Tutorial (Advanced-the beginning of understanding OpenTibia) Part 3.

Sorry if the title is confusing, couldn't post the full name because it's too long. I hope someone can make use of the set of tutorials i'm making... Here goes part 3 of the tutorial:

Programming in C and C++ (Advanced - the beginning of understanding OpenTibia codes)- Part 3.

Contents:
1. C/C++ - Header files. how do they work?
2. C/C++ - Switching from C to C++ (Types difference, declarations, defintions)
3. C/C++ - Main function parameters
4. C/C++ - The type qualifer: volatile
5: C++ - New type name: bool
6. C++ - classes
7. C++ - STD
8. C++ - STL
9. C++ - Own operators
10. C++ - Subclassing abstract classes and re-defining virtual functions.
11. C++ - The meaning of a static variable/function.
12: C++ - templates
13. C++ - Exceptions
14. C++ - iterators
15. A Brief idea of how OT code work (we'll be using TFS).

1. C/C++ - Header files. how do they work?
C/C++ headers suffix usually is ".h" or ".hpp", using header files is pretty simple process, all you do there is declare stuff and then define them in the source file.
Simple header file declaration:

myheader.h:
C++:
#ifndef MYHEADER_H
#define MYHEADER_H

// define a data structure.
typedef struct m_struct {
    int index;
    char *str;
} MyStruct;

extern void setIndex(MyStruct *s, int index);
extern void setString(MyStruct *s, char *str);

int index(MyStruct *s);
char *string(MyStruct *s);
#endif // MYHEADER_H

mysource.c:
C++:
#include "myheader.h"
#include <string.h> // strdup()

void setIndex(MyStruct *s, int index) {
    if (!s)
        return;

    s->index = index;
}

void setString(MyStruct *s, char *str) {
    if (!s)
        return;

    s->str = strdup(str);
}

int index(MyStruct *s) {
    if (!s)
        return -1;

    return s->index;
}

char *string(MyStruct *s) {
    if (!s)
        return -1;

    return s->string;
}

main.c:
C++:
#include "myheader.h"
#include <stdio.h>
#include <stdlib.h>

int main() {
    MyStruct *s = (MyStruct *)malloc(sizeof(MyStruct));
    setIndex(s, 1);
    setString(s, "hello world");

    // do some stuff ;)
    return 0;
}

2. C/C++ - Switching from C to C++ (Types difference, declarations, defintions)
Switching from C to C++ is an easy process, they're both same thing but ill be explaining the difference.

3. C/C++ - Main function parameters
Have you ever wondered how a program like apt-get on linux parses the command line parameters passed to it? Well, here goes, this is how but ofcourse not the same thing, just
an example on how it works, do the rest yourself! There are also another way of parsing command line parameters but stick with this one for now! ;-)

myprogram.c:
C++:
#include <stdio.h>
#include <string.h>

int main(int argc, char **argv)
{
    // argc -> argument count
    // argv -> argument values (char array).

    if (argc > 1) { // the user has passed other parameters than the program name, confused? The operating system automatically sends the program name to the command line parameters
        // say we want to get the program name:
        printf("%s: You've passed an argument to the command line!", argv[0]);

        // Now, let's parse the arguments the user has passed, we'll be accepting "-install" and "-uninstall" for now.
        for (int i = 1/* 1 for escaping program name!*/; i < argc; ++i) {
            char *currentArgument = argv[I];
            if (!currentArgument) {
                puts("currentArgument == NULL");
                return 1;
            }

            if (strcmp(currentArgument, "-install") == 0) {
                char *programToBeInstalled = (argc >= i+1 ? argv[i+1] : NULL);
            } else if (strcmp(currentArgument, "-uninstall") == 0) {
                char *programToBeUnInstalled = (argc >= i+1 ? argv[i+1] : NULL);
            } else {
                printf("Invalid argument passed!  Accepting only -install and -uninstall, what you passed is: %s", currentArgument);
                return 1;
            }
        }
    }

    return 0;
}

That's it.[/I]
4. C/C++ - The type qualifer: volatile
Soon to come, I find no use for it now.

5: C++ - New type name: bool
Before i've explained what a type name in C is, there's a new one in C++ called boolean, a boolean type name can only be "true" or "false".
i.e:
C++:
bool _bool = true;
if (_bool) {
    // success
}

6. C++ - classes
Classes in C++ is a long thing to discuss, I'll try to explain it as short and clear as possible.

Classes in C++ are like struct in C, but classes in C++ allow you to make the function either private/protected (not accessiable outside the class) or public (accessible outside).
The word "this" in C++ classes refer to the class itself, a pointer in the class, that can only be used inside the class functions.
A C++ Class consists of a constructor, destructor, public/private/protected functions:
1. Can have multiple constructors but only 1 destructor
2. Can have no constructor and no destructor.
3. A constructor can be private or public.
4. Destructor must be public.
5. using delete on the class with no destructor should throw an errow (As far as I remember correctly).

An example how a C++ class would look like:
C++:
class Person {
public:
    // multiple constructor example
    Person(int age);
    Person(char *name);
    Person(char *city);

    // Just 1.
    Person(int age, char *name, char *city);

    // Destructor.
    ~Person();

    // example of some public functions.
    int age() const;
    char *name() const;
    char *city() const;

    void setAge(int age);
    void setName(char *name);
    void setCity(char *city);

private: // private members
    int m_age;
    char *m_name, *m_city;
};

// definition of a class.
Person::person(int age)
    : m_age(age)
{}

Person::person(char *name)
    : m_name(name)
{}

Person::person(char *city)
    : m_city(city)
{}

Person::person(int age, char *name, char *city)
    : m_age(age),
      m_name(name),
      m_city(city)
{}

int Person::age() const
{
    return m_age;
}

char *Person::name() const
{
    return m_name;
}

char *Person::city() const
{
    return m_city;
}

void Person::setAge(int age)
{
    m_age = age;
}

void Person::setName(char *name)
{
    m_name = name;
]

void Person::setCity(char *city)
{
    m_city = city;
}

Simple, isn't it?
7. C++ - STD
C++ Standard Library consists, of alot of stuff (converted from C Standard library to fit with the C++ style), i.e: C time.h, C stdio.h, to include them:
C++:
#include <c(standard c lib)>
i.e:
C++:
#include <cstdio>
Also has the C++ string, easier to use than C strings, class name is apart of the C++ standard namespace which is (std). Let's check it out an example of how C++ strings work:
C++:
#include <string>

int main() {
    std::string str = "hello world";
    // can also be declared like this:
    std::string str;
    str = "hello world";
}
C++ STD also has it's own IO (Input/output) Library, which provides us useful, easier to use functions for outputting/inputting stuff to/from the console. (the header file is iostream).
Let's see an example for output stream:
C++:
#include <iostream> // C++ STD input output.
#include <string> // C++ STD string.

int main() {
    std::cout << "Hello world!"; // print out "Hello world!"
    // using C++ STD strings:
    std::string str = "Hello world!";
    std::cout << str;
    return 0;
}
for input stream:
C++:
#include <iostream>
#include <string>

int main() {
    std::string inputstring;
    std::cout << "Enter your name: ";
    std::cin >> inputString; // std::cin is the function for inputting! note the operator >> aswell!
    std::cout << "Hello, " << inputString;
    return 0;
}

The Normal C++ String does not support an operator to concat integers to it by default. But the C++ STD library provides us a class called "stringstream". A stringstream
is a class for strings to put stuff into it (including strings, integers, floats, doubles, etc...).
Example:
C++:
#include <sstream> // stringstream.
#include <string>
#include <iostream>

int main() {
    std::stringstream stringsteam;

    // Let's put some numbers into the stream and output it to console.
    stringstream << "Hello world " << 1 << " " << 2 << " "<< 3 << " " << 4 << " " << 5;
    std::cout << stringstream.str(); // -> "Hello world 1 2 3 4 5".

    // You can also make a new c++ string and assign the data of stringstream to it:
    std::string str;
    stringstream >> str;
    // str = "Hello world 1 2 3 4 5"
    return 0;
}

That's all for now.

8. C++ - STL
I'll be explaining only STL containers (vector, map, hashmap, hash, set, stack, list, bitset, ...) and not STL algorithms for now.
Let's start off with vector (
C++:
#include <vector>
): Vector is a template class for creating data storage instead of using C style structures, read more at:
vector - C++ Reference (functions etc.)

Initialising a C++ vector:
C++:
std::vector<type_name> my_vector;
A vector is stored like this:
C++:
type_name vec[] = {
    stuff
};
Real example:
C++:
#include <vector>
#include <iostream>

int main() {
    std::vector vec<int> position(3);
    // assign some integers to it.
    position[0] = 1000; position[1] = 1024; position[3] = 8;

    // output the values of the vector.
    for (int i = 0; i < position.size(); ++i)
        std::cout << position; // -> 1000, 1024, 8

    return 0;
}
--
C++ Map (
C++:
#include <map>
):
Init a C++ map:
C++:
std::map<typename_key, typename_value>
Example:
C++:
#include <map>
#include <iostream>

int main() {
    std::map<int, int> positionMap;

    for (int x = 0; x < 100; ++x) {
        for (int y = 0; y < 200; ++y)
            positionMap.insert(std::make_pair(x, y));
    }

    // iterating through the map.
    for (std::map<int, int>::const_iterator it = positionMap.begin();
            it != positionMap.end(); ++it) {
        // output values.
        std::cout << "x: " << (*it).first() << " y: " << (*it).second();
    }

    return 0;
}

More to come soon.
9. C++ - Own operators
Making own class operators or editing an existing class operator. for example let's edit the "<<" operator std::cout to output some typename that is not builtin or supported by
STD IO.

Syntax of outter-class operator is:
C++:
typename operator[actual operator here](typename varname, newtype var);
Real example:
C++:
std::eek:stream &operator<<(std::eek:stream &os, const MyPosition &p);
Assuming MyPosition is a class like this:
C++:
class MyPosition {
public:
    MyPosition(int x, int y, int z)
        : _x(x), _y(y), _z(z)
    {}
    // no destructor.

    int x() const { return _x; }
    int y() const { return _y; }
    int z() const { return _z; }

private:
    int _x, _y, _z;
};
Now for the operator definition body:
C++:
std::eek:stream &operator<<(std::eek:stream &os, const MyPosition &p) {
    os << "[x: " << p.x() << ", y: " << p.y() << ", z: " << p.z() << "]";
    os << std::endl; // new line! in STD.
    return os;
}
Usage of our new operator:
C++:
#include <iostream>

// definition of our operator goes here.
int main() {
    MyPosition pos(100, 200, 7);
    std::cout << pos; // -> [x: 100, y: 200, z: 7]
    return 0;
}

Try make your own, if it doesn't work, have a look at mine and compare:
C++:
#include <iostream>

class MyPosition {
public:
    MyPosition(int x, int y, int z)
        : _x(x), _y(y), _z(z)
    {}
    // no destructor.

    int x() const { return _x; }
    int y() const { return _y; }
    int z() const { return _z; }

private:
    int _x, _y, _z;
};

std::eek:stream &operator<<(std::eek:stream &os, const MyPosition &p) {
    os << "[x: " << p.x() << ", y: " << p.y() << ", z: " << p.z() << "]";
    os << std::endl; // new line! in STD.
    return os;
}

int main() {
    const MyPosition pos(100, 200, 7);
    std::cout << pos; // -> [x: 100, y: 200, z: 7]
    return 0;
}

--
Making an operator for your own class
C++:
class Point2D {
public:
    Point2D(int x, int y)
        : _x(x), _y(y)
    { }

    // copy-constructor.
    // so it can be used like this:
    // Point2D point(1, 1);
    // Point2D anotherPoint(point)
    Point2D(const Point2D &p) {
        operator=(p); // calls the operator.
    }

    // no need for destructor.

    int x() const { return _x; }
    int y() const { return _y; }

    // operator =
    // usage:
    // Point2D point(1, 1);
    // Point2D anotherPoint(2, 2);
    // point = anotherPoint; // point = {2, 2}
    Point2D &operator=(const Point2D &p) {
        _x = p.x();
        _y = p.y();

        return *this;
    }

private:
    int _x, _y;
};

// create a simple addition operator:
static Point2D operator+(const Point2D &p1, const Point2D &p2) { return Point2D(p1.x() + p2.x(), p1.y() + p2.y()); }
Try making your own stuff, if it doesn't work compare it to my code:
C++:
#include <iostream>

class Point2D {
public:
    Point2D(int x, int y)
        : _x(x), _y(y)
    { }

    // copy-constructor.
    // so it can be used like this:
    // Point2D point(1, 1);
    // Point2D anotherPoint(point)
    Point2D(const Point2D &p) {
        operator=(p); // calls the operator.
    }

    // no need for destructor.

    int x() const { return _x; }
    int y() const { return _y; }

    // operator =
    // usage:
    // Point2D point(1, 1);
    // Point2D anotherPoint(2, 2);
    // point = anotherPoint; // point = {2, 2}
    Point2D &operator=(const Point2D &p) {
        _x = p.x();
        _y = p.y();

        return *this;
    }

private:
    int _x, _y;
};

// outside class operator.
static Point2D operator+(const Point2D &p1, const Point2D &p2) { return Point2D(p1.x() + p2.x(), p1.y() + p2.y()); }

std::eek:stream &operator<<(std::eek:stream &os, const Point2D &p) {
    os << "[x: " << p.x() << ", y: " << p.y() << "]";
    os << std::endl; // new line! in STD.
    return os;
}

int main() {
    Point2D p1(100, 200);
    p1 = p1 + Point2D(100, 200); // add to it another {100, 200} point.
    // overall result: {200, 400}.

    std::cout << "[operator+]" << p1;

    // using copy-constructor.
    Point2D p2(p1); // p2 is now a copy of p1.
    std::cout << "[copy-constructor]p2 now is: " << p2;

    // using operator =
    Point2D p3 = p2; // p3 is now a copy of p2 which is a copy of p1.
    std::cout << "[internal-operator=]p3 now is: " << p3;
    return 0;
}
10. C++ - Subclassing abstract classes and re-defining virtual functions.
Coming soon.
11. C++ - The meaning of a static variable/function.
Coming soon.
12: C++ - templates
Coming soon.
13. C++ - Exceptions
Coming soon.
14. C++ - iterators
Coming soon.
15. A Brief idea of how OT code work (we'll be using TFS).
Coming soon.
 
great tutorials! :D
 
Gonna learn from this :)

I'll repost in 10 years time and let you know how great your tutorial was <3
 
after many fucking fails
and alot of help from fallen
i created this simple shit
from dust
yes, dust
 
Bomping this up, 440 views and few replies?! only Three Magic has interest in learning?!!!!

P.S: If you encourage an english mistake or some error in code, let me know, I written this when semi-sleepy (bored basically)!
 
Really nice tutorial, thanks for sharing with us :$
btw what mean these compound assignment operators: "|=", "&="?
 
Really nice tutorial, thanks for sharing with us :$
btw what mean these compound assignment operators: "|=", "&="?
I'm 100% sure I haven't explained how those work yet (note that the tutorial is incomplete, more to come later)
 
I'm looking forward to learn, great contribution Fallen! :)
 
I'm well experienced, I just don't have any interest in using C++ in OpenTibia.

Here's something I made a very very long time ago:

main code:
Code:
#include <stdio.h>
#include <iostream>
#include "gameMazeTestHeader.h"
#include <Windows.h>
using namespace std;

void printMap(int readMap[15][15], int mapValue)
{
	for (int x = 0; x < 15; x++)
	{
		for (int y = 0; y < 15; y++)
		{
			if (readMap[x][y] == 1) // MOUNTAIN
			{
				SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), DARKGREY);
				cout << "#";
			}
			else if (readMap[x][y] == 0) // WALKABLE SPACE
			{
				cout << " ";
			}
			else if (readMap[x][y] == 3) // TREE
			{
				SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), GREEN);
				cout << "^";
			}
			else if (readMap[x][y] == 4) // CHEST
			{
				SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), CYAN);
				cout << "+";
			}
			else if (readMap[x][y] == 5) // EMPTY CHEST
			{
				SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), CYAN);
				cout << "-";
			}
			else if (readMap[x][y] == 2) // DOOR
			{
				SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), CYAN);
				cout << "|";
			}
			else if (readMap[x][y] == 9) // USER
			{
				SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), YELLOW);
				cout << "&";
				posStoreY = y;
				posStoreX = x;
			}
		}
		cout << endl;
	}
	cout << endl << "X: " << posStoreY << ", Y: " << posStoreX << " ON MAP: " << mapValue << endl;
}

void movePlayer(int direction, int readMap[15][15], int &prePosX, int &prePosY)
{
	switch (direction)
	{
	case UP:
		{
			// UP -------------------------------------------------------
			if (readMap[prePosX - 1][prePosY] == 2)
			{
				if (keyValue == 1000)
				{
					SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), LIGHTGREY);
					cout << "You see:" << endl;
					readMap[prePosX][prePosY] = 0;
					mapValue = 2;
				}
				else
				{
					SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), LIGHTRED);
					cout << "You see: LOCKED DOOR" << endl;
					return;
				}
			}
			else if (readMap[prePosX - 1][prePosY] == 1 || readMap[prePosX - 1][prePosY] == 3 || readMap[prePosX - 1][prePosY] == 4
				|| readMap[prePosX - 1][prePosY] == 5)
			{
				if (readMap[prePosX - 1][prePosY] == 3)
				{
					SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), LIGHTGREY);
					cout << "You see: TREE" << endl;
					return;
				}
				else if (readMap[prePosX - 1][prePosY] == 4)
				{
					readMap[prePosX - 1][prePosY] = 5;
					SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), GREEN);
					cout << "You see: CHEST [you found a key]" << endl;
					keyValue = 1000;
					return;
				}
				else if (readMap[prePosX - 1][prePosY] == 5)
				{
					SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), LIGHTRED);
					cout << "You see: EMPTY CHEST" << endl;
					return;
				}
				else
				{
					SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), LIGHTGREY);
					cout << "You see:" << endl;
					return;
				}
			}
			else
			{
				SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), LIGHTGREY);
				cout << "You see:" << endl;
				readMap[prePosX][prePosY] = 0;
				prePosX -= 1;
				readMap[prePosX][prePosY] = 9;
			}
			break;
		}
	case DOWN:
		{
			// DOWN -------------------------------------------------------
			if (readMap[prePosX + 1][prePosY] == 2)
			{
				if (keyValue == 1000)
				{
					SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), LIGHTGREY);
					cout << "You see:" << endl;
					readMap[prePosX][prePosY] = 0;
					mapValue = 2;
				}
				else
				{
					SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), LIGHTRED);
					cout << "You see: LOCKED DOOR" << endl;
					return;
				}
			}
			else if (readMap[prePosX + 1][prePosY] == 1 || readMap[prePosX + 1][prePosY] == 3 || readMap[prePosX + 1][prePosY] == 4
				|| readMap[prePosX + 1][prePosY] == 5)
			{
				if (readMap[prePosX + 1][prePosY] == 3)
				{
					SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), LIGHTGREY);
					cout << "You see: TREE" << endl;
					return;
				}
				else if (readMap[prePosX + 1][prePosY] == 4)
				{
					readMap[prePosX + 1][prePosY] = 5;
					SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), GREEN);
					cout << "You see: CHEST [you found a key]" << endl;
					keyValue = 1000;
					return;
				}
				else if (readMap[prePosX + 1][prePosY] == 5)
				{
					SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), LIGHTRED);
					cout << "You see: EMPTY CHEST" << endl;
					return;
				}
				else
				{
					SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), LIGHTGREY);
					cout << "You see:" << endl;
					return;
				}
			}
			else
			{
				SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), LIGHTGREY);
				cout << "You see:" << endl;
				readMap[prePosX][prePosY] = 0;
				prePosX += 1;
				readMap[prePosX][prePosY] = 9;
			}
			break;
		}
	case RIGHT:
		{
			// RIGHT -------------------------------------------------------
			if (readMap[prePosX][prePosY + 1] == 2)
			{
				if (keyValue == 1000)
				{
					SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), LIGHTGREY);
					cout << "You see:" << endl;
					readMap[prePosX][prePosY] = 0;
					mapValue = 2;
				}
				else
				{
					SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), LIGHTRED);
					cout << "You see: LOCKED DOOR" << endl;
					return;
				}
			}
			else if (readMap[prePosX][prePosY + 1] == 1 || readMap[prePosX][prePosY + 1] == 3 || readMap[prePosX][prePosY + 1] == 4
				|| readMap[prePosX][prePosY + 1] == 5)
			{
				if (readMap[prePosX][prePosY + 1] == 3)
				{
					SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), LIGHTGREY);
					cout << "You see: TREE" << endl;
					return;
				}
				else if (readMap[prePosX][prePosY + 1] == 4)
				{
					readMap[prePosX][prePosY + 1] = 5;
					SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), GREEN);
					cout << "You see: CHEST [you found a key]" << endl;
					keyValue = 1000;
					return;
				}
				else if (readMap[prePosX][prePosY + 1] == 5)
				{
					SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), LIGHTRED);
					cout << "You see: EMPTY CHEST" << endl;
					return;
				}
				else
				{
					SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), LIGHTGREY);
					cout << "You see:" << endl;
					return;
				}
			}
			else
			{
				SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), LIGHTGREY);
				cout << "You see:" << endl;
				readMap[prePosX][prePosY] = 0;
				prePosY += 1;
				readMap[prePosX][prePosY] = 9;
			}
			break;
		}
	case LEFT:
		{
			// LEFT -------------------------------------------------------
			if (readMap[prePosX][prePosY - 1] == 2)
			{
				if (keyValue == 1000)
				{
					SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), LIGHTGREY);
					cout << "You see:" << endl;
					readMap[prePosX][prePosY] = 0;
					mapValue = 2;
				}
				else
				{
					SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), LIGHTRED);
					cout << "You see: LOCKED DOOR" << endl;
					return;
				}
			}
			else if (readMap[prePosX][prePosY - 1] == 1 || readMap[prePosX][prePosY - 1] == 3 || readMap[prePosX][prePosY - 1] == 4
				|| readMap[prePosX][prePosY - 1] == 5)
			{
				if (readMap[prePosX][prePosY - 1] == 3)
				{
					SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), LIGHTGREY);
					cout << "You see: TREE" << endl;
					return;
				}
				else if (readMap[prePosX][prePosY - 1] == 4)
				{
					readMap[prePosX][prePosY - 1] = 5;
					SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), GREEN);
					cout << "You see: CHEST [you found a key]" << endl;
					keyValue = 1000;
					return;
				}
				else if (readMap[prePosX][prePosY - 1] == 5)
				{
					SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), LIGHTRED);
					cout << "You see: EMPTY CHEST" << endl;
					return;
				}
				else
				{
					SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), LIGHTGREY);
					cout << "You see:" << endl;
					return;
				}
			}
			else
			{
				SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), LIGHTGREY);
				cout << "You see:" << endl;
				readMap[prePosX][prePosY] = 0;
				prePosY -= 1;
				readMap[prePosX][prePosY] = 9;
			}
			break;
		}
	default:
		{
			return;
		}
		return;
	}
}

int main()
{
	map[a][b] = 9; // Set first location

	cout << "You see:" << endl;
	printMap(map, mapValue);

	HANDLE OutHandle = GetStdHandle(STD_OUTPUT_HANDLE);
	SMALL_RECT windowSize = {0, 0, 35, 20};
	SetConsoleWindowInfo(OutHandle, TRUE, &windowSize);

	while(GetAsyncKeyState(VK_ESCAPE) == false)
	{
		if (GetAsyncKeyState(VK_UP))
		{
			if (mapValue == 1)
			{
				system("CLS");
				movePlayer(UP, map, a, b);
				printMap(map, mapValue);
				Sleep(50);
			}
			else
			{
				system("CLS");
				//map2[map2DoorLoc1][map2DoorLoc2] = 9;
				movePlayer(UP, map2, map2DoorLoc1, map2DoorLoc2);
				printMap(map2, mapValue);
				Sleep(50);
			}
		}
		if (GetAsyncKeyState(VK_DOWN))
		{
			if (mapValue == 1)
			{
				system("CLS");
				movePlayer(DOWN, map, a, b);
				printMap(map, mapValue);
				Sleep(50);
			}
			else
			{
				system("CLS");
				//map2[map2DoorLoc1][map2DoorLoc2] = 9;
				movePlayer(DOWN, map2, map2DoorLoc1, map2DoorLoc2);
				printMap(map2, mapValue);
				Sleep(50);
			}
		}
		if (GetAsyncKeyState(VK_RIGHT))
		{
			if (mapValue == 1)
			{
				system("CLS");
				movePlayer(RIGHT, map, a, b);
				printMap(map, mapValue);
				Sleep(50);
			}
			else
			{
				system("CLS");
				//map2[map2DoorLoc1][map2DoorLoc2] = 9;
				movePlayer(RIGHT, map2, map2DoorLoc1, map2DoorLoc2);
				printMap(map2, mapValue);
				Sleep(50);
			}
		}
		if (GetAsyncKeyState(VK_LEFT))
		{
			if (mapValue == 1)
			{
				system("CLS");
				movePlayer(LEFT, map, a, b);
				printMap(map, mapValue);
				Sleep(50);
			}
			else
			{
				system("CLS");
				//map2[map2DoorLoc1][map2DoorLoc2] = 9;
				movePlayer(LEFT, map2, map2DoorLoc1, map2DoorLoc2);
				printMap(map2, mapValue);
				Sleep(50);
			}
		}
	}
	system("PAUSE");
}

.h addition:
Code:
// This header file is used to store definitions

/* Map number definitions
0 = empty space (walkable)
1 = wall
2 = door
3 = tree
4 = chest
5 = empty chest
9 = player
*/

// Key value storage
int keyValue = 0;

// Color definitions
#define BLACK 0
#define BLUE 1
#define GREEN 2
#define CYAN 3
#define RED 4
#define MAGENTA 5
#define BROWN 6
#define LIGHTGREY 7
#define DARKGREY 8
#define LIGHTBLUE 9
#define LIGHTGREEN 10
#define LIGHTCYAN 11
#define LIGHTRED 12
#define LIGHTMAGENTA 13
#define YELLOW 14
#define WHITE 15
#define BLINK 128

// Direction definitions
#define UP 0
#define DOWN 1
#define RIGHT 2
#define LEFT 3

// Integer definitions
int getX;
int getY;
int newX;
int newY;
int a = 12; // Beginning location
int b = 1; // Beginning location
int posStoreX;
int posStoreY;

// Specified map door locations
int mapValue = 1;
int mapDoorLoc1 = 2;
int mapDoorLoc2 = 13;
int map2DoorLoc1 = 3;
int map2DoorLoc2 = 13;

int map[15][15] = {
	{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
	{1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 4, 0, 1},
	{1, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 1, 3, 0, 1},
	{2, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 1},
	{1, 0, 3, 0, 0, 0, 0, 0, 1, 1, 1, 1, 3, 0, 1},
	{1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1},
	{1, 1, 0, 0, 0, 3, 0, 0, 0, 0, 0, 1, 0, 3, 1},
	{1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 3, 0, 0, 0, 1},
	{1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
	{1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1},
	{1, 0, 0, 0, 0, 0, 0, 0, 3, 1, 0, 0, 0, 0, 1},
	{1, 0, 0, 0, 3, 0, 0, 0, 0, 1, 0, 0, 3, 0, 1},
	{1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 1, 1},
	{1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1},
	{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}
};

int map2[15][15] = {
	{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
	{1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
	{1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
	{1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
	{1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
	{1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
	{1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
	{1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
	{1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
	{1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
	{1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
	{1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
	{1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
	{1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
	{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}
};

CBJHEf.png


NOTE: I made this a long time ago, I could make this code a lot simpler and faster
 
I just don't have any interest in using C++ in OpenTibia.
Well, TFS is written in C++ (actually most of Ot Projects, if you're up-to-date, Otclient, RME, etc.). I seen some Ot members over here who were trying to edit TFS's source code and were not able to, so I don't see why not make a "simplified" tutorial like this one to read? I mean there are alot of longass tutorials over the interwebs that explain stuff like this but alot longer. correct me if I'm wrong.
 
Back
Top