]> git.rm.cloudns.org Git - xonotic/xonotic-data.pk3dir.git/commitdiff
Massively optimize bulldozer by using controllers with number arrays (reduces the...
authorMario <mario@smbclan.net>
Tue, 24 Jan 2017 14:32:57 +0000 (00:32 +1000)
committerMario <mario@smbclan.net>
Tue, 24 Jan 2017 14:32:57 +0000 (00:32 +1000)
qcsrc/common/minigames/minigame/bd.qc

index 09fae55d80ef2f08ab17d8097633646b1a641069..d4e1eebe808dadac63517112520f1675460bbef1 100644 (file)
@@ -1,13 +1,18 @@
 #include "bd.qh"
 REGISTER_MINIGAME(bd, "Bulldozer");
 
+REGISTER_NET_LINKED(ENT_CLIENT_BD_CONTROLLER)
+
 const int BD_TURN_MOVE  = 0x0100; // player must move the bulldozer
 const int BD_TURN_WIN   = 0x0200; // victory
 const int BD_TURN_LOSS  = 0x0400; // they did it?!
 const int BD_TURN_EDIT  = 0x0800; // editing mode
 const int BD_TURN_TYPE  = 0x0f00; // turn type mask
 
+// send flags
 const int BD_SF_PLAYERMOVES = MINIG_SF_CUSTOM;
+const int BD_SF_UPDATE_SINGLE = MINIG_SF_CUSTOM<<1;
+const int BD_SF_UPDATE_ALL = MINIG_SF_CUSTOM<<2;
 
 // 240 tiles...
 const int BD_LET_CNT = 20;
@@ -19,15 +24,22 @@ const int BD_TEAMS = 1;
 
 .int bd_dir;
 
+.int bd_dirs[BD_NUM_CNT];
+
 .int bd_moves;
 
+.int bd_tilelet;
+
 .string bd_levelname;
 .string bd_nextlevel;
 
 #ifdef SVQC
 .bool bd_canedit;
+.int bd_forceupdate;
 #endif
 
+.int bd_tiletypes[BD_NUM_CNT];
+
 .int bd_tiletype;
 const int BD_TILE_DOZER = 1;
 const int BD_TILE_TARGET = 2;
@@ -61,6 +73,15 @@ entity bd_find_piece(entity minig, string tile, bool check_target)
        return NULL;
 }
 
+entity bd_find_controller(entity minig, int letter)
+{
+       entity e = NULL;
+       while ( ( e = findentity(e,owner,minig) ) )
+               if ( e.classname == "bd_controller" && e.bd_tilelet == letter )
+                       return e;
+       return NULL;
+}
+
 // check if the tile name is valid (15x15 grid)
 bool bd_valid_tile(string tile)
 {
@@ -71,6 +92,14 @@ bool bd_valid_tile(string tile)
        return 0 <= number && number < BD_NUM_CNT && 0 <= letter && letter < BD_LET_CNT;
 }
 
+void bd_controller_update(entity controller, int number)
+{
+#ifdef SVQC
+       controller.bd_forceupdate = number;
+#endif
+       minigame_server_sendflags(controller,BD_SF_UPDATE_SINGLE);
+}
+
 entity bd_find_dozer(entity minig)
 {
        entity e = NULL;
@@ -80,6 +109,71 @@ entity bd_find_dozer(entity minig)
        return NULL;
 }
 
+#ifdef SVQC
+bool bd_controller_send(entity this, entity to, int sf)
+{
+       WriteHeader(MSG_ENTITY, ENT_CLIENT_BD_CONTROLLER);
+       if(sf & BD_SF_UPDATE_ALL)
+               sf &= ~BD_SF_UPDATE_SINGLE;
+
+       WriteByte(MSG_ENTITY, sf);
+       WriteByte(MSG_ENTITY, this.bd_tilelet);
+       WriteString(MSG_ENTITY,this.owner.netname);
+
+       if(sf & BD_SF_UPDATE_SINGLE)
+       {
+               int number = this.bd_forceupdate;
+               //this.bd_forceupdate = 0;
+               int ttype = this.bd_tiletypes[number];
+               int dir = this.bd_dirs[number];
+               WriteByte(MSG_ENTITY, number);
+               WriteByte(MSG_ENTITY, ttype);
+               WriteByte(MSG_ENTITY, dir);
+       }
+
+       if(sf & BD_SF_UPDATE_ALL)
+       {
+               for(int j = 0; j < BD_NUM_CNT; ++j)
+               {
+                       int ttype = this.bd_tiletypes[j];
+                       int dir = this.bd_dirs[j];
+                       WriteByte(MSG_ENTITY, ttype);
+                       WriteByte(MSG_ENTITY, dir);
+               }
+       }
+
+       return true;
+}
+#elif defined(CSQC)
+void minigame_read_owner(entity this);
+
+NET_HANDLE(ENT_CLIENT_BD_CONTROLLER, bool isNew)
+{
+       this.classname = "bd_controller";
+       return = true;
+
+       int sf = ReadByte();
+       this.bd_tilelet = ReadByte();
+       minigame_read_owner(this);
+
+       if(sf & BD_SF_UPDATE_SINGLE)
+       {
+               int number = ReadByte();
+               this.bd_tiletypes[number] = ReadByte();
+               this.bd_dirs[number] = ReadByte();
+       }
+
+       if(sf & BD_SF_UPDATE_ALL)
+       {
+               for(int j = 0; j < BD_NUM_CNT; ++j)
+               {
+                       this.bd_tiletypes[j] = ReadByte();
+                       this.bd_dirs[j] = ReadByte();
+               }
+       }
+}
+#endif
+
 void bd_check_winner(entity minig)
 {
        int total = 0, valid = 0;
@@ -168,11 +262,11 @@ bool bd_move_dozer(entity minigame, entity dozer)
        myy += dir.y;
 
        string newpos = minigame_tile_buildname(myx, myy);
-       entity hit = bd_find_piece(minigame, newpos, false);
-
        if(!bd_valid_tile(newpos))
                return false;
 
+       entity hit = bd_find_piece(minigame, newpos, false);
+
        if(hit)
        switch(hit.bd_tiletype)
        {
@@ -207,6 +301,20 @@ bool bd_move_dozer(entity minigame, entity dozer)
                }
        }
 
+       entity controller = bd_find_controller(minigame, minigame_tile_letter(newpos));
+       int number = minigame_tile_number(newpos);
+       switch(controller.bd_tiletypes[number])
+       {
+               case BD_TILE_BRICK8:
+               case BD_TILE_BRICK7:
+               case BD_TILE_BRICK6:
+               case BD_TILE_BRICK5:
+               case BD_TILE_BRICK4:
+               case BD_TILE_BRICK3:
+               case BD_TILE_BRICK2:
+               case BD_TILE_BRICK1: return false;
+       }
+
        if(dozer.netname) { strunzone(dozer.netname); }
        dozer.netname = strzone(newpos);
 
@@ -281,6 +389,17 @@ void bd_editor_place(entity minigame, entity player, string pos, int thetile, st
                        //if(dozer && thetile == BD_TILE_DOZER && pos != dozer.netname)
                                //return; // nice try
 
+                       int tlet = minigame_tile_letter(pos);
+                       int tnum = minigame_tile_number(pos);
+                       entity controller = bd_find_controller(minigame, tlet);
+                       if(controller.bd_tiletypes[tnum])
+                       {
+                               controller.bd_tiletypes[tnum] = 0;
+                               controller.bd_dirs[tnum] = 0;
+                               bd_controller_update(controller, tnum);
+                               return;
+                       }
+
                        if(found_piece || (targ && thetile != BD_TILE_BOULDER))
                        {
                                entity piece = bd_find_piece(minigame, pos, false);
@@ -294,12 +413,24 @@ void bd_editor_place(entity minigame, entity player, string pos, int thetile, st
                                return;
                        }
 
-                       entity piece = msle_spawn(minigame,"minigame_board_piece");
-                       piece.team = 1;
-                       piece.netname = strzone(pos);
-                       piece.bd_tiletype = thetile;
-                       piece.bd_dir = 0;
-                       minigame_server_sendflags(piece,MINIG_SF_UPDATE);
+                       if(bd_canfill(thetile))
+                       {
+                               int number = minigame_tile_number(pos);
+                               int letter = minigame_tile_letter(pos);
+                               entity controller = bd_find_controller(minigame, letter);
+                               controller.bd_tiletypes[number] = thetile;
+                               controller.bd_dirs[number] = 0;
+                               bd_controller_update(controller, number);
+                       }
+                       else
+                       {
+                               entity piece = msle_spawn(minigame,"minigame_board_piece");
+                               piece.team = 1;
+                               piece.netname = strzone(pos);
+                               piece.bd_tiletype = thetile;
+                               piece.bd_dir = 0;
+                               minigame_server_sendflags(piece,MINIG_SF_UPDATE);
+                       }
 
                        minigame_server_sendflags(minigame,MINIG_SF_UPDATE);
                }
@@ -425,6 +556,22 @@ void bd_setup_pieces(entity minigame)
                        if(e.netname) { strunzone(e.netname); }
                        delete(e);
                }
+       e = NULL;
+       while( (e = findentity(e, owner, minigame)) )
+               if(e.classname == "bd_controller")
+               {
+                       delete(e);
+               }
+
+       for(int letter = 0; letter < BD_LET_CNT; ++letter)
+       {
+               entity controller = new_pure(bd_controller);
+               controller.owner = minigame;
+               controller.bd_tilelet = letter;
+       #ifdef SVQC
+               Net_LinkEntity(controller, false, 0, bd_controller_send);
+       #endif
+       }
 
        bd_load_level(minigame);
 }
@@ -488,6 +635,19 @@ void bd_activate_editor(entity minigame, entity player)
        bd_setup_pieces(minigame);
 }
 
+string bd_save_controller_piece(entity minigame, entity e, int number)
+{
+       string bd_string = "";
+
+       string tilename = minigame_tile_buildname(e.bd_tilelet, number);
+
+       bd_string = strcat(bd_string, "\"", tilename, "\" ");
+       bd_string = strcat(bd_string, ftos(e.bd_tiletypes[number]), " ");
+       bd_string = strcat(bd_string, ftos(e.bd_dirs[number]));
+
+       return bd_string;
+}
+
 string bd_save_piece(entity minigame, entity e)
 {
        string bd_string = "";
@@ -517,25 +677,37 @@ int bd_fix_dir(vector dir)
        return BD_DIR_DN; // down if all else fails
 }
 
-entity bd_load_piece(entity minigame, string s)
+void bd_load_piece(entity minigame, string s)
 {
        // separate pieces between the ; symbols
        string bd_string = s;
 
        tokenize_console(bd_string);
 
-       entity e = msle_spawn(minigame,"minigame_board_piece");
-       e.team = 1;
-       e.bd_dir = 0;
-
        int argv_num = 0;
-       e.netname = strzone(argv(argv_num)); ++argv_num;
-       e.bd_tiletype = stof(argv(argv_num)); ++argv_num;
-       e.bd_dir = stoi(argv(argv_num)); ++argv_num;
+       string tilename = strzone(argv(argv_num)); ++argv_num;
+       int tiletype = stoi(argv(argv_num)); ++argv_num;
+       int dir = stoi(argv(argv_num)); ++argv_num;
 
-       minigame_server_sendflags(e,MINIG_SF_ALL);
+       if(bd_canfill(tiletype))
+       {
+               int letter = minigame_tile_letter(tilename);
+               int number = minigame_tile_number(tilename);
+               entity controller = bd_find_controller(minigame, letter);
+               controller.bd_tiletypes[number] = tiletype;
+               controller.bd_dirs[number] = dir;
 
-       return e;
+               bd_controller_update(controller, number);
+       }
+       else
+       {
+               entity e = msle_spawn(minigame,"minigame_board_piece");
+               e.netname = tilename;
+               e.team = 1;
+               e.bd_dir = dir;
+               e.bd_tiletype = tiletype;
+               minigame_server_sendflags(e,MINIG_SF_ALL);
+       }
 }
 
 bool bd_save_level(entity minigame)
@@ -569,6 +741,17 @@ bool bd_save_level(entity minigame)
                        fputs(file_get, strcat("nextlevel = \"", minigame.bd_nextlevel, "\"\n"));
 
                entity e = NULL;
+               while ( ( e = findentity(e,owner,minigame) ) )
+               if ( e.classname == "bd_controller" )
+               {
+                       for(int j = 0; j < BD_NUM_CNT; ++j)
+                       {
+                               // use a line of text for each object, listing all properties
+                               fputs(file_get, strcat(bd_save_controller_piece(minigame, e, j), "\n"));
+                       }
+               }
+               e = NULL;
+
                while ( ( e = findentity(e,owner,minigame) ) )
                if ( e.classname == "minigame_board_piece" )
                {
@@ -612,8 +795,7 @@ void bd_load_level(entity minigame)
                                continue;
                        }
 
-                       entity e;
-                       e = bd_load_piece(minigame, file_read);
+                       bd_load_piece(minigame, file_read);
                }
        }
        fclose(file_get);
@@ -673,6 +855,12 @@ int bd_server_event(entity minigame, string event, ...)
                                if(e.netname) { strunzone(e.netname); }
                                delete(e);
                        }
+                       e = NULL;
+                       while( (e = findentity(e, owner, minigame)) )
+                       if(e.classname == "bd_controller")
+                       {
+                               delete(e);
+                       }
 
                        if(minigame.bd_nextlevel) { strunzone(minigame.bd_nextlevel); }
                        if(minigame.bd_levelname) { strunzone(minigame.bd_levelname); }
@@ -800,7 +988,26 @@ void bd_hud_board(vector pos, vector mySize)
        }
        FOREACH_MINIGAME_ENTITY(e)
        {
-               if ( e.classname == "minigame_board_piece" )
+               if ( e.classname == "bd_controller" )
+               {
+                       for(int j = 0; j < BD_NUM_CNT; ++j)
+                       {
+                               if(!e.bd_tiletypes[j]) continue;
+
+                               int letter = e.bd_tilelet;
+                               string mypos = minigame_tile_buildname(letter, j);
+
+                               tile_pos = minigame_tile_pos(mypos,BD_NUM_CNT,BD_LET_CNT);
+                               tile_pos = minigame_hud_denormalize(tile_pos,pos,mySize);
+
+                               string thepiece = bd_get_tile_pic(e.bd_tiletypes[j]);
+
+                               minigame_drawpic_centered( tile_pos,
+                                               minigame_texture(thepiece),
+                                               tile_size, '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL );
+                       }       
+               }
+               else if ( e.classname == "minigame_board_piece" )
                {
                        if(e.bd_tiletype != BD_TILE_DOZER && !e.bd_hide) // hide boulders
                        {