1 (edited by Pikotee 2012-12-04 10:21:45)

Topic: [MODs] How to mod? C++ Tutorial for newbies

Introduction...

Hello everybody.
In this Topic i'll try my best to teach you how to create mods for Teeworlds.

At least you should know the Basics of C/C++: If/Else, For/While, Variables, Switch-Case... So, the Basics... and maybe functions and pointers

If not you better press Ctrl+T, type in youtube.com, then C++ Tutorial and then press Enter, choose any video and take an hour to learn the basics, it'll be easy if you have motivation...

You also need to know how to compile the source code (create an .exe), if not, take a look on my friend's compiling tutorial for Windows:
https://www.teeworlds.com/forum/viewtopic.php?id=9541/

To modify the Teeworlds source i recommend Microsoft Visual C++ 2010 Express Edition.

Ok let's start...

Maybe you've already took a look into the source and seen lots of files
I'll explain some important files, where you can gain visible success by changing only some parts:

...src\game\

  • variables.h - Here you're able to add rcon (F2) Commands


...src\game\server\

  • gamecontext.cpp - some server sided stuff, broadcast, explosion function

  • player.cpp - text you see in scoreboard ot this tee( Name, Score, Clan...)


...src\game\server\enteties\

  • character.cpp - lifehandling, armor, weapon,...

  • laser.cpp - Rifle settings

  • pickup.cpp - PickUps, respawntimer...

  • projectile.cpp - Lifetime of projectiles, explosion if needed, curvature, speed


...src\game\server\gamemodes\

  • ~.cpp - Name of GameType, maybe some speacial stuff

First Code:

// ...src\game\server\gamemodes\dm.cpp
// in constructor we see m_pGameType = "DM";
// change it to: m_pGameType = "OwnMod";  the string will be displayed as gametype
// Remember: Not more than 7 symbols

CGameControllerDM::CGameControllerDM(class CGameContext *pGameServer)
: IGameController(pGameServer)
{
    m_pGameType = "OwnMod";
}

Now compile it and see the quite little result...
I'll continue after school with Weapon modifications, because it's the easiest way to learn how to make mods and i guess almost everybody started like this.



Write down everything you want: Feedback, Corrections, Questions, Greeting, whatever...

...

#yemDX is a known troll

2 (edited by Dune 2012-12-04 14:01:31)

Re: [MODs] How to mod? C++ Tutorial for newbies

This has been discussed a lot and there have been several tries on the topic. The conclusion that has been come to is:


You cannot code anything good (i.e not just tuning such as infinite grenade ammo) for Teeworlds unless you're investing yourself in learning C++ and programming in general.


This kind of topic can be good (great!) to help people to begin with the Teeworlds code if they already have a decent experience with programming. Looking into Teeworlds code can be a great way to get motivated to learn programming, but no topic of this kind can ever learn you half of what you need to know.



Programming is great, it can be relaxing, exciting. I'm not trying to demotivate you from learning it. It's great, imo. But it takes time - a lot of time - you have to be motivated to get into it and do not expect to be able to code into Teeworlds after a few 10hours.

Not Luck, Just Magic.

3

Re: [MODs] How to mod? C++ Tutorial for newbies

Hm, i agree, but you can even code without any C/C++ experiance. E.g. $$Killer$$ who made KK|Town(http://www.kktown.tk/) asked me several times very simple and almost stupid things, but he made a city mod. I started with weapon tuning as well, and i knew only the already mentioned things (variables, If/eles, Loops) but after 2 years I'm able to do every mod i want.

Do you think i should explain the Variables, e.g. m_Score or should i write a code with and explain it?

I mean, people which already knows C++ can take a look and some ideas...

#yemDX is a known troll

4

Re: [MODs] How to mod? C++ Tutorial for newbies

Pikotee wrote:

Hm, i agree, but you can even code without any C/C++ experiance. E.g. $$Killer$$ who made KK|Town(http://www.kktown.tk/) asked me several times very simple and almost stupid things, but he made a city mod. I started with weapon tuning as well, [...]

myself wrote:

You cannot code anything good (i.e not just tuning such as infinite grenade ammo) for Teeworlds unless you're investing yourself in learning C++ and programming in general.

Not Luck, Just Magic.

5

Re: [MODs] How to mod? C++ Tutorial for newbies

So, do you recommend to continue with tutorial, or just answer questions?

#yemDX is a known troll

6

Re: [MODs] How to mod? C++ Tutorial for newbies

An explanation of pointers would be awesome

7 (edited by Pikotee 2012-12-04 18:48:23)

Re: [MODs] How to mod? C++ Tutorial for newbies

Based on Teeworlds?
or in general?

#yemDX is a known troll

8 (edited by Ich 2012-12-04 20:00:08)

Re: [MODs] How to mod? C++ Tutorial for newbies

twice but with easy and hard examples in teeworlds

9

Re: [MODs] How to mod? C++ Tutorial for newbies

That's great idea!

@Pikotee: pls, add smth like difference between character and player (character is alive, player is connected and player contains character)

Also, you can add some info about debugging, or just to HOWTO learn `what does this code?`. I mean write smth about str_format and printing to game chat.


@Dune:
your messages always begin with criticism, then you write how you like the same things from the other hand, and as a result it is not clear what do you mean, and only demotivate players to do anything else except simply playing

10 (edited by Pikotee 2012-12-04 23:32:20)

Re: [MODs] How to mod? C++ Tutorial for newbies

Pointer
dunno how to explain Pointer, but ...

What is a Pointer:
a Pointer saves the address of the given Variable's memory cell, you can change the value of this Variable by references

Pointer:
datatype *Name = &Variable;
(& means the address of the variable)

*Name = value of the variable
Name = address of the variable

Examples:

1. int *pScore = &m_Score;
set the value of m_Score with the Pointer:
*pScore = 1337;
get the address of m_Score:
pScore or &m_Score


2. We can reserve memory cells for special variables:
int *pVar = new int;
int pears = 5;
// pVar = &pears; Wrong, thx to BeaR
*pVar = pears; // is correct now...

after that we've to delete the reserved space:
delete pVar;

Why?? the pointers won't be deleted by system, so they are used like static variables. So we have to do it by our self if we don't need them.



Examples in Teeworlds:

int CCollision::IntersectLine(vec2 Pos0, vec2 Pos1, vec2 *pOutCollision, vec2 *pOutBeforeCollision)
{
    float Distance = distance(Pos0, Pos1);
    int End(Distance+1);
    vec2 Last = Pos0;

    for(int i = 0; i < End; i++)
    {
        float a = i/Distance;
        vec2 Pos = mix(Pos0, Pos1, a);
        if(CheckPoint(Pos.x, Pos.y))
        {
            if(pOutCollision)
                *pOutCollision = Pos;
            if(pOutBeforeCollision)
                *pOutBeforeCollision = Last;
            return GetCollisionAt(Pos.x, Pos.y);
        }
        Last = Pos;
    }
    if(pOutCollision)
        *pOutCollision = Pos1;
    if(pOutBeforeCollision)
        *pOutBeforeCollision = Pos1;
    return 0;
}
vec2 collisionIn = vec2(0, 0); //The Collision inside the Wall;
vec2 collisionOut = vec2(0, 0); //The Collision outside the Wall;

// IntersectLine(vec2 Pos0, vec2 Pos1, vec2 *pOutCollision, vec2 *pOutBeforeCollision)
// Our function to check for Walls

IntersectLine(MyPosition, EnemysPosition, &collisionIn, &collisionOut);

// collisionIn has the first wallcollision inside the wall and
// collisionOut has the last position before collisionIn

...

@Shahan: you can write a tutorial or some examples as well^^

#yemDX is a known troll

11 (edited by BeaR 2012-12-04 23:15:55)

Re: [MODs] How to mod? C++ Tutorial for newbies

Pikotee wrote:
2. We can reserve memory cells for special variables:
int *pVar = new int;
int pears = 5;
pVar = &pears;

after that we've to delete the reserved space:
delete pVar; 

Pls take a look again, what are u actually trying to delete with 'delete pVar'...
(Just skimmed the thread, so..)

12 (edited by unsigned char* 2012-12-04 23:12:59)

Re: [MODs] How to mod? C++ Tutorial for newbies

Memory Leak smile

13

Re: [MODs] How to mod? C++ Tutorial for newbies

Thank you, haven't seen that...

#yemDX is a known troll

14 (edited by Dune 2012-12-04 23:36:25)

Re: [MODs] How to mod? C++ Tutorial for newbies

Ich wrote:

An explanation of pointers would be awesome

This kind of question is definitely not appropriate here. This is the very basics of C programming, you should not be asking for this kind of very general explanations here, rather look for a C tutorial and PRACTICE - that last part is very important.


To your question, I think it could be a place for people to ask for specific Teeworlds code answers, but all C generic questions should be redirected to better places.

Not Luck, Just Magic.

15

Re: [MODs] How to mod? C++ Tutorial for newbies

CCharacter - character.cpp
So, let's start with character.cpp it has the ingame information about the player after spawn, so almost everything you see of any tee is written in CCharacter. That's the difference to player.cpp (CPlayer) that describes the connected player, name, country, skin, clan tag...

CCharacter

Variables:
int m_Health - health of Tee
int m_Armor - armor of Tee

int m_ActiveWeapon - the current Weapon
int m_LastWeapon - the weapon before switching
int m_QueuedWeapon - the wanted weapon on switch
int m_ReloadTimer - reload timer of weapons
Functions:
bool IsGrounded() - returns true if tee is on the ground
void SetWeapon(int Weapon) - WEAPON_GUN/SHOTGUN/RIFLE/GRENADE/HAMMER/NINJA sets the current Weapon
void Die(int Killer, int Weapon) - KillerPlayerID, Weapon: WEAPON_GAME(team switching), WEAPON_SELF(suizide), WEAPON_WORLD(death tiles) or any Weapon(WEAPON_GUN/GRENADE...)
bool TakeDamage(vec2 Force, int Dmg, int From, int Weapon) - Force added to Victim's Velocity, Damage, ID of attacking Player, Weapon
bool IncreaseHealth(int Amount) - Increases the health(up to 10)
bool IncreaseArmor(int Amount) - Increases the armor(up to 10)
bool GiveWeapon(int Weapon, int Ammo) - gives Weapon with x Ammo, or -1 for infinity Ammo
void GiveNinja() gives Ninja to player
bool IsAlive() - returns true if player is alive
Stuff:
m_pPlayer->GetCID() - ID of this CCharacter

if(m_aWeapons[...].m_Ammo > 0)// ...= m_ActiveWeapon, WEAPON_GUN/HAMMER/...
{// if Ammo of Weapon > 0}

if(m_aWeapons[...].m_Got)
{ // If player got this weapon} // ...= m_LastWeapon, WEAPON_NINJA/RIFLE/...


Write down any Questions, Greetings, Feedback, Mistakes...(NOT U Dune !!!)
...

#yemDX is a known troll

16

Re: [MODs] How to mod? C++ Tutorial for newbies

oh, Pikotee, pls don't explain difference between i++ and ++i! I'm about pointer tongue

btw, what u r doing is source documentation. Probably, you can draw UML diagram ^^

hey, Dune, what do you think about MediaWiki teeworlds documentation? I'm not going to offend anybody, but actual documentation "is not so good as it should" wink In mediawiki documentation site we will be able to explain the stuff of each source file, write step-by-step tutorials and so on^^

17 (edited by Dune 2012-12-05 22:53:21)

Re: [MODs] How to mod? C++ Tutorial for newbies

Shahan wrote:

oh, Pikotee, pls don't explain difference between i++ and ++i! I'm about pointer tongue

btw, what u r doing is source documentation. Probably, you can draw UML diagram ^^

hey, Dune, what do you think about MediaWiki teeworlds documentation? I'm not going to offend anybody, but actual documentation "is not so good as it should" wink In mediawiki documentation site we will be able to explain the stuff of each source file, write step-by-step tutorials and so on^^

Again, this kind of rhetorical question doesn't fit here. You don't have problems to deal with the Teeworlds code, but with general C++ coding.

About the doc, I don't feel like it's supposed to explain the code, more the conventions, how to deal with it, how to compile, contribute etc etc... The code keeps being updated, it wouldn't mean much. It could be better, but imo not in the way you think.

About the UML diagrams, I've heard the original developers initially drew some. Does anyone know where to find them?

Not Luck, Just Magic.

18

Re: [MODs] How to mod? C++ Tutorial for newbies

You can use programs like "StarUML" that have reverse engineer tools for generate diagrams that you can complete with info...

https://dl.dropbox.com/u/30566237/test.jpg

19 (edited by Dune 2012-12-06 09:59:42)

Re: [MODs] How to mod? C++ Tutorial for newbies

I do not know wrote:

You can use programs like "StarUML" that have reverse engineer tools for generate diagrams that you can complete with info...

Interesting! I'm not sure if StarUML is going to give us anything clear enough to read - since it's supposed to be exhaustive  - but now I know what the Reverse Engineer option is for tongue

Not Luck, Just Magic.

20

Re: [MODs] How to mod? C++ Tutorial for newbies

Maybe make full teeworlds source documentation? smile Each function explained with params.

C++/Java my life ^^
If you like dislike button click it
NOW!

21

Re: [MODs] How to mod? C++ Tutorial for newbies

kittyPL wrote:

Maybe make full teeworlds source documentation? smile Each function explained with params.

That should be in the code, shouldn't it.

Not Luck, Just Magic.

22

Re: [MODs] How to mod? C++ Tutorial for newbies

kittyPL wrote:

Maybe make full teeworlds source documentation? smile Each function explained with params.

The relevant code for the average modder is currently documented in the headers of the code.

...

public:
    IGameController(class CGameContext *pGameServer);
    virtual ~IGameController() {};

    // event
    /*
        Function: on_CCharacter_death
            Called when a CCharacter in the world dies.

        Arguments:
            victim - The CCharacter that died.
            killer - The player that killed it.
            weapon - What weapon that killed it. Can be -1 for undefined
                weapon when switching team or player suicides.
    */
    virtual int OnCharacterDeath(class CCharacter *pVictim, class CPlayer *pKiller, int Weapon);
    /*
        Function: on_CCharacter_spawn
            Called when a CCharacter spawns into the game world.

        Arguments:
            chr - The CCharacter that was spawned.
    */
    virtual void OnCharacterSpawn(class CCharacter *pChr);

    /*
        Function: on_entity
            Called when the map is loaded to process an entity
            in the map.

        Arguments:
            index - Entity index.
            pos - Where the entity is located in the world.

        Returns:
            bool?
    */
    virtual bool OnEntity(int Index, vec2 Pos);

...

23

Re: [MODs] How to mod? C++ Tutorial for newbies

Yeah, kittyPL, great idea - let's waste my life for description of whole source^^

#yemDX is a known troll

24

Re: [MODs] How to mod? C++ Tutorial for newbies

I mean, it's possible to generate online docu using doxygen or something like this... smile

C++/Java my life ^^
If you like dislike button click it
NOW!

25

Re: [MODs] How to mod? C++ Tutorial for newbies

hm... I'm too lazy for that...
I'm just doing little tutorials bit max. 30 minutes of work

#yemDX is a known troll