]> git.jsancho.org Git - lugaru.git/blobdiff - Source/GameTick.cpp
cleanup of campaign levels handling.
[lugaru.git] / Source / GameTick.cpp
index 1fba112a8fbf9ff782b06aa07f5fd8f293a83a17..4048f8783dbe7960a84857db1f80d8e125cc3e5e 100644 (file)
@@ -27,6 +27,7 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 #include <direct.h>
 #endif
 
+#include <limits>
 #include <ctime>
 #include "Game.h"
 #include "openal_wrapper.h"
@@ -35,6 +36,8 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 #include "Animation.h"
 #include "Awards.h"
 
+#include <algorithm>
+
 using namespace std;
 
 // Added more evilness needed for MSVC
@@ -77,10 +80,9 @@ extern bool keyboardfrozen;
 extern bool loadingstuff;
 extern XYZ windvector;
 extern bool debugmode;
-static int music1;
+static int leveltheme;
 extern int mainmenu;
 extern bool visibleloading;
-extern int loadscreencolor;
 extern XYZ envsound[30];
 extern float envsoundvol[30];
 extern int numenvsounds;
@@ -283,40 +285,37 @@ static void ch_map(Game *game, const char *args)
   campaign = 0;
 }
 
-static void ch_save(Game *game, const char *args)
-{
-  char buf[64];
-  int i, j, k, l, m, templength;
-  float headprop, bodyprop, armprop, legprop;
-  snprintf(buf, 63, ":Data:Maps:%s", args);
-
-
-  int mapvers = 12;;
-
-  FILE                 *tfile;
-  tfile=fopen( ConvertFileName(buf), "wb" );
-  fpackf(tfile, "Bi", mapvers);
-  fpackf(tfile, "Bi", maptype);
-  fpackf(tfile, "Bi", hostile);
-  fpackf(tfile, "Bf Bf", viewdistance, fadestart);
-  fpackf(tfile, "Bb Bf Bf Bf", skyboxtexture, skyboxr, skyboxg, skyboxb);
-  fpackf(tfile, "Bf Bf Bf", skyboxlightr, skyboxlightg, skyboxlightb);
-  fpackf(tfile, "Bf Bf Bf Bf Bf Bi", player[0].coords.x, player[0].coords.y, player[0].coords.z, player[0].rotation, player[0].targetrotation, player[0].num_weapons);
-  if(player[0].num_weapons>0&&player[0].num_weapons<5)
-    for(int j=0;j<player[0].num_weapons;j++){
-      fpackf(tfile, "Bi", weapons.type[player[0].weaponids[j]]);
-    }
+static void ch_save(Game *game, const char *args){
+    char buf[64];
+    snprintf(buf, 63, ":Data:Maps:%s", args);
+
+    int mapvers = 12;
+
+    FILE *tfile;
+    tfile=fopen( ConvertFileName(buf), "wb" );
+    fpackf(tfile, "Bi", mapvers);
+    fpackf(tfile, "Bi", maptype);
+    fpackf(tfile, "Bi", hostile);
+    fpackf(tfile, "Bf Bf", viewdistance, fadestart);
+    fpackf(tfile, "Bb Bf Bf Bf", skyboxtexture, skyboxr, skyboxg, skyboxb);
+    fpackf(tfile, "Bf Bf Bf", skyboxlightr, skyboxlightg, skyboxlightb);
+    fpackf(tfile, "Bf Bf Bf Bf Bf Bi", player[0].coords.x, player[0].coords.y, player[0].coords.z,
+            player[0].rotation, player[0].targetrotation, player[0].num_weapons);
+    if(player[0].num_weapons>0&&player[0].num_weapons<5)
+        for(int j=0;j<player[0].num_weapons;j++)
+          fpackf(tfile, "Bi", weapons[player[0].weaponids[j]].getType());
 
-  fpackf(tfile, "Bf Bf Bf", player[0].armorhead, player[0].armorhigh, player[0].armorlow);
-  fpackf(tfile, "Bf Bf Bf", player[0].protectionhead, player[0].protectionhigh, player[0].protectionlow);
-  fpackf(tfile, "Bf Bf Bf", player[0].metalhead, player[0].metalhigh, player[0].metallow);
-  fpackf(tfile, "Bf Bf", player[0].power, player[0].speedmult);
+    fpackf(tfile, "Bf Bf Bf", player[0].armorhead, player[0].armorhigh, player[0].armorlow);
+    fpackf(tfile, "Bf Bf Bf", player[0].protectionhead, player[0].protectionhigh, player[0].protectionlow);
+    fpackf(tfile, "Bf Bf Bf", player[0].metalhead, player[0].metalhigh, player[0].metallow);
+    fpackf(tfile, "Bf Bf", player[0].power, player[0].speedmult);
 
-  fpackf(tfile, "Bi", player[0].numclothes);
+    fpackf(tfile, "Bi", player[0].numclothes);
 
-  fpackf(tfile, "Bi Bi", player[0].whichskin, player[0].creature);
+    fpackf(tfile, "Bi Bi", player[0].whichskin, player[0].creature);
+
+    fpackf(tfile, "Bi", numdialogues);
 
-  fpackf(tfile, "Bi", numdialogues);
        for(int k=0;k<numdialogues;k++){
                fpackf(tfile, "Bi", numdialogueboxes[k]);
                fpackf(tfile, "Bi", dialoguetype[k]);
@@ -331,18 +330,20 @@ static void ch_save(Game *game, const char *args)
                        fpackf(tfile, "Bf", dialogueboxcolor[k][l][2]);
                        fpackf(tfile, "Bi", dialogueboxsound[k][l]);
 
-                       templength=strlen(dialoguetext[k][l]);
+                       int templength=strlen(dialoguetext[k][l]);
                        fpackf(tfile, "Bi",(templength));
                        for(int m=0;m<templength;m++){
                                fpackf(tfile, "Bb", dialoguetext[k][l][m]);
-                               if(dialoguetext[k][l][m]=='\0')break;
+                               if(dialoguetext[k][l][m]=='\0')
+                    break;
                        }
 
                        templength=strlen(dialoguename[k][l]);
                        fpackf(tfile, "Bi",templength);
                        for(int m=0;m<templength;m++){
                                fpackf(tfile, "Bb", dialoguename[k][l][m]);
-                               if(dialoguename[k][l][m]=='\0')break;
+                               if(dialoguename[k][l][m]=='\0')
+                    break;
                        }
 
                        fpackf(tfile, "Bf Bf Bf", dialoguecamera[k][l].x, dialoguecamera[k][l].y, dialoguecamera[k][l].z);
@@ -357,95 +358,96 @@ static void ch_save(Game *game, const char *args)
        }
 
        for(int k=0;k<player[0].numclothes;k++){
-               templength=strlen(player[0].clothes[k]);
+               int templength=strlen(player[0].clothes[k]);
                fpackf(tfile, "Bi", templength);
                for(int l=0;l<templength;l++)
                        fpackf(tfile, "Bb", player[0].clothes[k][l]);
                fpackf(tfile, "Bf Bf Bf", player[0].clothestintr[k], player[0].clothestintg[k], player[0].clothestintb[k]);
        }
 
-  fpackf(tfile, "Bi", environment);
+    fpackf(tfile, "Bi", environment);
 
-  fpackf(tfile, "Bi", objects.numobjects);
+    fpackf(tfile, "Bi", objects.numobjects);
 
-    for(int k=0;k<objects.numobjects;k++){
-      fpackf(tfile, "Bi Bf Bf Bf Bf Bf Bf", objects.type[k], objects.rotation[k], objects.rotation2[k], objects.position[k].x, objects.position[k].y, objects.position[k].z, objects.scale[k]);
-    }
+    for(int k=0;k<objects.numobjects;k++)
+        fpackf(tfile, "Bi Bf Bf Bf Bf Bf Bf", objects.type[k], objects.rotation[k], objects.rotation2[k],
+            objects.position[k].x, objects.position[k].y, objects.position[k].z, objects.scale[k]);
 
-  fpackf(tfile, "Bi", numhotspots);
+    fpackf(tfile, "Bi", numhotspots);
        for(int i=0;i<numhotspots;i++){
                fpackf(tfile, "Bi Bf Bf Bf Bf", hotspottype[i],hotspotsize[i],hotspot[i].x,hotspot[i].y,hotspot[i].z);
-               templength=strlen(hotspottext[i]);
+               int templength=strlen(hotspottext[i]);
                fpackf(tfile, "Bi",templength);
                for(int l=0;l<templength;l++)
                        fpackf(tfile, "Bb", hotspottext[i][l]);
        }
 
-  fpackf(tfile, "Bi", numplayers);
-  if(numplayers<maxplayers)
-    for(int j=1;j<numplayers;j++){
-               fpackf(tfile, "Bi Bi Bf Bf Bf Bi Bi Bf Bb Bf", player[j].whichskin, player[j].creature, player[j].coords.x, player[j].coords.y, player[j].coords.z, player[j].num_weapons, player[j].howactive, player[j].scale, player[j].immobile, player[j].rotation);
-               if(player[j].num_weapons<5)
-                       for(int k=0;k<player[j].num_weapons;k++){
-                               fpackf(tfile, "Bi", weapons.type[player[j].weaponids[k]]);
-                       }
-               if(player[j].numwaypoints<30){
-                       fpackf(tfile, "Bi", player[j].numwaypoints);
-                       for(int k=0;k<player[j].numwaypoints;k++){
-                               fpackf(tfile, "Bf", player[j].waypoints[k].x);
-                               fpackf(tfile, "Bf", player[j].waypoints[k].y);
-                               fpackf(tfile, "Bf", player[j].waypoints[k].z);
-                               fpackf(tfile, "Bi", player[j].waypointtype[k]);
-                       }
-                       fpackf(tfile, "Bi", player[j].waypoint);
-               } else {
-                       player[j].numwaypoints=0;
-                       player[j].waypoint=0;
-                       fpackf(tfile, "Bi Bi Bi", player[j].numwaypoints, player[j].waypoint, player[j].waypoint);
-               }
+    fpackf(tfile, "Bi", numplayers);
+    if(numplayers<maxplayers)
+        for(int j=1;j<numplayers;j++){
+            fpackf(tfile, "Bi Bi Bf Bf Bf Bi Bi Bf Bb Bf", player[j].whichskin, player[j].creature,
+                    player[j].coords.x, player[j].coords.y, player[j].coords.z,
+                    player[j].num_weapons, player[j].howactive, player[j].scale, player[j].immobile, player[j].rotation);
+            if(player[j].num_weapons<5)
+                for(int k=0;k<player[j].num_weapons;k++)
+                    fpackf(tfile, "Bi", weapons[player[j].weaponids[k]].getType());
+            if(player[j].numwaypoints<30){
+                fpackf(tfile, "Bi", player[j].numwaypoints);
+                for(int k=0;k<player[j].numwaypoints;k++){
+                    fpackf(tfile, "Bf", player[j].waypoints[k].x);
+                    fpackf(tfile, "Bf", player[j].waypoints[k].y);
+                    fpackf(tfile, "Bf", player[j].waypoints[k].z);
+                    fpackf(tfile, "Bi", player[j].waypointtype[k]);
+                }
+                fpackf(tfile, "Bi", player[j].waypoint);
+            }else{
+                player[j].numwaypoints=0;
+                player[j].waypoint=0;
+                fpackf(tfile, "Bi Bi Bi", player[j].numwaypoints, player[j].waypoint, player[j].waypoint);
+            }
 
-               fpackf(tfile, "Bf Bf Bf", player[j].armorhead, player[j].armorhigh, player[j].armorlow);
-               fpackf(tfile, "Bf Bf Bf", player[j].protectionhead, player[j].protectionhigh, player[j].protectionlow);
-               fpackf(tfile, "Bf Bf Bf", player[j].metalhead, player[j].metalhigh, player[j].metallow);
-               fpackf(tfile, "Bf Bf", player[j].power, player[j].speedmult);
-
-               if(player[j].creature==wolftype) {
-                       headprop=player[j].proportionhead.x/1.1;
-                       bodyprop=player[j].proportionbody.x/1.1;
-                       armprop=player[j].proportionarms.x/1.1;
-                       legprop=player[j].proportionlegs.x/1.1;
-               } else if(player[j].creature==rabbittype){
-                       headprop=player[j].proportionhead.x/1.2;
-                       bodyprop=player[j].proportionbody.x/1.05;
-                       armprop=player[j].proportionarms.x/1.00;
-                       legprop=player[j].proportionlegs.x/1.1;
-               }
+            fpackf(tfile, "Bf Bf Bf", player[j].armorhead, player[j].armorhigh, player[j].armorlow);
+            fpackf(tfile, "Bf Bf Bf", player[j].protectionhead, player[j].protectionhigh, player[j].protectionlow);
+            fpackf(tfile, "Bf Bf Bf", player[j].metalhead, player[j].metalhigh, player[j].metallow);
+            fpackf(tfile, "Bf Bf", player[j].power, player[j].speedmult);
+
+            float headprop, bodyprop, armprop, legprop;
+            if(player[j].creature==wolftype){
+                headprop=player[j].proportionhead.x/1.1;
+                bodyprop=player[j].proportionbody.x/1.1;
+                armprop=player[j].proportionarms.x/1.1;
+                legprop=player[j].proportionlegs.x/1.1;
+            }else if(player[j].creature==rabbittype){
+                headprop=player[j].proportionhead.x/1.2;
+                bodyprop=player[j].proportionbody.x/1.05;
+                armprop=player[j].proportionarms.x/1.00;
+                legprop=player[j].proportionlegs.x/1.1;
+            }
 
-               fpackf(tfile, "Bf Bf Bf Bf", headprop, bodyprop, armprop, legprop);
-
-               fpackf(tfile, "Bi", player[j].numclothes);
-               if(player[j].numclothes)
-                       for(int k=0;k<player[j].numclothes;k++){
-                               int templength;
-                               templength=strlen(player[j].clothes[k]);
-                               fpackf(tfile, "Bi", templength);
-                               for(int l=0;l<templength;l++)
-                                       fpackf(tfile, "Bb", player[j].clothes[k][l]);
-                               fpackf(tfile, "Bf Bf Bf", player[j].clothestintr[k], player[j].clothestintg[k], player[j].clothestintb[k]);
-                       }
-    }
+            fpackf(tfile, "Bf Bf Bf Bf", headprop, bodyprop, armprop, legprop);
+
+            fpackf(tfile, "Bi", player[j].numclothes);
+            if(player[j].numclothes)
+                for(int k=0;k<player[j].numclothes;k++){
+                    int templength;
+                    templength=strlen(player[j].clothes[k]);
+                    fpackf(tfile, "Bi", templength);
+                    for(int l=0;l<templength;l++)
+                        fpackf(tfile, "Bb", player[j].clothes[k][l]);
+                    fpackf(tfile, "Bf Bf Bf", player[j].clothestintr[k], player[j].clothestintg[k], player[j].clothestintb[k]);
+                }
+        }
 
-  fpackf(tfile, "Bi", game->numpathpoints);
+    fpackf(tfile, "Bi", game->numpathpoints);
        for(int j=0;j<game->numpathpoints;j++){
                fpackf(tfile, "Bf Bf Bf Bi", game->pathpoint[j].x, game->pathpoint[j].y, game->pathpoint[j].z, game->numpathpointconnect[j]);
-               for(int k=0;k<game->numpathpointconnect[j];k++){
+               for(int k=0;k<game->numpathpointconnect[j];k++)
                        fpackf(tfile, "Bi", game->pathpointconnect[j][k]);
-               }
        }
 
-  fpackf(tfile, "Bf Bf Bf Bf", game->mapcenter.x, game->mapcenter.y, game->mapcenter.z, game->mapradius);
+    fpackf(tfile, "Bf Bf Bf Bf", game->mapcenter.x, game->mapcenter.y, game->mapcenter.z, game->mapradius);
 
-  fclose(tfile);
+    fclose(tfile);
 }
 
 static void ch_cellar(Game *game, const char *args)
@@ -496,7 +498,7 @@ static void ch_size(Game *game, const char *args)
 static int find_closest()
 {
   int closest = 0;
-  float closestdist = 1.0/0.0;
+  float closestdist = std::numeric_limits<float>::max();
 
   for (int i = 1; i < numplayers; i++) {
     float distance;
@@ -640,7 +642,7 @@ static void set_clothes(int pnum, Game *game, const char *args)
   char buf[64];
   snprintf(buf, 63, ":Data:Textures:%s.png", args);
 
-  if (!game->AddClothes(buf,0,1,&player[pnum].skeleton.skinText[pnum],&player[pnum].skeleton.skinsize))
+  if (!game->AddClothes(buf,&player[pnum].skeleton.skinText[pnum]))
     return;
 
   player[pnum].DoMipmaps();
@@ -1130,8 +1132,6 @@ void Screenshot   (void)
        save_image(temp);
 }
 
-
-
 void Game::SetUpLighting(){
        if(environment==snowyenvironment)
         light.setColors(.65,.65,.7,.4,.4,.44);
@@ -1263,7 +1263,7 @@ int Game::checkcollide(XYZ startpoint,XYZ endpoint,int what){
        return -1;
 }
 
-void   Game::Setenvironment(int which)
+void Game::Setenvironment(int which)
 {
        LOGFUNC;
 
@@ -1272,9 +1272,9 @@ void      Game::Setenvironment(int which)
        float temptexdetail;
        environment=which;
 
-       pause_sound(stream_music1snow);
-       pause_sound(stream_music1grass);
-       pause_sound(stream_music1desert);
+       pause_sound(stream_snowtheme);
+       pause_sound(stream_grasstheme);
+       pause_sound(stream_deserttheme);
        pause_sound(stream_wind);
        pause_sound(stream_desertambient);
 
@@ -1317,8 +1317,7 @@ void      Game::Setenvironment(int which)
 
 
                texdetail=temptexdetail;
-       }
-       if(environment==desertenvironment){
+       } else if(environment==desertenvironment){
                windvector=0;
                windvector.z=2;
                LoadTexture(":Data:Textures:deserttree.png",&objects.treetextureptr,0,1);
@@ -1356,8 +1355,7 @@ void      Game::Setenvironment(int which)
 
 
                texdetail=temptexdetail;
-       }
-       if(environment==grassyenvironment){
+       } else if(environment==grassyenvironment){
                windvector=0;
                windvector.z=2;
                LoadTexture(":Data:Textures:tree.png",&objects.treetextureptr,0,1);
@@ -1401,27 +1399,24 @@ void    Game::Setenvironment(int which)
        texdetail=temptexdetail;
 }
 
-
-void   Game::Loadlevel(int which){
+void Game::Loadlevel(int which){
        stealthloading=0;
        whichlevel=which;
 
-       if (which == -1) {
+       if(which == -1){
            tutoriallevel = -1;
            Loadlevel("tutorial");
-       } else if (which >= 0 && which <= 15) {
+       }else if(which >= 0 && which <= 15){
            char buf[32];
-           snprintf(buf, 32, "map%d", which + 1);
+           snprintf(buf, 32, "map%d", which + 1); // challenges
            Loadlevel(buf);
-       } else
+       }else
            Loadlevel("mapsave");
 }
 
-void   Game::Loadlevel(const char *name){
-       static int oldlevel;
+void Game::Loadlevel(const char *name){
        int templength;
        float lamefloat;
-       int lameint;
        static const char *pfx = ":Data:Maps:";
        char *buf;
 
@@ -1431,27 +1426,28 @@ void    Game::Loadlevel(const char *name){
 
        LOG(std::string("Loading level...") + name);
 
-       if(!gameon)visibleloading=1;
-
-       if(stealthloading)visibleloading=0;
-
-       if(!stillloading)loadtime=0;
+       if(!gameon)
+        visibleloading=1;
+       if(stealthloading)
+        visibleloading=0;
+       if(!stillloading)
+        loadtime=0;
        gamestarted=1;
 
        numenvsounds=0;
        //visibleloading=1;
-       if(tutoriallevel!=-1)tutoriallevel=0;
-       else tutoriallevel=1;
+       if(tutoriallevel!=-1)
+        tutoriallevel=0;
+       else
+        tutoriallevel=1;
 
-       if(tutoriallevel==1)tutorialstage=0;
+       if(tutoriallevel==1)
+        tutorialstage=0;
        if(tutorialstage==0){
                tutorialstagetime=0;
                tutorialmaxtime=1;
        }
        loadingstuff=1;
-       if(!firstload){
-               oldlevel=50;
-       }
        pause_sound(whooshsound);
        pause_sound(stream_firesound);
 
@@ -1461,29 +1457,21 @@ void    Game::Loadlevel(const char *name){
        const char *FixedFN = ConvertFileName(buf);
 
        int mapvers;
-       FILE                    *tfile;
+       FILE *tfile;
        tfile=fopen( FixedFN, "rb" );
-       if(tfile)
-       {
+       if(tfile){
                pause_sound(stream_firesound);
-
-
                scoreadded=0;
                windialogue=0;
-
                hostiletime=0;
-
                won=0;
 
-               //campaign=0;
                animation[bounceidleanim].Load((char *)"Idle",middleheight,neutral);
 
                numdialogues=0;
 
                for(int i=0;i<20;i++)
-               {
                        dialoguegonethrough[i]=0;
-               }
 
                indialogue=-1;
                cameramode=0;
@@ -1491,10 +1479,13 @@ void    Game::Loadlevel(const char *name){
                damagedealt=0;
                damagetaken=0;
 
-               if(accountactive)difficulty=accountactive->getDifficulty();
+               if(accountactive)
+            difficulty=accountactive->getDifficulty();
 
-               if(difficulty!=2)minimap=1;
-               else minimap=0;
+               if(difficulty!=2)
+            minimap=1;
+               else
+            minimap=0;
 
                numhotspots=0;
                currenthotspot=-1;
@@ -1509,9 +1500,7 @@ void      Game::Loadlevel(const char *name){
                winfreeze=0;
 
                for(int i=0;i<100;i++)
-               {
                        bonusnum[i]=0;
-               }
 
                numfalls=0;
                numflipfail=0;
@@ -1534,84 +1523,83 @@ void    Game::Loadlevel(const char *name){
                bonus=0;
                gameon=1;
                changedelay=0;
-               if(console)
-               {
+               if(console){
                        emit_sound_np(consolesuccesssound);
                        freeze=0;
-                       console=0;
+                       console=false;
                }
 
-               if(!stealthloading)
-               {
+               if(!stealthloading){
                        terrain.numdecals=0;
                        Sprite::deleteSprites();
                        for(int i=0;i<objects.numobjects;i++)
-                       {
                                objects.model[i].numdecals=0;
-                       }
 
                        int j=objects.numobjects;
-                       for(int i=0;i<j;i++)
-                       {
+                       for(int i=0;i<j;i++){
                                objects.DeleteObject(0);
-                               if(visibleloading){loadscreencolor=4; LoadingScreen();}
+                               if(visibleloading)
+                    LoadingScreen();
                        }
 
                        for(int i=0;i<subdivision;i++)
-                       {
                                for(int j=0;j<subdivision;j++)
-                               {
                                        terrain.patchobjectnum[i][j]=0;
-                               }
-                       }
-                       if(visibleloading){loadscreencolor=4; LoadingScreen();}
+                       if(visibleloading)
+                LoadingScreen();
                }
 
-               weapons.numweapons=0;
+               weapons.clear();
 
                funpackf(tfile, "Bi", &mapvers);
-               if(mapvers>=15)funpackf(tfile, "Bi", &indemo);
-               else indemo=0;
-               if(mapvers>=5)funpackf(tfile, "Bi", &maptype);
-               else maptype=mapkilleveryone;
-               if(mapvers>=6)funpackf(tfile, "Bi", &hostile);
-               else hostile=1;
-               if(mapvers>=4)funpackf(tfile, "Bf Bf", &viewdistance, &fadestart);
+               if(mapvers>=15)
+            funpackf(tfile, "Bi", &indemo);
+               else
+            indemo=0;
+               if(mapvers>=5)
+            funpackf(tfile, "Bi", &maptype);
+               else
+            maptype=mapkilleveryone;
+               if(mapvers>=6)
+            funpackf(tfile, "Bi", &hostile);
                else
-               {
+            hostile=1;
+               if(mapvers>=4)
+            funpackf(tfile, "Bf Bf", &viewdistance, &fadestart);
+               else{
                        viewdistance=100;
                        fadestart=.6;
                }
-               if(mapvers>=2)funpackf(tfile, "Bb Bf Bf Bf", &skyboxtexture, &skyboxr, &skyboxg, &skyboxb);
-               else
-               {
+               if(mapvers>=2)
+            funpackf(tfile, "Bb Bf Bf Bf", &skyboxtexture, &skyboxr, &skyboxg, &skyboxb);
+               else{
                        skyboxtexture=1;
                        skyboxr=1;
                        skyboxg=1;
                        skyboxb=1;
                }
-               if(mapvers>=10)funpackf(tfile, "Bf Bf Bf", &skyboxlightr, &skyboxlightg, &skyboxlightb);
-               else
-               {
+               if(mapvers>=10)
+            funpackf(tfile, "Bf Bf Bf", &skyboxlightr, &skyboxlightg, &skyboxlightb);
+               else{
                        skyboxlightr=skyboxr;
                        skyboxlightg=skyboxg;
                        skyboxlightb=skyboxb;
                }
-               if(!stealthloading)funpackf(tfile, "Bf Bf Bf Bf Bf Bi", &player[0].coords.x,&player[0].coords.y,&player[0].coords.z,&player[0].rotation,&player[0].targetrotation, &player[0].num_weapons);
-               if(stealthloading)funpackf(tfile, "Bf Bf Bf Bf Bf Bi", &lamefloat,&lamefloat,&lamefloat,&lamefloat,&lamefloat, &player[0].num_weapons);
+               if(!stealthloading)
+            funpackf(tfile, "Bf Bf Bf Bf Bf Bi", &player[0].coords.x,&player[0].coords.y,&player[0].coords.z,&player[0].rotation,&player[0].targetrotation, &player[0].num_weapons);
+               if(stealthloading)
+            funpackf(tfile, "Bf Bf Bf Bf Bf Bi", &lamefloat,&lamefloat,&lamefloat,&lamefloat,&lamefloat, &player[0].num_weapons);
                player[0].originalcoords=player[0].coords;
                if(player[0].num_weapons>0&&player[0].num_weapons<5)
-               {
-                       for(int j=0;j<player[0].num_weapons;j++)
-                       {
-                               player[0].weaponids[j]=weapons.numweapons;
-                               funpackf(tfile, "Bi", &weapons.type[weapons.numweapons]);
-                               weapons.owner[weapons.numweapons]=0;
-                               weapons.numweapons++;
+                       for(int j=0;j<player[0].num_weapons;j++){
+                               player[0].weaponids[j]=weapons.size();
+                               int type;
+                               funpackf(tfile, "Bi", &type);
+                               weapons.push_back(Weapon(type,0));
                        }
-               }
 
-               if(visibleloading){loadscreencolor=4; LoadingScreen();}
+               if(visibleloading)
+            LoadingScreen();
 
                funpackf(tfile, "Bf Bf Bf", &player[0].armorhead, &player[0].armorhigh, &player[0].armorlow);
                funpackf(tfile, "Bf Bf Bf", &player[0].protectionhead, &player[0].protectionhigh, &player[0].protectionlow);
@@ -1621,11 +1609,8 @@ void     Game::Loadlevel(const char *name){
                funpackf(tfile, "Bi", &player[0].numclothes);
 
                if(mapvers>=9)
-               {
                        funpackf(tfile, "Bi Bi", &player[0].whichskin, &player[0].creature);
-               }
-               else
-               {
+               else{
                        player[0].whichskin=0;
                        player[0].creature=rabbittype;
                }
@@ -1634,214 +1619,195 @@ void  Game::Loadlevel(const char *name){
                player[0].lastattack2=-1;
                player[0].lastattack3=-1;
 
-               if(mapvers>=8)
-               {
+        //dialogues
+               if(mapvers>=8){
                        funpackf(tfile, "Bi", &numdialogues);
-                       if(numdialogues)
-                       {
-                               for(int k=0;k<numdialogues;k++)
-                               {
-                                       funpackf(tfile, "Bi", &numdialogueboxes[k]);
-                                       funpackf(tfile, "Bi", &dialoguetype[k]);
-                                       for(int l=0;l<10;l++)
-                                       {
-                                               funpackf(tfile, "Bf Bf Bf", &participantlocation[k][l].x, &participantlocation[k][l].y, &participantlocation[k][l].z);
-                                               funpackf(tfile, "Bf", &participantrotation[k][l]);
-                                       }
-                                       if(numdialogueboxes)
-                                       {
-                                               for(int l=0;l<numdialogueboxes[k];l++)
-                                               {
-                                                       funpackf(tfile, "Bi", &dialogueboxlocation[k][l]);
-                                                       funpackf(tfile, "Bf", &dialogueboxcolor[k][l][0]);
-                                                       funpackf(tfile, "Bf", &dialogueboxcolor[k][l][1]);
-                                                       funpackf(tfile, "Bf", &dialogueboxcolor[k][l][2]);
-                                                       funpackf(tfile, "Bi", &dialogueboxsound[k][l]);
-
-                                                       bool doneread;
-
-                                                       funpackf(tfile, "Bi",&templength);
-                                                       if(templength>128||templength<=0)templength=128;
-                            int m;
-                                                       for(m=0;m<templength;m++){
-                                                               funpackf(tfile, "Bb", &dialoguetext[k][l][m]);
-                                                               if(dialoguetext[k][l][m]=='\0')break;
-                                                       }
-                                                       dialoguetext[k][l][m] = 0;
-
-                                                       funpackf(tfile, "Bi",&templength);
-                                                       if(templength>64||templength<=0)templength=64;
-                                                       for(m=0;m<templength;m++){
-                                                               funpackf(tfile, "Bb", &dialoguename[k][l][m]);
-                                                               if(dialoguename[k][l][m]=='\0'){
-                                                                       break;
-                                                               }
-                                                       }
-                                                       dialoguename[k][l][m] = 0;
-                                                       funpackf(tfile, "Bf Bf Bf", &dialoguecamera[k][l].x, &dialoguecamera[k][l].y, &dialoguecamera[k][l].z);
-                                                       funpackf(tfile, "Bi", &participantfocus[k][l]);
-                                                       funpackf(tfile, "Bi", &participantaction[k][l]);
+            for(int k=0;k<numdialogues;k++){
+                funpackf(tfile, "Bi", &numdialogueboxes[k]);
+                funpackf(tfile, "Bi", &dialoguetype[k]);
+                for(int l=0;l<10;l++){
+                    funpackf(tfile, "Bf Bf Bf", &participantlocation[k][l].x, &participantlocation[k][l].y, &participantlocation[k][l].z);
+                    funpackf(tfile, "Bf", &participantrotation[k][l]);
+                }
+                for(int l=0;l<numdialogueboxes[k];l++){
+                    funpackf(tfile, "Bi", &dialogueboxlocation[k][l]);
+                    funpackf(tfile, "Bf", &dialogueboxcolor[k][l][0]);
+                    funpackf(tfile, "Bf", &dialogueboxcolor[k][l][1]);
+                    funpackf(tfile, "Bf", &dialogueboxcolor[k][l][2]);
+                    funpackf(tfile, "Bi", &dialogueboxsound[k][l]);
+
+                    funpackf(tfile, "Bi",&templength);
+                    if(templength>128||templength<=0)
+                        templength=128;
+                    int m;
+                    for(m=0;m<templength;m++){
+                        funpackf(tfile, "Bb", &dialoguetext[k][l][m]);
+                        if(dialoguetext[k][l][m]=='\0')
+                            break;
+                    }
+                    dialoguetext[k][l][m] = 0;
+
+                    funpackf(tfile, "Bi",&templength);
+                    if(templength>64||templength<=0)templength=64;
+                    for(m=0;m<templength;m++){
+                        funpackf(tfile, "Bb", &dialoguename[k][l][m]);
+                        if(dialoguename[k][l][m]=='\0')
+                            break;
+                    }
+                    dialoguename[k][l][m] = 0;
+                    funpackf(tfile, "Bf Bf Bf", &dialoguecamera[k][l].x, &dialoguecamera[k][l].y, &dialoguecamera[k][l].z);
+                    funpackf(tfile, "Bi", &participantfocus[k][l]);
+                    funpackf(tfile, "Bi", &participantaction[k][l]);
 
-                                                       for(m=0;m<10;m++)
-                                                               funpackf(tfile, "Bf Bf Bf", &participantfacing[k][l][m].x, &participantfacing[k][l][m].y, &participantfacing[k][l][m].z);
+                    for(m=0;m<10;m++)
+                        funpackf(tfile, "Bf Bf Bf", &participantfacing[k][l][m].x, &participantfacing[k][l][m].y, &participantfacing[k][l][m].z);
 
-                                                       funpackf(tfile, "Bf Bf",&dialoguecamerarotation[k][l],&dialoguecamerarotation2[k][l]);
-                                               }
-                                       }
-                               }
-                       }
-               }
-               else numdialogues=0;
-
-               if(player[0].numclothes)
-               {
-                       for(int k=0;k<player[0].numclothes;k++)
-                       {
-                               funpackf(tfile, "Bi", &templength);
-                               for(int l=0;l<templength;l++)
-                                       funpackf(tfile, "Bb", &player[0].clothes[k][l]);
-                               player[0].clothes[k][templength]='\0';
-                               funpackf(tfile, "Bf Bf Bf", &player[0].clothestintr[k], &player[0].clothestintg[k], &player[0].clothestintb[k]);
-                       }
-               }
+                    funpackf(tfile, "Bf Bf",&dialoguecamerarotation[k][l],&dialoguecamerarotation2[k][l]);
+                }
+            }
+               }else
+            numdialogues=0;
+
+        for(int k=0;k<player[0].numclothes;k++){
+            funpackf(tfile, "Bi", &templength);
+            for(int l=0;l<templength;l++)
+                funpackf(tfile, "Bb", &player[0].clothes[k][l]);
+            player[0].clothes[k][templength]='\0';
+            funpackf(tfile, "Bf Bf Bf", &player[0].clothestintr[k], &player[0].clothestintg[k], &player[0].clothestintb[k]);
+        }
 
                funpackf(tfile, "Bi", &environment);
 
                funpackf(tfile, "Bi", &objects.numobjects);
-               if(objects.numobjects)
-               {
-                       for(int i=0;i<objects.numobjects;i++)
-                       {
-                               funpackf(tfile, "Bi Bf Bf Bf Bf Bf Bf", &objects.type[i],&objects.rotation[i],&objects.rotation2[i], &objects.position[i].x, &objects.position[i].y, &objects.position[i].z,&objects.scale[i]);
-                               if(objects.type[i]==treeleavestype)objects.scale[i]=objects.scale[i-1];
-                       }
-               }
+        for(int i=0;i<objects.numobjects;i++){
+            funpackf(tfile, "Bi Bf Bf Bf Bf Bf Bf", &objects.type[i],&objects.rotation[i],&objects.rotation2[i], &objects.position[i].x, &objects.position[i].y, &objects.position[i].z,&objects.scale[i]);
+            if(objects.type[i]==treeleavestype)
+                objects.scale[i]=objects.scale[i-1];
+        }
 
-               if(mapvers>=7)
-               {
+               if(mapvers>=7){
                        funpackf(tfile, "Bi", &numhotspots);
-                       if(numhotspots)
-                       {
-                               for(int i=0;i<numhotspots;i++)
-                               {
-                                       funpackf(tfile, "Bi Bf Bf Bf Bf", &hotspottype[i],&hotspotsize[i],&hotspot[i].x,&hotspot[i].y,&hotspot[i].z);
-                                       funpackf(tfile, "Bi", &templength);
-                                       if(templength)
-                                               for(int l=0;l<templength;l++)
-                                                       funpackf(tfile, "Bb", &hotspottext[i][l]);
-                                       hotspottext[i][templength]='\0';
-                                       if(hotspottype[i]==-111)indemo=1;
-                               }
-                       }
-               }
-               else numhotspots=0;
+            for(int i=0;i<numhotspots;i++){
+                funpackf(tfile, "Bi Bf Bf Bf Bf", &hotspottype[i],&hotspotsize[i],&hotspot[i].x,&hotspot[i].y,&hotspot[i].z);
+                funpackf(tfile, "Bi", &templength);
+                if(templength)
+                    for(int l=0;l<templength;l++)
+                        funpackf(tfile, "Bb", &hotspottext[i][l]);
+                hotspottext[i][templength]='\0';
+                if(hotspottype[i]==-111)
+                    indemo=1;
+            }
+               }else
+            numhotspots=0;
 
-               if(visibleloading){loadscreencolor=4; LoadingScreen();}
+               if(visibleloading)
+            LoadingScreen();
 
-               if(!stealthloading)
-               {
+               if(!stealthloading){
                        objects.center=0;
                        for(int i=0;i<objects.numobjects;i++)
-                       {
                                objects.center+=objects.position[i];
-                       }
                        objects.center/=objects.numobjects;
 
 
-                       if(visibleloading){loadscreencolor=4; LoadingScreen();}
+                       if(visibleloading)
+                LoadingScreen();
 
                        float maxdistance=0;
                        float tempdist;
-                       int whichclosest;
-                       for(int i=0;i<objects.numobjects;i++)
-                       {
+                       //~ int whichclosest;
+                       for(int i=0;i<objects.numobjects;i++){
                                tempdist=findDistancefast(&objects.center,&objects.position[i]);
-                               if(tempdist>maxdistance)
-                               {
-                                       whichclosest=i;
+                               if(tempdist>maxdistance){
+                                       //~ whichclosest=i;
                                        maxdistance=tempdist;
                                }
                        }
                        objects.radius=fast_sqrt(maxdistance);
                }
 
-               if(visibleloading){loadscreencolor=4; LoadingScreen();}
+               if(visibleloading)
+            LoadingScreen();
                //mapcenter=objects.center;
                //mapradius=objects.radius;
 
                funpackf(tfile, "Bi", &numplayers);
                int howmanyremoved=0;
                bool removeanother=0;
-               if(numplayers>1&&numplayers<maxplayers)
-               {
-                       for(int i=1;i<numplayers;i++)
-                       {
-                               if(visibleloading){loadscreencolor=4; LoadingScreen();}
+               if(numplayers>1&&numplayers<maxplayers){
+                       for(int i=1;i<numplayers;i++){
+                               if(visibleloading)
+                    LoadingScreen();
                                removeanother=0;
 
                                funpackf(tfile, "Bi Bi Bf Bf Bf Bi",&player[i-howmanyremoved].whichskin,&player[i-howmanyremoved].creature, &player[i-howmanyremoved].coords.x,&player[i-howmanyremoved].coords.y,&player[i-howmanyremoved].coords.z,&player[i-howmanyremoved].num_weapons);
-                               if(mapvers>=5)funpackf(tfile, "Bi", &player[i-howmanyremoved].howactive);
-                               else player[i-howmanyremoved].howactive=typeactive;
-                               if(mapvers>=3)funpackf(tfile, "Bf",&player[i-howmanyremoved].scale);
-                               else player[i-howmanyremoved].scale=-1;
-                               if(mapvers>=11)funpackf(tfile, "Bb",&player[i-howmanyremoved].immobile);
-                               else player[i-howmanyremoved].immobile=0;
-                               if(mapvers>=12)funpackf(tfile, "Bf",&player[i-howmanyremoved].rotation);
-                               else player[i-howmanyremoved].rotation=0;
+                               if(mapvers>=5)
+                    funpackf(tfile, "Bi", &player[i-howmanyremoved].howactive);
+                               else
+                    player[i-howmanyremoved].howactive=typeactive;
+                               if(mapvers>=3)
+                    funpackf(tfile, "Bf",&player[i-howmanyremoved].scale);
+                               else
+                    player[i-howmanyremoved].scale=-1;
+                               if(mapvers>=11)
+                    funpackf(tfile, "Bb",&player[i-howmanyremoved].immobile);
+                               else
+                    player[i-howmanyremoved].immobile=0;
+                               if(mapvers>=12)
+                    funpackf(tfile, "Bf",&player[i-howmanyremoved].rotation);
+                               else
+                    player[i-howmanyremoved].rotation=0;
                                player[i-howmanyremoved].targetrotation=player[i-howmanyremoved].rotation;
                                if(player[i-howmanyremoved].num_weapons<0||player[i-howmanyremoved].num_weapons>5){
                                        removeanother=1;
                                        howmanyremoved++;
                                }
-                               if(!removeanother)
-                               {
-                                       if(player[i-howmanyremoved].num_weapons>0&&player[i-howmanyremoved].num_weapons<5)
-                                       {
-                                               for(int j=0;j<player[i-howmanyremoved].num_weapons;j++)
-                                               {
-                                                       player[i-howmanyremoved].weaponids[j]=weapons.numweapons;
-                                                       funpackf(tfile, "Bi", &weapons.type[player[i-howmanyremoved].weaponids[j]]);
-                                                       weapons.owner[player[i-howmanyremoved].weaponids[j]]=i;
-                                                       weapons.numweapons++;
+                               if(!removeanother){
+                                       if(player[i-howmanyremoved].num_weapons>0&&player[i-howmanyremoved].num_weapons<5){
+                                               for(int j=0;j<player[i-howmanyremoved].num_weapons;j++){
+                                                       player[i-howmanyremoved].weaponids[j]=weapons.size();
+                                                       int type;
+                                                       funpackf(tfile, "Bi", &type);
+                                                       weapons.push_back(Weapon(type,i));
                                                }
                                        }
                                        funpackf(tfile, "Bi", &player[i-howmanyremoved].numwaypoints);
                                        //player[i-howmanyremoved].numwaypoints=10;
-                                       for(int j=0;j<player[i-howmanyremoved].numwaypoints;j++)
-                                       {
+                                       for(int j=0;j<player[i-howmanyremoved].numwaypoints;j++){
                                                funpackf(tfile, "Bf", &player[i-howmanyremoved].waypoints[j].x);
                                                funpackf(tfile, "Bf", &player[i-howmanyremoved].waypoints[j].y);
                                                funpackf(tfile, "Bf", &player[i-howmanyremoved].waypoints[j].z);
-                                               if(mapvers>=5)funpackf(tfile, "Bi", &player[i-howmanyremoved].waypointtype[j]);
-                                               else player[i-howmanyremoved].waypointtype[j] = wpkeepwalking;
+                                               if(mapvers>=5)
+                            funpackf(tfile, "Bi", &player[i-howmanyremoved].waypointtype[j]);
+                                               else
+                            player[i-howmanyremoved].waypointtype[j] = wpkeepwalking;
                                        }
 
                                        funpackf(tfile, "Bi", &player[i-howmanyremoved].waypoint);
-                                       if(player[i-howmanyremoved].waypoint>player[i-howmanyremoved].numwaypoints-1)player[i-howmanyremoved].waypoint=0;
+                                       if(player[i-howmanyremoved].waypoint>player[i-howmanyremoved].numwaypoints-1)
+                        player[i-howmanyremoved].waypoint=0;
 
                                        funpackf(tfile, "Bf Bf Bf", &player[i-howmanyremoved].armorhead, &player[i-howmanyremoved].armorhigh, &player[i-howmanyremoved].armorlow);
                                        funpackf(tfile, "Bf Bf Bf", &player[i-howmanyremoved].protectionhead, &player[i-howmanyremoved].protectionhigh, &player[i-howmanyremoved].protectionlow);
                                        funpackf(tfile, "Bf Bf Bf", &player[i-howmanyremoved].metalhead, &player[i-howmanyremoved].metalhigh, &player[i-howmanyremoved].metallow);
                                        funpackf(tfile, "Bf Bf", &player[i-howmanyremoved].power, &player[i-howmanyremoved].speedmult);
 
-                                       if(mapvers>=4)funpackf(tfile, "Bf Bf Bf Bf", &headprop, &bodyprop, &armprop, &legprop);
-                                       else
-                                       {
+                                       if(mapvers>=4)
+                        funpackf(tfile, "Bf Bf Bf Bf", &headprop, &bodyprop, &armprop, &legprop);
+                                       else{
                                                headprop=1;
                                                bodyprop=1;
                                                armprop=1;
                                                legprop=1;
                                        }
-                                       if(player[i-howmanyremoved].creature==wolftype)
-                                       {
+                                       if(player[i-howmanyremoved].creature==wolftype){
                                                player[i-howmanyremoved].proportionhead=1.1*headprop;
                                                player[i-howmanyremoved].proportionbody=1.1*bodyprop;
                                                player[i-howmanyremoved].proportionarms=1.1*armprop;
                                                player[i-howmanyremoved].proportionlegs=1.1*legprop;
                                        }
 
-                                       if(player[i-howmanyremoved].creature==rabbittype)
-                                       {
+                                       if(player[i-howmanyremoved].creature==rabbittype){
                                                player[i-howmanyremoved].proportionhead=1.2*headprop;
                                                player[i-howmanyremoved].proportionbody=1.05*bodyprop;
                                                player[i-howmanyremoved].proportionarms=1.00*armprop;
@@ -1850,10 +1816,8 @@ void     Game::Loadlevel(const char *name){
                                        }
 
                                        funpackf(tfile, "Bi", &player[i-howmanyremoved].numclothes);
-                                       if(player[i-howmanyremoved].numclothes)
-                                       {
-                                               for(int k=0;k<player[i-howmanyremoved].numclothes;k++)
-                                               {
+                                       if(player[i-howmanyremoved].numclothes){
+                                               for(int k=0;k<player[i-howmanyremoved].numclothes;k++){
                                                        int templength;
                                                        funpackf(tfile, "Bi", &templength);
                                                        for(int l=0;l<templength;l++)
@@ -1865,23 +1829,21 @@ void    Game::Loadlevel(const char *name){
                                }
                        }
                }
-               if(visibleloading){loadscreencolor=4; LoadingScreen();}
+               if(visibleloading)
+            LoadingScreen();
 
                numplayers-=howmanyremoved;
                funpackf(tfile, "Bi", &numpathpoints);
                if(numpathpoints>30||numpathpoints<0)
                        numpathpoints=0;
-               if(numpathpoints)
-               {
-                       for(int j=0;j<numpathpoints;j++)
-                       {
-                               funpackf(tfile, "Bf Bf Bf Bi", &pathpoint[j].x,&pathpoint[j].y,&pathpoint[j].z,&numpathpointconnect[j]);
-                               for(int k=0;k<numpathpointconnect[j];k++){
-                                       funpackf(tfile, "Bi", &pathpointconnect[j][k]);
-                               }
-                       }
-               }
-               if(visibleloading){loadscreencolor=4; LoadingScreen();}
+        for(int j=0;j<numpathpoints;j++){
+            funpackf(tfile, "Bf Bf Bf Bi", &pathpoint[j].x,&pathpoint[j].y,&pathpoint[j].z,&numpathpointconnect[j]);
+            for(int k=0;k<numpathpointconnect[j];k++){
+                funpackf(tfile, "Bi", &pathpointconnect[j][k]);
+            }
+        }
+               if(visibleloading)
+            LoadingScreen();
 
                funpackf(tfile, "Bf Bf Bf Bf", &mapcenter.x,&mapcenter.y,&mapcenter.z,&mapradius);
 
@@ -1890,46 +1852,41 @@ void    Game::Loadlevel(const char *name){
             Setenvironment(environment);
                oldenvironment=environment;
 
-               if(!stealthloading)
-               {
+               if(!stealthloading){
                        int j=objects.numobjects;
                        objects.numobjects=0;
-                       for(int i=0;i<j;i++)
-                       {
-                               //if(objects.type[i]!=spiketype)
+                       for(int i=0;i<j;i++){
                                objects.MakeObject(objects.type[i],objects.position[i],objects.rotation[i],objects.rotation2[i],objects.scale[i]);
-                               if(visibleloading){loadscreencolor=4; LoadingScreen();}
+                               if(visibleloading)
+                    LoadingScreen();
                        }
 
-                       //if(skyboxtexture){
                        terrain.DoShadows();
-                       if(visibleloading){loadscreencolor=4; LoadingScreen();}
+                       if(visibleloading)
+                LoadingScreen();
                        objects.DoShadows();
-                       if(visibleloading){loadscreencolor=4; LoadingScreen();}
-                       /*}
-                       else terrain.DoLighting();
-                       */
+                       if(visibleloading)
+                LoadingScreen();
                }
 
                fclose(tfile);
 
-               oldlevel=whichlevel;
-
-
-               if(numplayers>maxplayers-1)numplayers=maxplayers-1;
-               for(int i=0;i<numplayers;i++)
-               {
-                       if(visibleloading){loadscreencolor=4; LoadingScreen();}
+               if(numplayers>maxplayers-1)
+            numplayers=maxplayers-1;
+               for(int i=0;i<numplayers;i++){
+                       if(visibleloading)
+                LoadingScreen();
                        player[i].burnt=0;
                        player[i].bled=0;
                        player[i].onfire=0;
-                       if(i==0||player[i].scale<0)player[i].scale=.2;
+                       if(i==0||player[i].scale<0)
+                player[i].scale=.2;
                        player[i].skeleton.free=0;
                        player[i].skeleton.id=i;
-                       //if(Random()%2==0)player[i].creature=wolftype;
-                       //else player[i].creature=rabbittype;
-                       if(i==0&&mapvers<9)player[i].creature=rabbittype;
-                       if(player[i].creature!=wolftype)player[i].skeleton.Load(
+                       if(i==0&&mapvers<9)
+                player[i].creature=rabbittype;
+                       if(player[i].creature!=wolftype){
+                player[i].skeleton.Load(
                     (char *)":Data:Skeleton:Basic Figure",
                     (char *)":Data:Skeleton:Basic Figurelow",
                     (char *)":Data:Skeleton:Rabbitbelt",
@@ -1942,8 +1899,7 @@ void      Game::Loadlevel(const char *name){
                     (char *)":Data:Models:Body7.solid",
                     (char *)":Data:Models:Bodylow.solid",
                     (char *)":Data:Models:Belt.solid",0);
-                       else
-                       {
+            }else{
                                if(player[i].creature!=wolftype){
                                        player[i].skeleton.Load(
                             (char *)":Data:Skeleton:Basic Figure",
@@ -1978,21 +1934,17 @@ void    Game::Loadlevel(const char *name){
                        }
 
 
-                       int texsize;
-                       texsize=512*512*3/texdetail/texdetail;
-                       //if(!player[i].loaded)player[i].skeleton.skinText = new GLubyte[texsize];
-                       //player[i].skeleton.skinText.resize(texsize);
+                       //~ int texsize;
+                       //~ texsize=512*512*3/texdetail/texdetail;
 
                        LoadTextureSave(creatureskin[player[i].creature][player[i].whichskin],&player[i].skeleton.drawmodel.textureptr,1,&player[i].skeleton.skinText[0],&player[i].skeleton.skinsize);
 
-                       if(player[i].numclothes)
-                       {
-                               for(int j=0;j<player[i].numclothes;j++)
-                               {
+                       if(player[i].numclothes){
+                               for(int j=0;j<player[i].numclothes;j++){
                                        tintr=player[i].clothestintr[j];
                                        tintg=player[i].clothestintg[j];
                                        tintb=player[i].clothestintb[j];
-                                       AddClothes((char *)player[i].clothes[j],0,1,&player[i].skeleton.skinText[0],&player[i].skeleton.skinsize);
+                                       AddClothes((char *)player[i].clothes[j],&player[i].skeleton.skinText[0]);
                                }
                                player[i].DoMipmaps();
                        }
@@ -2003,8 +1955,10 @@ void     Game::Loadlevel(const char *name){
                        player[i].targetframe=1;
                        player[i].target=0;
                        player[i].speed=1+(float)(Random()%100)/1000;
-                       if(difficulty==0)player[i].speed-=.2;
-                       if(difficulty==1)player[i].speed-=.1;
+                       if(difficulty==0)
+                player[i].speed-=.2;
+                       if(difficulty==1)
+                player[i].speed-=.1;
 
                        player[i].velocity=0;
                        player[i].oldcoords=player[i].coords;
@@ -2018,8 +1972,7 @@ void      Game::Loadlevel(const char *name){
                        player[i].aitype=passivetype;
                        player[i].madskills=0;
 
-                       if(i==0)
-                       {
+                       if(i==0){
                                player[i].proportionhead=1.2;
                                player[i].proportionbody=1.05;
                                player[i].proportionarms=1.00;
@@ -2029,35 +1982,18 @@ void    Game::Loadlevel(const char *name){
                        player[i].headless=0;
                        player[i].currentoffset=0;
                        player[i].targetoffset=0;
-                       /*player[i].armorhead=1;
-                       player[i].armorhigh=1;
-                       player[i].armorlow=1;
-                       player[i].protectionhead=1;
-                       player[i].protectionhigh=1;
-                       player[i].protectionlow=1;
-                       player[i].metalhead=1;
-                       player[i].metalhigh=1;
-                       player[i].metallow=1;
-                       player[i].power=1;
-                       player[i].speedmult=1;*/
 
                        player[i].damagetolerance=200;
 
-                       if(player[i].creature==wolftype)
-                       {
-                               /*player[i].proportionhead=1.1;
-                               player[i].proportionbody=1.1;
-                               player[i].proportionarms=1.1;
-                               player[i].proportionlegs=1.1;
-                               player[i].proportionlegs.y=1.1;*/
-                               if(i==0||player[i].scale<0)player[i].scale=.23;
-
+                       if(player[i].creature==wolftype){
+                               if(i==0||player[i].scale<0)
+                    player[i].scale=.23;
                                player[i].damagetolerance=300;
                        }
 
-                       if(visibleloading){loadscreencolor=4; LoadingScreen();}
-                       if(cellophane)
-                       {
+                       if(visibleloading)
+                LoadingScreen();
+                       if(cellophane){
                                player[i].proportionhead.z=0;
                                player[i].proportionbody.z=0;
                                player[i].proportionarms.z=0;
@@ -2103,24 +2039,21 @@ void    Game::Loadlevel(const char *name){
                player[0].weaponactive=-1;
 
                if(difficulty==1)
-               {
-                       //player[0].speedmult=1/.9;
                        player[0].power=1/.9;
-               }
 
                if(difficulty==0)
-               {
-                       //player[0].speedmult=1/.8;
                        player[0].power=1/.8;
-               }
 
-               //player[0].weaponstuck=1;
-
-               if(difficulty==1)player[0].damagetolerance=250;
-               if(difficulty==0)player[0].damagetolerance=300;
-               if(difficulty==0)player[0].armorhead*=1.5;
-               if(difficulty==0)player[0].armorhigh*=1.5;
-               if(difficulty==0)player[0].armorlow*=1.5;
+               if(difficulty==1)
+            player[0].damagetolerance=250;
+               if(difficulty==0)
+            player[0].damagetolerance=300;
+               if(difficulty==0)
+            player[0].armorhead*=1.5;
+               if(difficulty==0)
+            player[0].armorhigh*=1.5;
+               if(difficulty==0)
+            player[0].armorlow*=1.5;
                cameraloc=player[0].coords;
                cameraloc.y+=5;
                rotation=player[0].rotation;
@@ -2128,51 +2061,21 @@ void    Game::Loadlevel(const char *name){
                hawkcoords=player[0].coords;
                hawkcoords.y+=30;
 
-               if(visibleloading){loadscreencolor=4; LoadingScreen();}
-               //weapons.numweapons=numplayers;
-               for(int i=0;i<weapons.numweapons;i++)
-               {
-                       weapons.bloody[i]=0;
-                       weapons.blooddrip[i]=0;
-                       weapons.blooddripdelay[i]=0;
-                       weapons.onfire[i]=0;
-                       weapons.flamedelay[i]=0;
-                       weapons.damage[i]=0;
-                       //weapons.type[i]=sword;
-                       if(weapons.type[i]==sword){
-                               weapons.mass[i]=1.5;
-                               weapons.tipmass[i]=1;
-                               weapons.length[i]=.8;
-                       }
-                       if(weapons.type[i]==staff){
-                               weapons.mass[i]=2;
-                               weapons.tipmass[i]=1;
-                               weapons.length[i]=1.5;
-                       }
-                       if(weapons.type[i]==knife){
-                               weapons.mass[i]=1;
-                               weapons.tipmass[i]=1.2;
-                               weapons.length[i]=.25;
-                       }
-                       weapons.position[i]=-1000;
-                       weapons.tippoint[i]=-1000;
-               }
+               if(visibleloading)
+            LoadingScreen();
+               //~ for(int i=0;i<weapons.size();i++){
+               //~ }
                
                LOG("Starting background music...");
 
                OPENAL_StopSound(OPENAL_ALL);
-               if(environment==snowyenvironment)
-               {
+               if(environment==snowyenvironment){
                        if(ambientsound)
                          emit_stream_np(stream_wind);
-               }
-               else if(environment==desertenvironment)
-               {
+               }else if(environment==desertenvironment){
                        if(ambientsound)
                          emit_stream_np(stream_desertambient);
-               }
-               else if(environment==grassyenvironment)
-               {
+               }else if(environment==grassyenvironment){
                        if(ambientsound)
                          emit_stream_np(stream_wind, 100.);
                }
@@ -2182,442 +2085,13 @@ void   Game::Loadlevel(const char *name){
                oldmusicvolume[3]=0;
 
                if(!firstload)
-               {
                        firstload=1;
-               }
        }
        leveltime=0;
        loadingstuff=0;
        visibleloading=0;
 }
 
-
-
-void Game::MenuTick(){
-    //menu buttons
-    if(mainmenu==1||mainmenu==2){
-        if(Input::MouseClicked()&&selected==1){
-            if(!gameon){
-                fireSound(firestartsound);
-                flash();
-                //new game
-                if(accountactive) {
-                    mainmenu=5;
-                } else {
-                    mainmenu=7;
-                }
-                selected=-1;
-            }else{
-                //resume
-                mainmenu=0;
-                pause_sound(stream_music3);
-                resume_stream(music1);
-            }
-        }
-
-        if(Input::MouseClicked()&&selected==2){
-            fireSound();
-            flash();
-            //options
-            mainmenu=3;
-            if(newdetail>2)newdetail=detail;
-            if(newdetail<0)newdetail=detail;
-            if(newscreenwidth>3000)newscreenwidth=screenwidth;
-            if(newscreenwidth<0)newscreenwidth=screenwidth;
-            if(newscreenheight>3000)newscreenheight=screenheight;
-            if(newscreenheight<0)newscreenheight=screenheight;
-        }
-
-        if(Input::MouseClicked()&&selected==3){
-            fireSound();
-            flash();
-            if(!gameon){
-                //quit
-                tryquit=1;
-                pause_sound(stream_music3);
-            }else{
-                //end game
-                gameon=0;
-                mainmenu=1;
-            }
-        }
-    }
-    if(mainmenu==3){
-        if(Input::MouseClicked()){
-
-            if(selected!=-1)
-                fireSound();
-
-            switch(selected){
-                case 0: {
-                    extern SDL_Rect **resolutions;
-                    bool isCustomResolution = true;
-                    bool found = false;
-                    for(int i = 0; (!found) && (resolutions[i]); i++){
-                        if((resolutions[i]->w == screenwidth) && (resolutions[i]->h == screenwidth))
-                            isCustomResolution = false;
-
-                        if((resolutions[i]->w == newscreenwidth) && (resolutions[i]->h == newscreenheight)){
-                            i++;
-                            if(resolutions[i] != NULL){
-                                newscreenwidth = (int) resolutions[i]->w;
-                                newscreenheight = (int) resolutions[i]->h;
-                            }else if(isCustomResolution){
-                                if((screenwidth == newscreenwidth) && (screenheight == newscreenheight)){
-                                    newscreenwidth = (int) resolutions[0]->w;
-                                    newscreenheight = (int) resolutions[0]->h;
-                                }else{
-                                    newscreenwidth = screenwidth;
-                                    newscreenheight = screenheight;
-                                }
-                            }else{
-                                newscreenwidth = (int) resolutions[0]->w;
-                                newscreenheight = (int) resolutions[0]->h;
-                            }
-                            found = true;
-                        }
-                    }
-
-                    if(!found){
-                        newscreenwidth = (int) resolutions[0]->w;
-                        newscreenheight = (int) resolutions[0]->h;
-                    }
-                    } break;
-                case 1:
-                    newdetail++;
-                    if(newdetail>2)newdetail=0;
-                    break;
-                case 2:
-                    bloodtoggle++;
-                    if(bloodtoggle>2)bloodtoggle=0;
-                    break;
-                case 3:
-                    difficulty++;
-                    if(difficulty>2)difficulty=0;
-                    break;
-                case 4:
-                    ismotionblur=1-ismotionblur;
-                    break;
-                case 5:
-                    decals=1-decals;
-                    break;
-                case 6:
-                    musictoggle=1-musictoggle;
-
-                    if(!musictoggle){
-                        pause_sound(music1);
-                        pause_sound(stream_music2);
-                        pause_sound(stream_music3);
-
-                        for(int i=0;i<4;i++){
-                            oldmusicvolume[i]=0;
-                            musicvolume[i]=0;
-                        }
-                    }
-
-                    if(musictoggle)
-                      emit_stream_np(stream_music3);
-                    break;
-                case 7:
-                    flash();
-                    //options
-                    mainmenu=4;
-                    selected=-1;
-                    keyselect=-1;
-                    break;
-                case 8:
-                    flash();
-
-                    if(newdetail>2)newdetail=detail;
-                    if(newdetail<0)newdetail=detail;
-                    if(newscreenwidth<0)newscreenwidth=screenwidth;
-                    if(newscreenheight<0)newscreenheight=screenheight;
-
-                    SaveSettings(*this);
-                    if(mainmenu==3&&gameon)mainmenu=2;
-                    if(mainmenu==3&&!gameon)mainmenu=1;
-                    break;
-                case 9:
-                    invertmouse=1-invertmouse;
-                    break;
-                case 10:
-                    usermousesensitivity+=.2;
-                    if(usermousesensitivity>2)usermousesensitivity=.2;
-                    break;
-                case 11:
-                    volume+=.1f;
-                    if(volume>1.0001f)volume=0;
-                    OPENAL_SetSFXMasterVolume((int)(volume*255));
-                    break;
-                case 12:
-                    flash();
-                    
-                    newstereomode = stereomode;
-                    mainmenu=18;
-                    keyselect=-1;
-                    break;
-                case 13:
-                    showdamagebar=!showdamagebar;
-                    break;
-            }
-        }
-    }
-    if(mainmenu==4){
-        if(Input::MouseClicked()&&selected!=-1&&!waiting){
-            fireSound();
-            if(selected<9&&keyselect==-1)
-                keyselect=selected;
-            if(keyselect!=-1)
-                setKeySelected();
-            if(selected==9){
-                flash();
-
-                mainmenu=3;
-
-                if(newdetail>2)newdetail=detail;
-                if(newdetail<0)newdetail=detail;
-                if(newscreenwidth>3000)newscreenwidth=screenwidth;
-                if(newscreenwidth<0)newscreenwidth=screenwidth;
-                if(newscreenheight>3000)newscreenheight=screenheight;
-                if(newscreenheight<0)newscreenheight=screenheight;
-            }
-        }
-    }
-
-    if(mainmenu==5){
-
-        if(endgame==2){
-            accountactive->endGame();
-            endgame=0;
-        }
-
-        if(Input::MouseClicked()){
-            if((selected-7>=accountactive->getCampaignChoicesMade())){
-                fireSound();
-                flash();
-                startbonustotal=0;
-
-                loading=2;
-                loadtime=0;
-                targetlevel=7;
-                if(firstload) TickOnceAfter();
-                else LoadStuff();
-                whichchoice=selected-7-accountactive->getCampaignChoicesMade();
-                visibleloading=1;
-                stillloading=1;
-                Loadlevel(campaignmapname[campaignchoicewhich[selected-7-accountactive->getCampaignChoicesMade()]]);
-                //Loadlevel(campaignmapname[levelorder[selected-7]]);
-                campaign=1;
-                mainmenu=0;
-                gameon=1;
-                pause_sound(stream_music3);
-            }
-            if(selected>=1 && selected<=5){
-                fireSound();
-                flash();
-            }
-            switch(selected){
-                case 1:
-                    startbonustotal=0;
-
-                    loading=2;
-                    loadtime=0;
-                    targetlevel=-1;
-                    if(firstload)TickOnceAfter();
-                    if(!firstload)LoadStuff();
-                    else {
-                        Loadlevel(-1);
-                    }
-
-                    mainmenu=0;
-                    gameon=1;
-                    pause_sound(stream_music3);
-                    break;
-                case 2:
-                    mainmenu=9;
-                    break;
-                case 3:
-                    mainmenu=6;
-                    break;
-                case 4:
-                    if(mainmenu==5&&gameon)mainmenu=2;
-                    if(mainmenu==5&&!gameon)mainmenu=1;
-                    break;
-                case 5:
-                    mainmenu=7;
-                    break;
-            }
-        }
-    }
-    else if(mainmenu==9){
-        if(Input::MouseClicked()&&selected<numchallengelevels&&selected>=0&&selected<=accountactive->getProgress()){
-            fireSound();
-            flash();
-
-            startbonustotal=0;
-
-            loading=2;
-            loadtime=0;
-            targetlevel=selected;
-            if(firstload)TickOnceAfter();
-            if(!firstload)LoadStuff();
-            else {
-                Loadlevel(selected);
-            }
-            campaign=0;
-
-            mainmenu=0;
-            gameon=1;
-            pause_sound(stream_music3);
-        }
-        if(Input::MouseClicked()&&selected==numchallengelevels){
-            fireSound();
-            flash();
-            mainmenu=5;
-        }
-    }
-    if(mainmenu==10){
-        endgame=2;
-        if(Input::MouseClicked()&&selected==3){
-            fireSound();
-            flash();
-            mainmenu=5;
-        }
-    }
-
-    if(mainmenu==6){
-        if(Input::MouseClicked()) {
-            if(selected>-1){
-                fireSound();
-                if(selected==1) {
-                    flash();
-                    accountactive = Account::destroy(accountactive);
-                    mainmenu=7;
-                } else if(selected==2) {
-                    flash();
-                    mainmenu=5;
-                }
-            }
-        }
-    }
-    if(mainmenu==7){
-        if(Input::MouseClicked()) {
-            if(selected!=-1){
-                fireSound();
-                if(selected==0&&Account::getNbAccounts()<8){
-                    entername=1;
-                } else if (selected<Account::getNbAccounts()+1) {
-                    accountactive=Account::get(selected-1);
-                    mainmenu=5;
-                    flash();
-                } else if (selected==Account::getNbAccounts()+1) {
-                    flash();
-
-                    mainmenu=1;
-
-                    for(int j=0;j<255;j++){
-                        displaytext[0][j]=' ';
-                    }
-                    displaychars[0]=0;
-                    displayselected=0;
-                    entername=0;
-                }
-            }
-        }
-    }
-    if(mainmenu==8){
-        if(Input::MouseClicked()&&selected>-1){
-            fireSound();
-            flash();
-            if(selected<=2)
-                accountactive->setDifficulty(selected);
-            mainmenu=5;
-        }
-    }
-    if (mainmenu==18) {                        
-        if(Input::MouseClicked()&&selected==0) {
-            newstereomode = (StereoMode)(newstereomode + 1);
-            while(!CanInitStereo(newstereomode)) {
-                printf("Failed to initialize mode %s (%i)\n", StereoModeName(newstereomode), newstereomode);
-                newstereomode = (StereoMode)(newstereomode + 1);
-                if ( newstereomode >= stereoCount ) {
-                    newstereomode = stereoNone;
-                }
-            }
-        }
-        
-        if(Input::isKeyPressed(MOUSEBUTTON1)&&selected==1)
-            stereoseparation+=0.001;
-        if(Input::isKeyPressed(MOUSEBUTTON2)&&selected==1)
-            stereoseparation-=0.001;
-
-        if(Input::MouseClicked()&&selected==2) {
-            stereoreverse =! stereoreverse;
-        }
-        
-        if(Input::MouseClicked()&&selected==3) {
-            flash();
-
-            stereomode = newstereomode;
-            InitStereo(stereomode);
-            
-            mainmenu=3;
-        }
-    }
-
-    if(Input::isKeyDown(SDLK_q)&&Input::isKeyDown(SDLK_LMETA)){
-        tryquit=1;
-        if(mainmenu==3){
-            if(newdetail>2)newdetail=detail;
-            if(newdetail<0)newdetail=detail;
-            if(newscreenwidth<0)newscreenwidth=screenwidth;
-            if(newscreenheight<0)newscreenheight=screenheight;
-
-            SaveSettings(*this);
-        }
-    }
-
-    if(mainmenu==1||mainmenu==2){
-        if(loaddistrib>4)transition+=multiplier/8;
-        if(transition>1){
-            transition=0;
-            anim++;
-            if(anim>4)anim=0;
-            loaddistrib=0;
-        }
-    }
-    OPENAL_SetFrequency(channels[stream_music3], 22050);
-
-    if(entername) {
-        inputText(displaytext[0],&displayselected,&displaychars[0]);
-        if(!waiting) { // the input as finished
-            if(displaychars[0]){ // with enter
-                accountactive = Account::add(string(displaytext[0]));
-
-                mainmenu=8;
-
-                flash();
-
-                fireSound(firestartsound);
-
-                for(int i=0;i<255;i++){
-                    displaytext[0][i]=' ';
-                }
-                displaychars[0]=0;
-
-                displayselected=0;
-            }
-            entername=0;
-        }
-        
-        displayblinkdelay-=multiplier;
-        if(displayblinkdelay<=0){
-            displayblinkdelay=.3;
-            displayblink=1-displayblink;
-        }
-    }
-}
-
 void Game::doTutorial(){
     if(tutorialstagetime>tutorialmaxtime){
         tutorialstage++;
@@ -2765,25 +2239,19 @@ void Game::doTutorial(){
                 temp2.y=75;
                 temp2.z=447;
 
+                               Weapon w(knife,-1);
+                w.position=(temp+temp2)/2;
+                w.tippoint=(temp+temp2)/2;
 
-                weapons.owner[weapons.numweapons]=-1;
-                weapons.type[weapons.numweapons]=knife;
-                weapons.damage[weapons.numweapons]=0;
-                weapons.mass[weapons.numweapons]=1;
-                weapons.tipmass[weapons.numweapons]=1.2;
-                weapons.length[weapons.numweapons]=.25;
-                weapons.position[weapons.numweapons]=(temp+temp2)/2;
-                weapons.tippoint[weapons.numweapons]=(temp+temp2)/2;
-
-                weapons.velocity[weapons.numweapons]=0.1;
-                weapons.tipvelocity[weapons.numweapons]=0.1;
-                weapons.missed[weapons.numweapons]=1;
-                weapons.hitsomething[weapons.numweapons]=0;
-                weapons.freetime[weapons.numweapons]=0;
-                weapons.firstfree[weapons.numweapons]=1;
-                weapons.physics[weapons.numweapons]=1;
-
-                weapons.numweapons++;
+                w.velocity=0.1;
+                w.tipvelocity=0.1;
+                w.missed=1;
+                w.hitsomething=0;
+                w.freetime=0;
+                w.firstfree=1;
+                w.physics=1;
+
+                weapons.push_back(w);
             }
             break; case 40:
                 tutorialmaxtime=300;
@@ -2794,7 +2262,7 @@ void Game::doTutorial(){
             break; case 43:
                 tutorialmaxtime=300;
             break; case 44:
-                weapons.owner[0]=1;
+                weapons[0].owner=1;
                 player[0].weaponactive=-1;
                 player[0].num_weapons=0;
                 player[1].weaponactive=0;
@@ -2807,7 +2275,7 @@ void Game::doTutorial(){
 
                 tutorialmaxtime=300;
             break; case 45:
-                weapons.owner[0]=1;
+                weapons[0].owner=1;
                 player[0].weaponactive=-1;
                 player[0].num_weapons=0;
                 player[1].weaponactive=0;
@@ -2816,14 +2284,14 @@ void Game::doTutorial(){
 
                 tutorialmaxtime=300;
             break; case 46:
-                weapons.owner[0]=1;
+                weapons[0].owner=1;
                 player[0].weaponactive=-1;
                 player[0].num_weapons=0;
                 player[1].weaponactive=0;
                 player[1].num_weapons=1;
                 player[1].weaponids[0]=0;
 
-                weapons.type[0]=sword;
+                weapons[0].setType(sword);
 
                 tutorialmaxtime=300;
             break; case 47: {
@@ -2838,25 +2306,22 @@ void Game::doTutorial(){
                 temp2.y=75;
                 temp2.z=447;
 
-                weapons.owner[weapons.numweapons]=-1;
-                weapons.type[weapons.numweapons]=sword;
-                weapons.damage[weapons.numweapons]=0;
-                weapons.mass[weapons.numweapons]=1;
-                weapons.tipmass[weapons.numweapons]=1.2;
-                weapons.length[weapons.numweapons]=.25;
-                weapons.position[weapons.numweapons]=(temp+temp2)/2;
-                weapons.tippoint[weapons.numweapons]=(temp+temp2)/2;
-
-                weapons.velocity[weapons.numweapons]=0.1;
-                weapons.tipvelocity[weapons.numweapons]=0.1;
-                weapons.missed[weapons.numweapons]=1;
-                weapons.hitsomething[weapons.numweapons]=0;
-                weapons.freetime[weapons.numweapons]=0;
-                weapons.firstfree[weapons.numweapons]=1;
-                weapons.physics[weapons.numweapons]=1;
-
-                weapons.owner[0]=1;
-                weapons.owner[1]=0;
+                               Weapon w(sword,-1);
+                w.position=(temp+temp2)/2;
+                w.tippoint=(temp+temp2)/2;
+
+                w.velocity=0.1;
+                w.tipvelocity=0.1;
+                w.missed=1;
+                w.hitsomething=0;
+                w.freetime=0;
+                w.firstfree=1;
+                w.physics=1;
+                
+                weapons.push_back(w);
+
+                weapons[0].owner=1;
+                weapons[1].owner=0;
                 player[0].weaponactive=0;
                 player[0].num_weapons=1;
                 player[0].weaponids[0]=1;
@@ -2864,7 +2329,6 @@ void Game::doTutorial(){
                 player[1].num_weapons=1;
                 player[1].weaponids[0]=0;
 
-                weapons.numweapons++;
             }
             break; case 48:
                 canattack=0;
@@ -2873,8 +2337,8 @@ void Game::doTutorial(){
 
                 tutorialmaxtime=15;
 
-                weapons.owner[0]=1;
-                weapons.owner[1]=0;
+                weapons[0].owner=1;
+                weapons[1].owner=0;
                 player[0].weaponactive=0;
                 player[0].num_weapons=1;
                 player[0].weaponids[0]=1;
@@ -2882,10 +2346,12 @@ void Game::doTutorial(){
                 player[1].num_weapons=1;
                 player[1].weaponids[0]=0;
 
-                if(player[0].weaponactive!=-1)weapons.type[player[0].weaponids[player[0].weaponactive]]=staff;
-                else weapons.type[0]=staff;
+                if(player[0].weaponactive!=-1)
+                                       weapons[player[0].weaponids[player[0].weaponactive]].setType(staff);
+                else 
+                                       weapons[0].setType(staff);
 
-                weapons.numweapons++;
+                //~ weapons.size()++;
             break; case 49:
                 canattack=0;
                 cananger=0;
@@ -2893,20 +2359,20 @@ void Game::doTutorial(){
 
                 tutorialmaxtime=200;
 
-                weapons.position[1]=1000;
-                weapons.tippoint[1]=1000;
+                weapons[1].position=1000;
+                weapons[1].tippoint=1000;
+
+                //~ weapons.size()=1;
+                weapons[0].setType(knife);
 
-                weapons.numweapons=1;
-                weapons.owner[0]=0;
+                //~ weapons.size()++;
+                weapons[0].owner=0;
                 player[1].weaponactive=-1;
                 player[1].num_weapons=0;
                 player[0].weaponactive=0;
                 player[0].num_weapons=1;
                 player[0].weaponids[0]=0;
 
-                weapons.type[0]=knife;
-
-                weapons.numweapons++;
             break; case 50: {
                 tutorialmaxtime=8;
 
@@ -2927,16 +2393,7 @@ void Game::doTutorial(){
                 player[1].weaponstuck=-1;
                 player[1].weaponactive=-1;
 
-                weapons.numweapons=0;
-
-                weapons.owner[0]=-1;
-                weapons.velocity[0]=0.1;
-                weapons.tipvelocity[0]=-0.1;
-                weapons.missed[0]=1;
-                weapons.hitsomething[0]=0;
-                weapons.freetime[0]=0;
-                weapons.firstfree[0]=1;
-                weapons.physics[0]=1;
+                weapons.clear();
             }
             break; case 51:
                 tutorialmaxtime=80000;
@@ -2997,76 +2454,25 @@ void Game::doTutorial(){
         if(tutorialsuccess>=1)tutorialstagetime=tutorialmaxtime-3;
 
 
-        if(tutorialstagetime==tutorialmaxtime-3){
-            emit_sound_np(consolesuccesssound);
-        }
-
-        if(tutorialsuccess>=1){
-            if(tutorialstage==34||tutorialstage==35)
-                tutorialstagetime=tutorialmaxtime-1;
-        }
-    }
-
-    if(tutorialstage<14||tutorialstage>=50){
-        player[1].coords.y=300;
-        player[1].velocity=0;
-    }
-}
-
-
-
-void Game::doDebugKeys(){
-       float headprop,bodyprop,armprop,legprop;
-    if(debugmode){
-               if(Input::isKeyPressed(SDLK_v)){
-                       freeze=1-freeze;
-                       if(freeze){
-                               OPENAL_SetFrequency(OPENAL_ALL, 0.001);
-                       }
-               }
-
-               if(Input::isKeyPressed(SDLK_BACKQUOTE)){
-                       console=1-console;
-                       if(console){
-                               OPENAL_SetFrequency(OPENAL_ALL, 0.001);
-                       } else {
-                               freeze=0;
-                               waiting=false;
-                       }
-               }
-
-               if(console)
-            freeze=1;
-               if(console&&!Input::isKeyDown(SDLK_LMETA)){
-                       inputText(consoletext[0],&consoleselected,&consolechars[0]);
-                       if(!waiting) {
-                               archiveselected=0;
-                               cmd_dispatch(this, consoletext[0]);
-                               if(consolechars[0]>0){
-
-                                       for(int k=14;k>=1;k--){
-                                               for(int j=0;j<255;j++){
-                                                       consoletext[k][j]=consoletext[k-1][j];
-                                               }
-                                               consolechars[k]=consolechars[k-1];
-                                       }
-                                       for(int j=0;j<255;j++){
-                                               consoletext[0][j]=' ';
-                                       }
-                                       consolechars[0]=0;
-                                       consoleselected=0;
-                               }
-                       }
-
-                       consoleblinkdelay-=multiplier;
-                       if(consoleblinkdelay<=0){
-                               consoleblinkdelay=.3;
-                               consoleblink=1-consoleblink;
-                       }
-               }
-
+        if(tutorialstagetime==tutorialmaxtime-3){
+            emit_sound_np(consolesuccesssound);
+        }
+
+        if(tutorialsuccess>=1){
+            if(tutorialstage==34||tutorialstage==35)
+                tutorialstagetime=tutorialmaxtime-1;
+        }
+    }
 
+    if(tutorialstage<14||tutorialstage>=50){
+        player[1].coords.y=300;
+        player[1].velocity=0;
+    }
+}
 
+void Game::doDebugKeys(){
+       float headprop,bodyprop,armprop,legprop;
+    if(debugmode){
         if(Input::isKeyPressed(SDLK_h)){
             player[0].damagetolerance=200000;
             player[0].damage=0;
@@ -3088,25 +2494,12 @@ void Game::doDebugKeys(){
 
         if(Input::isKeyPressed(SDLK_x)&&!Input::isKeyDown(SDLK_LSHIFT)){
             if(player[0].num_weapons>0){
-                if(weapons.type[player[0].weaponids[0]]==sword)weapons.type[player[0].weaponids[0]]=staff;
-                else if(weapons.type[player[0].weaponids[0]]==staff)weapons.type[player[0].weaponids[0]]=knife;
-                else weapons.type[player[0].weaponids[0]]=sword;
-                if(weapons.type[player[0].weaponids[0]]==sword){
-                    weapons.mass[player[0].weaponids[0]]=1.5;
-                    weapons.tipmass[player[0].weaponids[0]]=1;
-                    weapons.length[player[0].weaponids[0]]=.8;
-                }
-                if(weapons.type[player[0].weaponids[0]]==staff){
-                    weapons.mass[player[0].weaponids[0]]=2;
-                    weapons.tipmass[player[0].weaponids[0]]=1;
-                    weapons.length[player[0].weaponids[0]]=1.5;
-                }
-
-                if(weapons.type[player[0].weaponids[0]]==knife){
-                    weapons.mass[player[0].weaponids[0]]=1;
-                    weapons.tipmass[player[0].weaponids[0]]=1.2;
-                    weapons.length[player[0].weaponids[0]]=.25;
-                }
+                if(weapons[player[0].weaponids[0]].getType()==sword)
+                                       weapons[player[0].weaponids[0]].setType(staff);
+                else if(weapons[player[0].weaponids[0]].getType()==staff)
+                                       weapons[player[0].weaponids[0]].setType(knife);
+                else
+                                       weapons[player[0].weaponids[0]].setType(sword);
             }
         }
 
@@ -3124,44 +2517,19 @@ void Game::doDebugKeys(){
                 }
             if(closest!=-1){
                 if(player[closest].num_weapons){
-                    if(weapons.type[player[closest].weaponids[0]]==sword)
-                        weapons.type[player[closest].weaponids[0]]=staff;
-                    else if(weapons.type[player[closest].weaponids[0]]==staff)
-                        weapons.type[player[closest].weaponids[0]]=knife;
-                    else weapons.type[player[closest].weaponids[0]]=sword;
-                    if(weapons.type[player[closest].weaponids[0]]==sword){
-                        weapons.mass[player[closest].weaponids[0]]=1.5;
-                        weapons.tipmass[player[closest].weaponids[0]]=1;
-                        weapons.length[player[closest].weaponids[0]]=.8;
-                    }
-                    if(weapons.type[player[0].weaponids[0]]==staff){
-                        weapons.mass[player[0].weaponids[0]]=2;
-                        weapons.tipmass[player[0].weaponids[0]]=1;
-                        weapons.length[player[0].weaponids[0]]=1.5;
-                    }
-                    if(weapons.type[player[closest].weaponids[0]]==knife){
-                        weapons.mass[player[closest].weaponids[0]]=1;
-                        weapons.tipmass[player[closest].weaponids[0]]=1.2;
-                        weapons.length[player[closest].weaponids[0]]=.25;
-                    }
+                    if(weapons[player[closest].weaponids[0]].getType()==sword)
+                        weapons[player[closest].weaponids[0]].setType(staff);
+                    else if(weapons[player[closest].weaponids[0]].getType()==staff)
+                        weapons[player[closest].weaponids[0]].setType(knife);
+                    else
+                                               weapons[player[closest].weaponids[0]].setType(sword);
                 }
                 if(!player[closest].num_weapons){
-                    player[closest].weaponids[0]=weapons.numweapons;
-                    weapons.owner[weapons.numweapons]=closest;
-                    weapons.type[weapons.numweapons]=knife;
-                    weapons.damage[weapons.numweapons]=0;
-                    weapons.numweapons++;
+                    player[closest].weaponids[0]=weapons.size();
+                    
+                    weapons.push_back(Weapon(knife,closest));
+                    
                     player[closest].num_weapons=1;
-                    if(weapons.type[player[closest].weaponids[0]]==sword){
-                        weapons.mass[player[closest].weaponids[0]]=1.5;
-                        weapons.tipmass[player[closest].weaponids[0]]=1;
-                        weapons.length[player[closest].weaponids[0]]=.8;
-                    }
-                    if(weapons.type[player[closest].weaponids[0]]==knife){
-                        weapons.mass[player[closest].weaponids[0]]=1;
-                        weapons.tipmass[player[closest].weaponids[0]]=1.2;
-                        weapons.length[player[closest].weaponids[0]]=.25;
-                    }
                 }
             }
         }
@@ -3213,7 +2581,7 @@ void Game::doDebugKeys(){
                     tintr=player[closest].clothestintr[i];
                     tintg=player[closest].clothestintg[i];
                     tintb=player[closest].clothestintb[i];
-                    AddClothes((char *)player[closest].clothes[i],0,1,&player[closest].skeleton.skinText[0],&player[closest].skeleton.skinsize);
+                    AddClothes((char *)player[closest].clothes[i],&player[closest].skeleton.skinText[0]);
                 }
                 player[closest].DoMipmaps();
             }
@@ -3483,9 +2851,6 @@ void Game::doDebugKeys(){
         }
 
         if(Input::isKeyPressed(SDLK_n)&&Input::isKeyDown(SDLK_LCTRL)){
-            int closest=-1;
-            float closestdist=-1;
-            float distance;
             for(int i=0;i<objects.numobjects;i++){
                 if(objects.type[i]==treeleavestype){
                     objects.scale[i]*=.9;
@@ -3510,7 +2875,8 @@ void Game::doDebugKeys(){
         //skip level
         if(whichlevel!=-2&&Input::isKeyPressed(SDLK_k)&&Input::isKeyDown(SDLK_LSHIFT)&&!editorenabled){
             targetlevel++;
-            if(targetlevel>numchallengelevels-1)targetlevel=0;
+            if(targetlevel>numchallengelevels-1)
+                targetlevel=0;
             loading=1;
             leveltime=5;
         }
@@ -3692,7 +3058,7 @@ void Game::doDebugKeys(){
                             tintr=player[numplayers].clothestintr[i];
                             tintg=player[numplayers].clothestintg[i];
                             tintb=player[numplayers].clothestintb[i];
-                            AddClothes((char *)player[numplayers].clothes[i],0,1,&player[numplayers].skeleton.skinText[0],&player[numplayers].skeleton.skinsize);
+                            AddClothes((char *)player[numplayers].clothes[i],&player[numplayers].skeleton.skinText[0]);
                         }
                     if(player[numplayers].numclothes){
                         player[numplayers].DoMipmaps();
@@ -3762,7 +3128,8 @@ void Game::doDebugKeys(){
 
             if(Input::isKeyPressed(SDLK_PERIOD)){
                 pathpointselected++;
-                if(pathpointselected>=numpathpoints)pathpointselected=-1;
+                if(pathpointselected>=numpathpoints)
+                    pathpointselected=-1;
             }
             if(Input::isKeyPressed(SDLK_COMMA)&&!Input::isKeyDown(SDLK_LSHIFT)){
                 pathpointselected--;
@@ -3855,8 +3222,6 @@ void Game::doDebugKeys(){
     }
 }
 
-
-
 void Game::doJumpReversals(){
     for(int k=0;k<numplayers;k++)
         for(int i=k;i<numplayers;i++){
@@ -4417,7 +3782,7 @@ void Game::doAttacks(){
                          player[k].targetanimation==walkanim||
                          player[k].targetanimation==sneakanim||
                          player[k].isCrouch())){
-                    const int attackweapon=player[k].weaponactive==-1?0:weapons.type[player[k].weaponids[player[k].weaponactive]];
+                    const int attackweapon=player[k].weaponactive==-1?0:weapons[player[k].weaponids[player[k].weaponactive]].getType();
                     //normal attacks (?)
                     player[k].hasvictim=0;
                     if(numplayers>1)
@@ -4753,7 +4118,7 @@ void Game::doAttacks(){
                                                          player[i].skeleton.longdead<300&&
                                                          player[k].lastattack!=spinkickanim&&
                                                          player[i].skeleton.free)&&
-                                                        (!player[i].dead||musictype!=stream_music2)){
+                                                        (!player[i].dead||musictype!=stream_fighttheme)){
                                                     player[k].targetanimation=dropkickanim;
                                                     for(int j=0;j<terrain.numdecals;j++){
                                                         if((terrain.decaltype[j]==blooddecal||terrain.decaltype[j]==blooddecalslow)&&
@@ -5065,8 +4430,6 @@ void Game::doPlayerCollisions(){
             }
 }
 
-
-
 void Game::doAI(int i){
     static bool connected;
     if(player[i].aitype!=playercontrolled&&indialogue==-1){
@@ -5353,10 +4716,10 @@ void Game::doAI(int i){
                         if(j==0||(player[j].dead&&player[j].bloodloss>0)){
                             float smelldistance=50;
                             if(j==0&&player[j].num_weapons>0){
-                                if(weapons.bloody[player[j].weaponids[0]])
+                                if(weapons[player[j].weaponids[0]].bloody)
                                     smelldistance=100;
                                 if(player[j].num_weapons==2)
-                                    if(weapons.bloody[player[j].weaponids[1]])
+                                    if(weapons[player[j].weaponids[1]].bloody)
                                         smelldistance=100;
                             }
                             if(j!=0)
@@ -5659,9 +5022,9 @@ void Game::doAI(int i){
                 if(player[i].ally<0){
                     int closest=-1;
                     float closestdist=-1;
-                    for(int k=0;k<weapons.numweapons;k++)
-                        if(weapons.owner[k]==-1){
-                            float distance=findDistancefast(&player[i].coords,&weapons.position[k]);
+                    for(int k=0;k<weapons.size();k++)
+                        if(weapons[k].owner==-1){
+                            float distance=findDistancefast(&player[i].coords,&weapons[k].position);
                             if(closestdist==-1||distance<closestdist){
                                 closestdist=distance;
                                 closest=k;
@@ -5683,13 +5046,13 @@ void Game::doAI(int i){
                     }
                 if(!player[0].dead)
                     if(player[i].ally>=0){
-                        if(weapons.owner[player[i].ally]!=-1||
-                                findDistancefast(&player[i].coords,&weapons.position[player[i].ally])>16){
+                        if(weapons[player[i].ally].owner!=-1||
+                                findDistancefast(&player[i].coords,&weapons[player[i].ally].position)>16){
                             player[i].aitype=attacktypecutoff;
                             player[i].lastseentime=1;
                         }
                         //TODO: factor these out as moveToward()
-                        player[i].targetrotation=roughDirectionTo(player[i].coords,weapons.position[player[i].ally]);
+                        player[i].targetrotation=roughDirectionTo(player[i].coords,weapons[player[i].ally].position);
                         player[i].lookrotation=player[i].targetrotation;
                         player[i].aiupdatedelay=.05;
                         player[i].forwardkeydown=1;
@@ -5744,7 +5107,7 @@ void Game::doAI(int i){
                     if(player[i].isIdle())
                         player[i].crouchkeydown=1;
                     if(player[0].targetanimation!=rabbitkickanim&&player[0].weaponactive!=-1){
-                        if(weapons.type[player[0].weaponids[0]]==knife){
+                        if(weapons[player[0].weaponids[0]].getType()==knife){
                             if(player[i].isIdle()||player[i].isCrouch()||player[i].isRun()||player[i].isFlip()){
                                 if(abs(Random()%2==0))
                                     setAnimation(i,backhandspringanim);
@@ -5771,14 +5134,14 @@ void Game::doAI(int i){
             }
             //go for weapon on the ground
             if(player[i].wentforweapon<3)
-                for(int k=0;k<weapons.numweapons;k++)
+                for(int k=0;k<weapons.size();k++)
                     if(player[i].creature!=wolftype)
                         if(player[i].num_weapons==0&&
-                                weapons.owner[k]==-1&&
-                                weapons.velocity[i].x==0&&
-                                weapons.velocity[i].z==0&&
-                                weapons.velocity[i].y==0){
-                            if(findDistancefast(&player[i].coords,&weapons.position[k])<16){
+                                weapons[k].owner==-1&&
+                                weapons[i].velocity.x==0&&
+                                weapons[i].velocity.z==0&&
+                                weapons[i].velocity.y==0){
+                            if(findDistancefast(&player[i].coords,&weapons[k].position)<16){
                                 player[i].wentforweapon++;
                                 player[i].lastchecktime=6;
                                 player[i].aitype=getweapontype;
@@ -6042,19 +5405,414 @@ void Game::doAI(int i){
     }
 }
 
+/*
+Values of mainmenu :
+1 Main menu
+2 Menu pause (resume/end game)
+3 Option menu
+4 Controls configuration menu
+5 Main game menu (choose level or challenge)
+6 Deleting user menu
+7 User managment menu (select/add)
+8 Choose difficulty menu
+9 Challenge level selection menu
+10 End of the campaign congratulation (is that really a menu?)
+11 Same that 9 ??? => unused
+18 stereo configuration
+*/
+    
+void Game::MenuTick(){
+    //menu buttons
+    
+    // some specific case where we do something even if the left mouse button is not pressed.
+       if((mainmenu==5) && (endgame==2)) {
+               accountactive->endGame();
+               endgame=0;
+       }
+       if(mainmenu==10)
+               endgame=2;
+       if( (mainmenu==18) && Input::isKeyPressed(MOUSEBUTTON2) && (selected==1) )
+               stereoseparation-=0.001;
+               
+    if(Input::MouseClicked() && (selected >= 0)) { // handling of the left mouse clic in menus
+               switch(mainmenu) {
+                       case 1:
+                       case 2:
+                               switch(selected) {
+                                       case 1:
+                                               if(gameon) { //resume
+                                                       mainmenu=0;
+                                                       pause_sound(stream_menutheme);
+                                                       resume_stream(leveltheme);
+                                               } else { //new game
+                                                       fireSound(firestartsound);
+                                                       flash();
+                                                       mainmenu=(accountactive?5:7);
+                                                       selected=-1;
+                                               }
+                                               break;
+                                       case 2: //options
+                                               fireSound();
+                                               flash();
+                                               mainmenu=3;
+                                               if(newdetail>2) newdetail=detail;
+                                               if(newdetail<0) newdetail=detail;
+                                               if(newscreenwidth>3000) newscreenwidth=screenwidth;
+                                               if(newscreenwidth<0) newscreenwidth=screenwidth;
+                                               if(newscreenheight>3000) newscreenheight=screenheight;
+                                               if(newscreenheight<0) newscreenheight=screenheight;
+                                               break;
+                                       case 3:
+                                               fireSound();
+                                               flash();
+                                               if(gameon){ //end game
+                                                       gameon=0;
+                                                       mainmenu=1;
+                                               } else { //quit
+                                                       tryquit=1;
+                                                       pause_sound(stream_menutheme);
+                                               }
+                                               break;
+                               }
+                               break;
+                       case 3:
+                               fireSound();
+                               bool isCustomResolution,found;
+                               switch(selected){
+                                       case 0:
+                                               extern SDL_Rect **resolutions;
+                                               isCustomResolution = true;
+                                               found = false;
+                                               for(int i = 0; (!found) && (resolutions[i]); i++) {
+                                                       if((resolutions[i]->w == screenwidth) && (resolutions[i]->h == screenwidth))
+                                                               isCustomResolution = false;
+
+                                                       if((resolutions[i]->w == newscreenwidth) && (resolutions[i]->h == newscreenheight)) {
+                                                               i++;
+                                                               if(resolutions[i] != NULL) {
+                                                                       newscreenwidth = (int) resolutions[i]->w;
+                                                                       newscreenheight = (int) resolutions[i]->h;
+                                                               } else if(isCustomResolution){
+                                                                       if((screenwidth == newscreenwidth) && (screenheight == newscreenheight)) {
+                                                                               newscreenwidth = (int) resolutions[0]->w;
+                                                                               newscreenheight = (int) resolutions[0]->h;
+                                                                       } else {
+                                                                               newscreenwidth = screenwidth;
+                                                                               newscreenheight = screenheight;
+                                                                       }
+                                                               } else {
+                                                                       newscreenwidth = (int) resolutions[0]->w;
+                                                                       newscreenheight = (int) resolutions[0]->h;
+                                                               }
+                                                               found = true;
+                                                       }
+                                               }
+
+                                               if(!found) {
+                                                       newscreenwidth = (int) resolutions[0]->w;
+                                                       newscreenheight = (int) resolutions[0]->h;
+                                               }
+                                               break;
+                                       case 1:
+                                               newdetail++;
+                                               if(newdetail>2) newdetail=0;
+                                               break;
+                                       case 2:
+                                               bloodtoggle++;
+                                               if(bloodtoggle>2) bloodtoggle=0;
+                                               break;
+                                       case 3:
+                                               difficulty++;
+                                               if(difficulty>2) difficulty=0;
+                                               break;
+                                       case 4:
+                                               ismotionblur = !ismotionblur;
+                                               break;
+                                       case 5:
+                                               decals = !decals;
+                                               break;
+                                       case 6:
+                                               musictoggle = !musictoggle;
+
+                                               if(musictoggle) {
+                                                 emit_stream_np(stream_menutheme);
+                                               } else {
+                                                       pause_sound(leveltheme);
+                                                       pause_sound(stream_fighttheme);
+                                                       pause_sound(stream_menutheme);
+
+                                                       for(int i=0;i<4;i++){
+                                                               oldmusicvolume[i]=0;
+                                                               musicvolume[i]=0;
+                                                       }
+                                               }
+
+                                               break;
+                                       case 7: // controls
+                                               flash();
+                                               mainmenu=4;
+                                               selected=-1;
+                                               keyselect=-1;
+                                               break;
+                                       case 8:
+                                               flash();
+                                               
+                                               SaveSettings(*this);
+                                               mainmenu=gameon?2:1;
+                                               break;
+                                       case 9:
+                                               invertmouse = !invertmouse;
+                                               break;
+                                       case 10:
+                                               usermousesensitivity+=.2;
+                                               if(usermousesensitivity>2) usermousesensitivity=.2;
+                                               break;
+                                       case 11:
+                                               volume+=.1f;
+                                               if(volume>1.0001f) volume=0;
+                                               OPENAL_SetSFXMasterVolume((int)(volume*255));
+                                               break;
+                                       case 12:
+                                               flash();
+                                               
+                                               newstereomode = stereomode;
+                                               mainmenu=18;
+                                               keyselect=-1;
+                                               break;
+                                       case 13:
+                                               showdamagebar = !showdamagebar;
+                                               break;
+                               }
+                               break;
+                       case 4:
+                               if(!waiting) {
+                                       fireSound();
+                                       if(selected<9 && keyselect==-1)
+                                               keyselect=selected;
+                                       if(keyselect!=-1)
+                                               setKeySelected();
+                                       if(selected==9){
+                                               flash();
+
+                                               mainmenu=3;
+                                       }
+                               }
+                               break;
+                       case 5:
+                               fireSound();
+                               flash();
+                               if((selected-NB_CAMPAIGN_MENU_ITEM-1 >= accountactive->getCampaignChoicesMade())) {
+                                       startbonustotal=0;
+
+                                       loading=2;
+                                       loadtime=0;
+                                       targetlevel=7;
+                                       if(firstload)
+                                               TickOnceAfter();
+                                       else
+                                               LoadStuff();
+                                       whichchoice=selected-NB_CAMPAIGN_MENU_ITEM-1-accountactive->getCampaignChoicesMade();
+                                       actuallevel=(accountactive->getCampaignChoicesMade()>0?campaignlevels[accountactive->getCampaignChoicesMade()-1].nextlevel[whichchoice]:0);
+                                       visibleloading=1;
+                                       stillloading=1;
+                                       Loadlevel(campaignlevels[actuallevel].mapname.c_str());
+                                       campaign=1;
+                                       mainmenu=0;
+                                       gameon=1;
+                                       pause_sound(stream_menutheme);
+                               }
+                               switch(selected){
+                                       case 1:
+                                               startbonustotal=0;
+
+                                               loading=2;
+                                               loadtime=0;
+                                               targetlevel=-1;
+                                               if(firstload) {
+                                                       TickOnceAfter();
+                                                       Loadlevel(-1);
+                                               } else
+                                                       LoadStuff();
+
+                                               mainmenu=0;
+                                               gameon=1;
+                                               pause_sound(stream_menutheme);
+                                               break;
+                                       case 2:
+                                               mainmenu=9;
+                                               break;
+                                       case 3:
+                                               mainmenu=6;
+                                               break;
+                                       case 4:
+                                               mainmenu=(gameon?2:1);
+                                               break;
+                                       case 5:
+                                               mainmenu=7;
+                                               break;
+                                       case 6:
+                                               vector<string> campaigns = ListCampaigns();
+                                               vector<string>::iterator c;
+                                               if ((c = find(campaigns.begin(),campaigns.end(),accountactive->getCurrentCampaign()))==campaigns.end()) {
+                                                       if(!campaigns.empty())
+                                                               accountactive->setCurrentCampaign(campaigns.front());
+                                               } else {
+                                                       c++;
+                                                       if(c==campaigns.end())
+                                                               c=campaigns.begin();
+                                                       accountactive->setCurrentCampaign(*c);
+                                               }
+                                               if(Mainmenuitems[7])
+                                                       glDeleteTextures(1,&Mainmenuitems[7]); // we delete the world texture so load campaign will reload it
+                                               Mainmenuitems[7] = 0;
+                                               LoadCampaign();
+                                               break;
+                               }
+                               break;
+                       case 6:
+                               fireSound();
+                               if(selected==1) {
+                                       flash();
+                                       accountactive = Account::destroy(accountactive);
+                                       mainmenu=7;
+                               } else if(selected==2) {
+                                       flash();
+                                       mainmenu=5;
+                               }
+                               break;
+                       case 7:
+                               fireSound();
+                               if(selected==0 && Account::getNbAccounts()<8){
+                                       entername=1;
+                               } else if (selected < Account::getNbAccounts()+1) {
+                                       flash();
+                                       mainmenu=5;
+                                       accountactive=Account::get(selected-1);
+                               } else if (selected == Account::getNbAccounts()+1) {
+                                       flash();
+                                       mainmenu=5;
+                                       for(int j=0;j<255;j++){
+                                               displaytext[0][j]=0;
+                                       }
+                                       displaychars[0]=0;
+                                       displayselected=0;
+                                       entername=0;
+                               }
+                               break;
+                       case 8:
+                               fireSound();
+                               flash();
+                               if(selected<=2)
+                                       accountactive->setDifficulty(selected);
+                               mainmenu=5;
+                               break;
+                       case 9:
+                               if(selected<numchallengelevels && selected<=accountactive->getProgress()){
+                                       fireSound();
+                                       flash();
+
+                                       startbonustotal=0;
+
+                                       loading=2;
+                                       loadtime=0;
+                                       targetlevel=selected;
+                                       if(firstload)TickOnceAfter();
+                                       if(!firstload)LoadStuff();
+                                       else Loadlevel(selected);
+                                       campaign=0;
+
+                                       mainmenu=0;
+                                       gameon=1;
+                                       pause_sound(stream_menutheme);
+                               }
+                               if(selected==numchallengelevels){
+                                       fireSound();
+                                       flash();
+                                       mainmenu=5;
+                               }
+                               break;
+                       case 10:
+                               if(selected==3){
+                                       fireSound();
+                                       flash();
+                                       mainmenu=5;
+                               }
+                               break;
+                       case 18:
+                               if(selected==1)
+                                       stereoseparation+=0.001;
+                               else {
+                                       fireSound();
+                                       if(selected==0){
+                                               newstereomode = (StereoMode)(newstereomode + 1);
+                                               while(!CanInitStereo(newstereomode)){
+                                                       printf("Failed to initialize mode %s (%i)\n", StereoModeName(newstereomode), newstereomode);
+                                                       newstereomode = (StereoMode)(newstereomode + 1);
+                                                       if(newstereomode >= stereoCount)
+                                                               newstereomode = stereoNone;
+                                               }
+                                       } else if(selected==2) {
+                                               stereoreverse = !stereoreverse;
+                                       } else if(selected==3) {
+                                               flash();
+                                               mainmenu=3;
+
+                                               stereomode = newstereomode;
+                                               InitStereo(stereomode);
+                                       }
+                               }
+                               break;
+               }
+       }
+
+    if(Input::isKeyDown(SDLK_q) && Input::isKeyDown(SDLK_LMETA)){
+        tryquit=1;
+        if(mainmenu==3) {
+            SaveSettings(*this);
+        }
+    }
+
+    OPENAL_SetFrequency(channels[stream_menutheme], 22050);
+
+    if(entername) {
+        inputText(displaytext[0],&displayselected,&displaychars[0]);
+        if(!waiting) { // the input as finished
+            if(displaychars[0]){ // with enter
+                accountactive = Account::add(string(displaytext[0]));
+
+                mainmenu=8;
+
+                flash();
+
+                fireSound(firestartsound);
+
+                for(int i=0;i<255;i++){
+                    displaytext[0][i]=0;
+                }
+                displaychars[0]=0;
 
+                displayselected=0;
+            }
+            entername=0;
+        }
+        
+        displayblinkdelay-=multiplier;
+        if(displayblinkdelay<=0){
+            displayblinkdelay=.3;
+            displayblink=1-displayblink;
+        }
+    }
+}
 
 void Game::Tick(){
        static XYZ facing,flatfacing;
        static int target;
 
-       int templength;
-
        for(int i=0;i<15;i++){
                displaytime[i]+=multiplier;
        }
 
-       keyboardfrozen=0;
+       keyboardfrozen=false;
     Input::Tick();
 
        if(Input::isKeyPressed(SDLK_F6)){
@@ -6092,78 +5850,97 @@ void Game::Tick(){
                emit_sound_np(consolefailsound, 128.);
        }
 
-       if(!console){
-               if(mainmenu&&endgame==1)mainmenu=10;
-        // menu back
-               if( (Input::isKeyPressed(SDLK_ESCAPE)
-                    ||(mainmenu==0
-                        &&((Input::isKeyPressed(jumpkey)
-                                ||Input::isKeyPressed(SDLK_SPACE)
-                                ||(campaign)))
-                        &&campaign
-                        &&winfreeze))
-                && (!mainmenu||gameon||mainmenu==3||mainmenu==4||mainmenu==5||mainmenu==6||(mainmenu==7&&!entername)||mainmenu==9||mainmenu==10)){
+    /*
+    Values of mainmenu :
+    1 Main menu
+    2 Menu pause (resume/end game)
+    3 Option menu
+    4 Controls configuration menu
+    5 Main game menu (choose level or challenge)
+    6 Deleting user menu
+    7 User managment menu (select/add)
+    8 Choose difficulty menu
+    9 Challenge level selection menu
+    10 End of the campaign congratulation (is that really a menu?)
+    11 Same that 9 ??? => unused
+    18 stereo configuration
+    */
+        
+       if(!console) {
+        //campaign over?
+               if(mainmenu&&endgame==1)
+            mainmenu=10;
+        //go to level select after completing a campaign level
+        if(campaign&&winfreeze&&mainmenu==0&&campaignlevels[actuallevel].choosenext==1) {
+            mainmenu=5;
+            gameon=0;
+            winfreeze=0;
+            fireSound();
+            flash();
+            if(musictoggle) {
+                OPENAL_SetFrequency(OPENAL_ALL, 0.001);
+                emit_stream_np(stream_menutheme);
+                pause_sound(leveltheme);
+            }
+                       LoadCampaign();
+        }
+        //escape key pressed
+        //TODO: there must be code somewhere else that handles clicking the Back button, merge it with this
+               if(Input::isKeyPressed(SDLK_ESCAPE)&&
+                (gameon||mainmenu==0||(mainmenu>=3&&mainmenu!=8&&!(mainmenu==7&&entername)))) {
                        selected=-1;
-                       if(mainmenu==1||mainmenu==2||mainmenu==0){
-                               if(mainmenu==0&&!winfreeze)mainmenu=2;
-                               else if(mainmenu==0&&winfreeze&&(campaignchoosenext[campaignchoicewhich[whichchoice]])==1)mainmenu=100;
-                               else if(mainmenu==0&&winfreeze){ }
-                               else if(mainmenu==1||mainmenu==2)mainmenu=0;
-                               if(mainmenu&&musictoggle){
-                                       if(mainmenu==1||mainmenu==2||mainmenu==100){
-                                               OPENAL_SetFrequency(OPENAL_ALL, 0.001);
-                                               emit_stream_np(stream_music3);
-                                               pause_sound(music1);
-                                       }
-                               }
-                               if(!mainmenu){
-                                       pause_sound(stream_music3);
-                                       resume_stream(music1);
-                               }
-                       }
+            if(mainmenu==0&&!winfreeze)
+                mainmenu=2; //pause
+            else if(mainmenu==1||mainmenu==2){
+                mainmenu=0; //unpause
+            }
+            //play menu theme
+            if(musictoggle&&(mainmenu==1||mainmenu==2)){
+                OPENAL_SetFrequency(OPENAL_ALL, 0.001);
+                emit_stream_np(stream_menutheme);
+                pause_sound(leveltheme);
+            }
+            //on resume, play level music
+            if(!mainmenu){
+                pause_sound(stream_menutheme);
+                resume_stream(leveltheme);
+            }
+            //finished with settings menu
                        if(mainmenu==3){
-                               if(newdetail>2)newdetail=detail;
-                               if(newdetail<0)newdetail=detail;
-                               if(newscreenwidth<0)newscreenwidth=screenwidth;
-                               if(newscreenheight<0)newscreenheight=screenheight;
                                SaveSettings(*this);
                        }
-                       if((mainmenu>=3 && mainmenu<=7)||mainmenu==9||mainmenu==10||mainmenu==100){
+            //effects
+                       if(mainmenu>=3&&mainmenu!=8){
                                fireSound();
                                flash();
                        }
-                       if(mainmenu==3&&gameon)mainmenu=2;
-                       if(mainmenu==3&&!gameon)mainmenu=1;
-                       if(mainmenu==5&&gameon)mainmenu=2;
-                       if(mainmenu==5&&!gameon)mainmenu=1;
-                       if(mainmenu==4)mainmenu=3;
-                       if(mainmenu==6)mainmenu=5;
-                       if(mainmenu==7)mainmenu=1;
-                       if(mainmenu==9)mainmenu=5;
-                       if(mainmenu==10)mainmenu=5;
-                       if(mainmenu==100){
-                               mainmenu=5;
-                               gameon=0;
-                               winfreeze=0;
-                       }
+            //go back
+            switch(mainmenu){
+                case 3: case 5:
+                    mainmenu=gameon?2:1; break;
+                case 4: case 18:
+                    mainmenu=3; break;
+                case 6: case 7: case 9: case 10:
+                    mainmenu=5; break;
+            }
                }
        }
 
-       if(mainmenu){
+       if(mainmenu) {
         MenuTick();
        }
 
-       if(!mainmenu){
+       if(!mainmenu) {
                if(hostile==1)hostiletime+=multiplier;
                else hostiletime=0;
                if(!winfreeze)leveltime+=multiplier;
 
         //keys
-               if(Input::isKeyDown(SDLK_ESCAPE)){
-                       chatting=0;
-                       console=0;
-                       freeze=0;
-                       displaychars[0]=0;
+               if(Input::isKeyPressed(SDLK_v)&&debugmode){
+                       freeze=1-freeze;
+                       if(freeze){
+                               OPENAL_SetFrequency(OPENAL_ALL, 0.001);
+                       }
                }
 
                if(Input::isKeyPressed(chatkey)&&!console&&!chatting&&debugmode)
@@ -6173,9 +5950,8 @@ void Game::Tick(){
                        inputText(displaytext[0],&displayselected,&displaychars[0]);
                        if(!waiting) {
                                if(displaychars[0]){
-                                       for(int j=0;j<255;j++){
-                                               displaytext[0][j]=' ';
-                                       }
+                                       for(int j=0;j<255;j++)
+                                               displaytext[0][j]=0;
                                        displaychars[0]=0;
                                        displayselected=0;
                                }       
@@ -6189,16 +5965,51 @@ void Game::Tick(){
                        }
                }
                if(chatting)
-            keyboardfrozen=1;
+            keyboardfrozen=true;
+
+               if(Input::isKeyPressed(SDLK_BACKQUOTE)&&debugmode){
+                       console=!console;
+                       if(console){
+                               OPENAL_SetFrequency(OPENAL_ALL, 0.001);
+                       } else {
+                               freeze=0;
+                               waiting=false;
+                       }
+               }
+
+               if(console)
+            freeze=1;
+               if(console&&!Input::isKeyDown(SDLK_LMETA)){
+                       inputText(consoletext[0],&consoleselected,&consolechars[0]);
+                       if(!waiting) {
+                               archiveselected=0;
+                               if(consolechars[0]>0){
+                    consoletext[0][consolechars[0]]=' ';
+                    cmd_dispatch(this, consoletext[0]);
+                                       for(int k=14;k>=1;k--){
+                                               for(int j=0;j<255;j++)
+                                                       consoletext[k][j]=consoletext[k-1][j];
+                                               consolechars[k]=consolechars[k-1];
+                                       }
+                                       for(int j=0;j<255;j++)
+                                               consoletext[0][j]=0;
+                                       consolechars[0]=0;
+                                       consoleselected=0;
+                               }
+                       }
+
+                       consoleblinkdelay-=multiplier;
+                       if(consoleblinkdelay<=0){
+                               consoleblinkdelay=.3;
+                               consoleblink=1-consoleblink;
+                       }
+               }
+
+
 
                if(Input::isKeyDown(SDLK_q)&&Input::isKeyDown(SDLK_LMETA)){
                        tryquit=1;
-                       if(mainmenu==3){
-                               if(newdetail>2)newdetail=detail;
-                               if(newdetail<0)newdetail=detail;
-                               if(newscreenwidth<0)newscreenwidth=screenwidth;
-                               if(newscreenheight<0)newscreenheight=screenheight;
-
+                       if(mainmenu==3) {
                                SaveSettings(*this);
                        }
                }
@@ -6216,11 +6027,16 @@ void Game::Tick(){
                if((Input::isKeyPressed(jumpkey)||Input::isKeyPressed(SDLK_SPACE))&&!campaign)
                        if(winfreeze)
                 winfreeze=0;
-               if((Input::isKeyDown(SDLK_ESCAPE))&&!campaign&&gameon)
-                       if(winfreeze){
+               if((Input::isKeyDown(SDLK_ESCAPE))&&!campaign&&gameon){
+            if(console){
+                console=false;
+                freeze=0;
+            } else if(winfreeze) {
                                mainmenu=9;
                                gameon=0;
                        }
+        }
+
 
 
         //TODO: what is this test?
@@ -6573,8 +6389,6 @@ void Game::Tick(){
                 }
             }
 
-            static float keyrefreshdelay=0,bigrefreshdelay=0;
-
             if(!player[0].jumpkeydown){
                 player[0].jumptogglekeydown=0;
             }
@@ -6597,8 +6411,6 @@ void Game::Tick(){
 
                 hawkcalldelay=16+abs(Random()%8);
             }
-            static float temptexdetail;
-
 
             doDebugKeys();
 
@@ -6712,8 +6524,6 @@ void Game::Tick(){
                         player[i].targetheadrotation2=pitch(participantfacing[whichdialogue][indialogue][i]);
                     }
 
-                    bool pause;
-
                     if(leveltime<.5)
                         numenvsounds=0;
 
@@ -6819,13 +6629,13 @@ void Game::Tick(){
                                  player[i].isFlip()||
                                  player[i].isFlip()||
                                  player[i].aitype!=playercontrolled)){
-                            for(int j=0;j<weapons.numweapons;j++){
-                                if((weapons.velocity[j].x==0&&weapons.velocity[j].y==0&&weapons.velocity[j].z==0||
+                            for(int j=0;j<weapons.size();j++){
+                                if((weapons[j].velocity.x==0&&weapons[j].velocity.y==0&&weapons[j].velocity.z==0||
                                             player[i].aitype==playercontrolled)&&
-                                        weapons.owner[j]==-1&&
+                                        weapons[j].owner==-1&&
                                         player[i].weaponactive==-1)
-                                    if(findDistancefastflat(&player[i].coords,&weapons.position[j])<2){
-                                        if(findDistancefast(&player[i].coords,&weapons.position[j])<2){
+                                    if(findDistancefastflat(&player[i].coords,&weapons[j].position)<2){
+                                        if(findDistancefast(&player[i].coords,&weapons[j].position)<2){
                                             if(player[i].isCrouch()||
                                                     player[i].targetanimation==sneakanim||
                                                     player[i].isRun()||
@@ -6833,25 +6643,25 @@ void Game::Tick(){
                                                     player[i].aitype!=playercontrolled){
                                                 player[i].throwtogglekeydown=1;
                                                 setAnimation(i,crouchremoveknifeanim);
-                                                player[i].targetrotation=roughDirectionTo(player[i].coords,weapons.position[j]);
+                                                player[i].targetrotation=roughDirectionTo(player[i].coords,weapons[j].position);
                                                 player[i].hasvictim=0;
                                             }
                                             if(player[i].targetanimation==rollanim||player[i].targetanimation==backhandspringanim){
                                                 player[i].throwtogglekeydown=1;
                                                 player[i].hasvictim=0;
 
-                                                if((weapons.velocity[j].x==0&&weapons.velocity[j].y==0&&weapons.velocity[j].z==0||
+                                                if((weapons[j].velocity.x==0&&weapons[j].velocity.y==0&&weapons[j].velocity.z==0||
                                                                 player[i].aitype==playercontrolled)&&
-                                                            weapons.owner[j]==-1||
+                                                            weapons[j].owner==-1||
                                                         player[i].victim&&
-                                                        weapons.owner[j]==player[i].victim->id)
-                                                    if(findDistancefastflat(&player[i].coords,&weapons.position[j])<2&&player[i].weaponactive==-1)
-                                                        if(findDistancefast(&player[i].coords,&weapons.position[j])<1||player[i].victim){
-                                                            if(weapons.type[j]!=staff)
+                                                        weapons[j].owner==player[i].victim->id)
+                                                    if(findDistancefastflat(&player[i].coords,&weapons[j].position)<2&&player[i].weaponactive==-1)
+                                                        if(findDistancefast(&player[i].coords,&weapons[j].position)<1||player[i].victim){
+                                                            if(weapons[j].getType()!=staff)
                                                                 emit_sound_at(knifedrawsound, player[i].coords, 128.);
 
                                                             player[i].weaponactive=0;
-                                                            weapons.owner[j]=player[i].id;
+                                                            weapons[j].owner=player[i].id;
                                                             if(player[i].num_weapons>0)
                                                                 player[i].weaponids[player[i].num_weapons]=player[i].weaponids[0];
                                                             player[i].num_weapons++;
@@ -6861,31 +6671,31 @@ void Game::Tick(){
                                         }else if((player[i].isIdle()||
                                                     player[i].isFlip()||
                                                     player[i].aitype!=playercontrolled)&&
-                                                findDistancefast(&player[i].coords,&weapons.position[j])<5&&
-                                                player[i].coords.y<weapons.position[j].y){
+                                                findDistancefast(&player[i].coords,&weapons[j].position)<5&&
+                                                player[i].coords.y<weapons[j].position.y){
                                             if(!player[i].isFlip()){
                                                 player[i].throwtogglekeydown=1;
                                                 setAnimation(i,removeknifeanim);
-                                                player[i].targetrotation=roughDirectionTo(player[i].coords,weapons.position[j]);
+                                                player[i].targetrotation=roughDirectionTo(player[i].coords,weapons[j].position);
                                             }
                                             if(player[i].isFlip()){
                                                 player[i].throwtogglekeydown=1;
                                                 player[i].hasvictim=0;
 
-                                                for(int k=0;k<weapons.numweapons;k++){
+                                                for(int k=0;k<weapons.size();k++){
                                                     if(player[i].weaponactive==-1)
-                                                        if((weapons.velocity[k].x==0&&weapons.velocity[k].y==0&&weapons.velocity[k].z==0||
+                                                        if((weapons[k].velocity.x==0&&weapons[k].velocity.y==0&&weapons[k].velocity.z==0||
                                                                         player[i].aitype==playercontrolled)&&
-                                                                    weapons.owner[k]==-1||
+                                                                    weapons[k].owner==-1||
                                                                 player[i].victim&&
-                                                                 weapons.owner[k]==player[i].victim->id)
-                                                            if(findDistancefastflat(&player[i].coords,&weapons.position[k])<3&&
+                                                                 weapons[k].owner==player[i].victim->id)
+                                                            if(findDistancefastflat(&player[i].coords,&weapons[k].position)<3&&
                                                                     player[i].weaponactive==-1){
-                                                                if(weapons.type[k]!=staff)
+                                                                if(weapons[k].getType()!=staff)
                                                                     emit_sound_at(knifedrawsound, player[i].coords, 128.);
 
                                                                 player[i].weaponactive=0;
-                                                                weapons.owner[k]=player[i].id;
+                                                                weapons[k].owner=player[i].id;
                                                                 if(player[i].num_weapons>0)
                                                                     player[i].weaponids[player[i].num_weapons]=player[i].weaponids[0];
                                                                 player[i].num_weapons++;
@@ -6935,14 +6745,14 @@ void Game::Tick(){
                                                                 }
                                                             }
                                                             if(!fleshstuck){
-                                                                if(weapons.type[k]!=staff)
+                                                                if(weapons[k].getType()!=staff)
                                                                   emit_sound_at(knifedrawsound, player[i].coords, 128.);
                                                             }
                                                             if(fleshstuck)
                                                               emit_sound_at(fleshstabremovesound, player[i].coords, 128.);
 
                                                             player[i].weaponactive=0;
-                                                            if(weapons.owner[k]!=-1){
+                                                            if(weapons[k].owner!=-1){
                                                                 if(player[i].victim->num_weapons==1)player[i].victim->num_weapons=0;
                                                                 else player[i].victim->num_weapons=1;
 
@@ -6961,12 +6771,12 @@ void Game::Tick(){
                                                                 Normalise(&relative);
                                                                 XYZ footvel,footpoint;
                                                                 footvel=0;
-                                                                footpoint=weapons.position[k];
+                                                                footpoint=weapons[k].position;
                                                                 if(player[i].victim->weaponstuck!=-1){
                                                                     if(player[i].victim->weaponids[player[i].victim->weaponstuck]==k){
                                                                         if(bloodtoggle)Sprite::MakeSprite(cloudimpactsprite, footpoint,footvel, 1,0,0, .8, .3);
-                                                                        weapons.bloody[k]=2;
-                                                                        weapons.blooddrip[k]=5;
+                                                                        weapons[k].bloody=2;
+                                                                        weapons[k].blooddrip=5;
                                                                         player[i].victim->weaponstuck=-1;
                                                                         player[i].victim->bloodloss+=2000;
                                                                         player[i].victim->DoDamage(2000);
@@ -6985,7 +6795,7 @@ void Game::Tick(){
                                                                 playerJoint(player[i].victim,rightshoulder).velocity+=relative*6;
                                                                 playerJoint(player[i].victim,leftshoulder).velocity+=relative*6;
                                                             }
-                                                            weapons.owner[k]=i;
+                                                            weapons[k].owner=i;
                                                             if(player[i].num_weapons>0){
                                                                 player[i].weaponids[player[i].num_weapons]=player[i].weaponids[0];
                                                             }
@@ -6998,7 +6808,7 @@ void Game::Tick(){
                             }
                         }
                         if(player[i].weaponactive!=-1&&player[i].aitype==playercontrolled){
-                            if(weapons.type[player[i].weaponids[0]]==knife){
+                            if(weapons[player[i].weaponids[0]].getType()==knife){
                                 if(player[i].isIdle()||
                                         player[i].isRun()||
                                         player[i].isCrouch()||
@@ -7026,18 +6836,18 @@ void Game::Tick(){
                                                                     player[i].throwtogglekeydown=1;
                                                                     player[i].victim=&player[j];
                                                                     XYZ aim;
-                                                                    weapons.owner[player[i].weaponids[0]]=-1;
+                                                                    weapons[player[i].weaponids[0]].owner=-1;
                                                                     aim=player[i].victim->coords+DoRotation(playerJoint(player[i].victim,abdomen).position,0,player[i].victim->rotation,0)*player[i].victim->scale+player[i].victim->velocity*findDistance(&player[i].victim->coords,&player[i].coords)/50-(player[i].coords+DoRotation(playerJoint(i,righthand).position,0,player[i].rotation,0)*player[i].scale);
                                                                     Normalise(&aim);
 
                                                                     aim=DoRotation(aim,(float)abs(Random()%30)-15,(float)abs(Random()%30)-15,0);
 
-                                                                    weapons.velocity[player[i].weaponids[0]]=aim*50;
-                                                                    weapons.tipvelocity[player[i].weaponids[0]]=aim*50;
-                                                                    weapons.missed[player[i].weaponids[0]]=0;
-                                                                    weapons.freetime[player[i].weaponids[0]]=0;
-                                                                    weapons.firstfree[player[i].weaponids[0]]=1;
-                                                                    weapons.physics[player[i].weaponids[0]]=0;
+                                                                    weapons[player[i].weaponids[0]].velocity=aim*50;
+                                                                    weapons[player[i].weaponids[0]].tipvelocity=aim*50;
+                                                                    weapons[player[i].weaponids[0]].missed=0;
+                                                                    weapons[player[i].weaponids[0]].freetime=0;
+                                                                    weapons[player[i].weaponids[0]].firstfree=1;
+                                                                    weapons[player[i].weaponids[0]].physics=0;
                                                                     player[i].num_weapons--;
                                                                     if(player[i].num_weapons){
                                                                         player[i].weaponids[0]=player[i].weaponids[player[i].num_weapons];
@@ -7052,14 +6862,14 @@ void Game::Tick(){
                         if(player[i].weaponactive!=-1&&player[i].aitype==playercontrolled){
                             if(player[i].isCrouch()||player[i].targetanimation==sneakanim){
                                 player[i].throwtogglekeydown=1;
-                                weapons.owner[player[i].weaponids[0]]=-1;
-                                weapons.velocity[player[i].weaponids[0]]=player[i].velocity*.2;
-                                if(weapons.velocity[player[i].weaponids[0]].x==0)weapons.velocity[player[i].weaponids[0]].x=.1;
-                                weapons.tipvelocity[player[i].weaponids[0]]=weapons.velocity[player[i].weaponids[0]];
-                                weapons.missed[player[i].weaponids[0]]=1;
-                                weapons.freetime[player[i].weaponids[0]]=0;
-                                weapons.firstfree[player[i].weaponids[0]]=1;
-                                weapons.physics[player[i].weaponids[0]]=1;
+                                weapons[player[i].weaponids[0]].owner=-1;
+                                weapons[player[i].weaponids[0]].velocity=player[i].velocity*.2;
+                                if(weapons[player[i].weaponids[0]].velocity.x==0)weapons[player[i].weaponids[0]].velocity.x=.1;
+                                weapons[player[i].weaponids[0]].tipvelocity=weapons[player[i].weaponids[0]].velocity;
+                                weapons[player[i].weaponids[0]].missed=1;
+                                weapons[player[i].weaponids[0]].freetime=0;
+                                weapons[player[i].weaponids[0]].firstfree=1;
+                                weapons[player[i].weaponids[0]].physics=1;
                                 player[i].num_weapons--;
                                 if(player[i].num_weapons){
                                     player[i].weaponids[0]=player[i].weaponids[player[i].num_weapons];
@@ -7076,20 +6886,20 @@ void Game::Tick(){
                     }
 
                     //draw weapon
-                    if(i==0||!player[0].dead||player[i].weaponactive!=-1)
+                    if(i==0||!player[0].dead||player[i].weaponactive!=-1) {
                         if(player[i].drawkeydown&&!player[i].drawtogglekeydown||
                                 player[i].num_weapons==2&&
                                 player[i].weaponactive==-1&&
                                 player[i].isIdle()||
                                 player[0].dead&&
                                 player[i].weaponactive!=-1&&
-                                i!=0){
-                            bool isgood=1;
+                                i!=0) {
+                            bool isgood=true;
                             if(player[i].weaponactive!=-1)
-                                if(weapons.type[player[i].weaponids[player[i].weaponactive]]==staff)
-                                    isgood=0;
+                                if(weapons[player[i].weaponids[player[i].weaponactive]].getType()==staff)
+                                    isgood=false;
                             if(isgood&&player[i].creature!=wolftype){
-                                if(player[i].isIdle()&&player[i].num_weapons&&weapons.type[player[i].weaponids[0]]==knife){
+                                if(player[i].isIdle()&&player[i].num_weapons&&weapons[player[i].weaponids[0]].getType()==knife){
                                     setAnimation(i,drawrightanim);
                                     player[i].drawtogglekeydown=1;
                                 }
@@ -7098,47 +6908,46 @@ void Game::Tick(){
                                              player[0].weaponactive!=-1&&
                                              player[i].isRun()))&&
                                         player[i].num_weapons&&
-                                        weapons.type[player[i].weaponids[0]]==sword){
+                                        weapons[player[i].weaponids[0]].getType()==sword){
                                     setAnimation(i,drawleftanim);
                                     player[i].drawtogglekeydown=1;
                                 }
-                                if(player[i].isCrouch()&&player[i].num_weapons&&weapons.type[player[i].weaponids[0]]==knife){
+                                if(player[i].isCrouch()&&player[i].num_weapons&&weapons[player[i].weaponids[0]].getType()==knife){
                                     setAnimation(i,crouchdrawrightanim);
                                     player[i].drawtogglekeydown=1;
                                 }
                             }
                         }
-                    //clean weapon
-                    if(player[i].isCrouch()&&
-                            weapons.bloody[player[i].weaponids[player[i].weaponactive]]&&
-                            bloodtoggle&&
-                            player[i].onterrain&&
-                            player[i].num_weapons&&
-                            player[i].weaponactive!=-1&&
-                            player[i].attackkeydown){
-                        if(weapons.bloody[player[i].weaponids[player[i].weaponactive]]&&
-                                player[i].onterrain&&
-                                bloodtoggle&&musictype!=stream_music2){
-                            if(weapons.type[player[i].weaponids[player[i].weaponactive]]==knife)
-                                setAnimation(i,crouchstabanim);
-                            if(weapons.type[player[i].weaponids[player[i].weaponactive]]==sword)
-                                setAnimation(i,swordgroundstabanim);
-                            player[i].hasvictim=0;
-                            //player[i].attacktogglekeydown=1;
-                        }
                     }
+                    
+                    //clean weapon
+                    if(player[i].weaponactive!=-1) {
+                                               if (player[i].isCrouch()&&
+                                                       weapons[player[i].weaponids[player[i].weaponactive]].bloody&&
+                                                       bloodtoggle&&
+                                                       player[i].onterrain&&
+                                                       player[i].num_weapons&&
+                                                       player[i].attackkeydown&&
+                                                       musictype!=stream_fighttheme) {
+                                                               if(weapons[player[i].weaponids[player[i].weaponactive]].getType()==knife)
+                                                                       setAnimation(i,crouchstabanim);
+                                                               if(weapons[player[i].weaponids[player[i].weaponactive]].getType()==sword)
+                                                                       setAnimation(i,swordgroundstabanim);
+                                                               player[i].hasvictim=0;
+                                               }
+                                       }
 
                     if(!player[i].drawkeydown)
                         player[i].drawtogglekeydown=0;
 
                     XYZ absflatfacing;
-                    if(i==0){
+                    if(i==0) {
                         absflatfacing=0;
                         absflatfacing.z=-1;
 
                         absflatfacing=DoRotation(absflatfacing,0,-rotation,0);
-                    }
-                    else absflatfacing=flatfacing;
+                    } else
+                                               absflatfacing=flatfacing;
 
                     if(indialogue!=-1){
                         player[i].forwardkeydown=0;
@@ -7559,7 +7368,7 @@ void Game::Tick(){
                         OPENAL_StopSound(OPENAL_ALL);  // hack...OpenAL renderer isn't stopping music after tutorial goes to level menu...
                         OPENAL_SetFrequency(OPENAL_ALL, 0.001);
 
-                        emit_stream_np(stream_music3);
+                        emit_stream_np(stream_menutheme);
 
                         gameon=0;
                         mainmenu=5;
@@ -7649,9 +7458,7 @@ void Game::Tick(){
                Screenshot();
 }
 
-
-
-void   Game::TickOnce(){
+void Game::TickOnce(){
        if(mainmenu)
                rotation+=multiplier*5;
        else
@@ -7668,12 +7475,11 @@ void    Game::TickOnce(){
                }
 }
 
-void   Game::TickOnceAfter(){
+void Game::TickOnceAfter(){
        static XYZ colviewer;
        static XYZ coltarget;
        static XYZ target;
        static XYZ col;
-       static float brotate;
        static XYZ facing;
        static float changedelay;
        static bool alldead;
@@ -7683,15 +7489,15 @@ void    Game::TickOnceAfter(){
        if(!mainmenu){
 
                if(environment==snowyenvironment)
-            music1=stream_music1snow;
+            leveltheme=stream_snowtheme;
                if(environment==grassyenvironment)
-            music1=stream_music1grass;
+            leveltheme=stream_grasstheme;
                if(environment==desertenvironment)
-            music1=stream_music1desert;
+            leveltheme=stream_deserttheme;
 
                realthreat=0;
 
-               musictype=music1;
+               musictype=leveltheme;
                for(int i=0;i<numplayers;i++){
                        if((player[i].aitype==attacktypecutoff||
                         player[i].aitype==getweapontype||
@@ -7701,26 +7507,26 @@ void    Game::TickOnceAfter(){
                     (player[i].targetanimation!=sneakattackedanim&&
                      player[i].targetanimation!=knifesneakattackedanim&&
                      player[i].targetanimation!=swordsneakattackedanim)){
-                               musictype=stream_music2;
+                               musictype=stream_fighttheme;
                                realthreat=1;
                        }
                }
                if(player[0].dead)
-            musictype=stream_music3;
+            musictype=stream_menutheme;
 
 
-               if(musictype==stream_music2)
+               if(musictype==stream_fighttheme)
                        unseendelay=1;
 
-               if(oldmusictype==stream_music2&&musictype!=stream_music2){
+               if(oldmusictype==stream_fighttheme&&musictype!=stream_fighttheme){
                        unseendelay-=multiplier;
                        if(unseendelay>0)
-                               musictype=stream_music2;
+                               musictype=stream_fighttheme;
                }
 
 
                if(loading==2){
-                       musictype=stream_music3;
+                       musictype=stream_menutheme;
                        musicvolume[2]=512;
                        musicvolume[0]=0;
                        musicvolume[1]=0;
@@ -7728,19 +7534,19 @@ void    Game::TickOnceAfter(){
                }
 
                if(musictoggle)
-                       if(musictype!=oldmusictype&&musictype==stream_music2)
+                       if(musictype!=oldmusictype&&musictype==stream_fighttheme)
                                emit_sound_np(alarmsound);
                musicselected=musictype;
 
-               if(musicselected==music1)
+               if(musicselected==leveltheme)
             musicvolume[0]+=multiplier*450;
                else
             musicvolume[0]-=multiplier*450;
-               if(musicselected==stream_music2)
+               if(musicselected==stream_fighttheme)
             musicvolume[1]+=multiplier*450;
                else
             musicvolume[1]-=multiplier*450;
-               if(musicselected==stream_music3)
+               if(musicselected==stream_menutheme)
             musicvolume[2]+=multiplier*450;
                else
             musicvolume[2]-=multiplier*450;
@@ -7757,31 +7563,31 @@ void    Game::TickOnceAfter(){
 
                if(musictoggle){
                        if(musicvolume[0]>0&&oldmusicvolume[0]<=0)
-                         emit_stream_np(music1, musicvolume[0]);
+                         emit_stream_np(leveltheme, musicvolume[0]);
                        if(musicvolume[1]>0&&oldmusicvolume[1]<=0)
-                         emit_stream_np(stream_music2, musicvolume[1]);
+                         emit_stream_np(stream_fighttheme, musicvolume[1]);
                        if(musicvolume[2]>0&&oldmusicvolume[2]<=0)
-                         emit_stream_np(stream_music3, musicvolume[2]);
+                         emit_stream_np(stream_menutheme, musicvolume[2]);
                        if(musicvolume[0]<=0&&oldmusicvolume[0]>0)
-                               pause_sound(music1);
+                               pause_sound(leveltheme);
                        if(musicvolume[1]<=0&&oldmusicvolume[1]>0)
-                               pause_sound(stream_music2);
+                               pause_sound(stream_fighttheme);
                        if(musicvolume[2]<=0&&oldmusicvolume[2]>0)
-                               pause_sound(stream_music3);
+                               pause_sound(stream_menutheme);
 
                        if(musicvolume[0]!=oldmusicvolume[0])
-                               OPENAL_SetVolume(channels[music1], musicvolume[0]);
+                               OPENAL_SetVolume(channels[leveltheme], musicvolume[0]);
                        if(musicvolume[1]!=oldmusicvolume[1])
-                               OPENAL_SetVolume(channels[stream_music2], musicvolume[1]);
+                               OPENAL_SetVolume(channels[stream_fighttheme], musicvolume[1]);
                        if(musicvolume[2]!=oldmusicvolume[2])
-                               OPENAL_SetVolume(channels[stream_music3], musicvolume[2]);
+                               OPENAL_SetVolume(channels[stream_menutheme], musicvolume[2]);
 
                        for(int i=0;i<3;i++)
                                oldmusicvolume[i]=musicvolume[i];
                } else {
-                       pause_sound(music1);
-                       pause_sound(stream_music2);
-                       pause_sound(stream_music3);
+                       pause_sound(leveltheme);
+                       pause_sound(stream_fighttheme);
+                       pause_sound(stream_menutheme);
 
                        for(int i=0;i<4;i++){
                                oldmusicvolume[i]=0;
@@ -7844,12 +7650,12 @@ void    Game::TickOnceAfter(){
                                if(targetlevel>numchallengelevels-1)targetlevel=0;
                        }
 
-                       if(changedelay>0&&!player[0].dead&&!won){
+                       if(changedelay>0&&!player[0].dead&&!won) {
                                //high scores, awards, win
-                               if(campaign){
+                               if(campaign) {
                                        accountactive->winCampaignLevel(whichchoice, bonustotal, leveltime);
                                        scoreadded=1;
-                               }else{
+                               } else {
                                        accountactive->winLevel(whichlevel,bonustotal-startbonustotal,leveltime);
                                }
                                won=1;
@@ -7866,7 +7672,7 @@ void      Game::TickOnceAfter(){
                                killhotspot=0;
                        }
 
-                       if(!editorenabled&&gameon&&!mainmenu){
+                       if(!editorenabled&&gameon&&!mainmenu) {
                                if(changedelay!=-999)
                     changedelay-=multiplier/7;
                                if(player[0].dead)
@@ -7893,7 +7699,7 @@ void      Game::TickOnceAfter(){
 
                                        fireSound(firestartsound);
 
-                                       Loadlevel(campaignmapname[levelorder[accountactive->getCampaignChoicesMade()]]);
+                                       Loadlevel(campaignlevels[accountactive->getCampaignChoicesMade()].mapname.c_str());
 
                                        fireSound();
 
@@ -7905,17 +7711,16 @@ void    Game::TickOnceAfter(){
                         (player[0].dead||
                          (alldead&&maptype==mapkilleveryone)||
                          (winhotspot)||
-                         (killhotspot))&&
-                        !winfreeze)
+                         (killhotspot)))
                     loading=1;
                                if((player[0].dead||
                             (alldead&&maptype==mapkilleveryone)||
                             (winhotspot)||
                             (windialogue)||
                             (killhotspot))&&
-                        changedelay<=0){
-                    if(whichlevel!=-2&&!loading&&!player[0].dead){
-                        winfreeze=1;
+                        changedelay<=0) {
+                    if(whichlevel!=-2&&!loading&&!player[0].dead) {
+                        winfreeze=true;
                         changedelay=-999;
                     }
                     if(player[0].dead)
@@ -7923,19 +7728,18 @@ void    Game::TickOnceAfter(){
                                }
                        }
 
-                       if(campaign)
-                               if(mainmenu==0&&winfreeze&&(campaignchoosenext[campaignchoicewhich[whichchoice]])==1)
-                                       if(campaignnumnext[campaignchoicewhich[whichchoice]]==0)
+                       if(campaign) {
+                // campaignchoosenext determines what to do when the level is complete:
+                // 0 = load next level
+                // 1 = go back to level select screen
+                // 2 = stealthload next level
+                               if(mainmenu==0&&winfreeze&&(campaignlevels[actuallevel].choosenext)==1){
+                                       if(campaignlevels[actuallevel].nextlevel.empty())
                                                endgame=1;
-                               else if(mainmenu==0&&winfreeze){
-                                       if(campaignchoosenext[campaignchoicewhich[whichchoice]]==2)
-                                               stealthloading=1;
-                                       else
-                        stealthloading=0;
+                               } else if(mainmenu==0&&winfreeze) {
+                                       stealthloading = (campaignlevels[actuallevel].choosenext==2);
 
                                        if(!stealthloading){
-                                               float gLoc[3]={0,0,0};
-                                               float vel[3]={0,0,0};
                                                fireSound(firestartsound);
 
                                                flash();
@@ -7943,83 +7747,26 @@ void    Game::TickOnceAfter(){
 
                                        startbonustotal=0;
 
-                                       ifstream ipstream(ConvertFileName(":Data:Campaigns:main.txt"));
-                                       ipstream.ignore(256,':');
-                                       ipstream >> campaignnumlevels;
-                                       for(int i=0;i<campaignnumlevels;i++){
-                                               ipstream.ignore(256,':');
-                                               ipstream.ignore(256,':');
-                                               ipstream.ignore(256,' ');
-                                               ipstream >> campaignmapname[i];
-                                               ipstream.ignore(256,':');
-                                               ipstream >> campaigndescription[i];
-                                               for(int j=0;j<256;j++){
-                                                       if(campaigndescription[i][j]=='_')campaigndescription[i][j]=' ';
-                                               }
-                                               ipstream.ignore(256,':');
-                                               ipstream >> campaignchoosenext[i];
-                                               ipstream.ignore(256,':');
-                                               ipstream >> campaignnumnext[i];
-                                               for(int j=0;j<campaignnumnext[i];j++){
-                                                       ipstream.ignore(256,':');
-                                                       ipstream >> campaignnextlevel[i][j];
-                                                       campaignnextlevel[i][j]-=1;
-                                               }
-                                               ipstream.ignore(256,':');
-                                               ipstream >> campaignlocationx[i];
-                                               ipstream.ignore(256,':');
-                                               ipstream >> campaignlocationy[i];
-                                       }
-                                       ipstream.close();
-
-                                       for(int i=0;i<campaignnumlevels;i++){
-                                               levelvisible[i]=0;
-                                               levelhighlight[i]=0;
-                                       }
-
-
-                                       for(int i=0;i<campaignnumlevels;i++){
-                                               levelvisible[i]=0;
-                                               levelhighlight[i]=0;
-                                       }
-
-                                       levelorder[0]=0;
-                                       levelvisible[0]=1;
-                                       for(int i=0;i<accountactive->getCampaignChoicesMade();i++){
-                                               levelorder[i+1]=campaignnextlevel[levelorder[i]][accountactive->getCampaignChoice(i)];
-                                               levelvisible[levelorder[i+1]]=1;
-                                       }
-                                       int whichlevelstart;
-                                       whichlevelstart=accountactive->getCampaignChoicesMade()-1;
-                                       if(whichlevelstart<0){
-                                               campaignchoicenum=1;
-                                               campaignchoicewhich[0]=0;
-                                       }
-                                       else
-                                       {
-                                               campaignchoicenum=campaignnumnext[levelorder[whichlevelstart]];
-                                               for(int i=0;i<campaignchoicenum;i++){
-                                                       campaignchoicewhich[i]=campaignnextlevel[levelorder[whichlevelstart]][i];
-                                                       levelvisible[campaignnextlevel[levelorder[whichlevelstart]][i]]=1;
-                                                       levelhighlight[campaignnextlevel[levelorder[whichlevelstart]][i]]=1;
-                                               }
-                                       }
+                                       LoadCampaign();
 
                                        loading=2;
                                        loadtime=0;
                                        targetlevel=7;
-                                       if(!firstload)LoadStuff();
+                                       if(!firstload)
+                                               LoadStuff();
                                        whichchoice=0;
+                                       actuallevel=campaignlevels[actuallevel].nextlevel.front();
                                        visibleloading=1;
                                        stillloading=1;
-                                       Loadlevel(campaignmapname[campaignchoicewhich[0]]);
+                                       Loadlevel(campaignlevels[actuallevel].mapname.c_str());
                                        campaign=1;
                                        mainmenu=0;
                                        gameon=1;
-                                       pause_sound(stream_music3);
+                                       pause_sound(stream_menutheme);
 
                                        stealthloading=0;
                                }
+            }
 
             if(loading==3)
                 loading=0;
@@ -8036,7 +7783,6 @@ void      Game::TickOnceAfter(){
        facing=DoRotation(facing,0,0-rotation,0);
        viewerfacing=facing;
 
-       brotate=0;
        if(!cameramode){
                if((animation[player[0].targetanimation].attack!=3&&animation[player[0].currentanimation].attack!=3)||player[0].skeleton.free)target=player[0].coords+player[0].currentoffset*(1-player[0].target)*player[0].scale+player[0].targetoffset*player[0].target*player[0].scale-player[0].facing*.05;
                else target=player[0].oldcoords+player[0].currentoffset*(1-player[0].target)*player[0].scale+player[0].targetoffset*player[0].target*player[0].scale-player[0].facing*.05;