]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blobdiff - qcsrc/client/mapvoting.qc
Merge branch 'master' into Mario/buff_updates
[xonotic/xonotic-data.pk3dir.git] / qcsrc / client / mapvoting.qc
index 5bffcdee144564aef7628ad541627a2eccb8b638..f00ee544c059f76cd3ec85bdf5375914df4caa9e 100644 (file)
@@ -1,13 +1,26 @@
+#include "mapvoting.qh"
+#include "_all.qh"
+
+#include "hud.qh"
+#include "scoreboard.qh"
+
+#include "../common/mapinfo.qh"
+#include "../common/util.qh"
+
+#include "../dpdefs/keycodes.qh"
+
+
 int mv_num_maps;
 
 float mv_active;
 string mv_maps[MAPVOTE_COUNT];
 string mv_pics[MAPVOTE_COUNT];
-string mv_pk3[MAPVOTE_COUNT];
+string mv_pk3[MAPVOTE_COUNT]; // map pk3 name or gametype human readable name
+string mv_desc[MAPVOTE_COUNT];
 float mv_preview[MAPVOTE_COUNT];
 float mv_votes[MAPVOTE_COUNT];
-float mv_avail[MAPVOTE_COUNT];
-float mv_avail_start[MAPVOTE_COUNT];
+float mv_flags[MAPVOTE_COUNT];
+float mv_flags_start[MAPVOTE_COUNT];
 entity mv_pk3list;
 float mv_abstain;
 float mv_ownvote;
@@ -27,16 +40,20 @@ string mapvote_chosenmap;
 vector gtv_text_size;
 vector gtv_text_size_small;
 
-string MapVote_FormatMapItem(int id, string map, float count, float maxwidth, vector fontsize)
+const int NUM_SSDIRS = 4;
+string ssdirs[NUM_SSDIRS];
+int n_ssdirs;
+
+string MapVote_FormatMapItem(int id, string map, float _count, float maxwidth, vector fontsize)
 {
        string pre, post;
        pre = sprintf("%d. ", id+1);
        if(mv_detail)
        {
-               if(count == 1)
+               if(_count == 1)
                        post = _(" (1 vote)");
-               else if(count >= 0 && mv_avail[id] == GTV_AVAILABLE)
-                       post = sprintf(_(" (%d votes)"), count);
+               else if(_count >= 0 && (mv_flags[id] & GTV_AVAILABLE))
+                       post = sprintf(_(" (%d votes)"), _count);
                else
                        post = "";
        }
@@ -47,14 +64,9 @@ string MapVote_FormatMapItem(int id, string map, float count, float maxwidth, ve
        return strcat(pre, map, post);
 }
 
-string GameTypeVote_DescriptionByID(float id)
-{
-       return MapInfo_Type_Description(MapInfo_Type_FromString(mv_maps[id]));
-}
-
 vector MapVote_RGB(int id)
 {
-       if(mv_avail[id] != GTV_AVAILABLE)
+       if(!(mv_flags[id] & GTV_AVAILABLE))
                return '1 1 1';
        if(id == mv_ownvote)
                return '0 1 0';
@@ -64,58 +76,63 @@ vector MapVote_RGB(int id)
                return '1 1 1';
 }
 
-void GameTypeVote_DrawGameTypeItem(vector pos, float maxh, float tsize, string gtype, string pic, float count, int id)
+void GameTypeVote_DrawGameTypeItem(vector pos, float maxh, float tsize, string gtype, string pic, float _count, int id)
 {
+       // Find the correct alpha
        float alpha;
-       float desc_padding = gtv_text_size.x * 3;
+       if(!(mv_flags_start[id] & GTV_AVAILABLE))
+               alpha = 0.2; // The gametype isn't supported by the map
+       else if ( !(mv_flags[id] & GTV_AVAILABLE) && mv_top2_alpha)
+               alpha = mv_top2_alpha; // Fade away if not one of the top 2 choice
+       else
+               alpha = 1; // Normal, full alpha
+
+       // Bounding box details
        float rect_margin = hud_fontsize.y / 2;
        vector rect_pos = pos - '0.5 0.5 0' * rect_margin;
        vector rect_size = '1 1 0';
-       rect_size_x = tsize + rect_margin;
-       rect_size_y = maxh + rect_margin;
-       vector rgb = MapVote_RGB(id);
-       vector offset = pos;
-       float nlines = 0;
-       
-       if(mv_avail_start[id] != GTV_AVAILABLE)
-               alpha = 0.2;
-       else if ( mv_avail[id] != GTV_AVAILABLE && mv_top2_alpha)
-               alpha = mv_top2_alpha;
-       else
-               alpha = 1;
-       
-       if(id == mv_selection && mv_avail[id] == GTV_AVAILABLE)
+       rect_size.x = tsize + rect_margin;
+       rect_size.y = maxh + rect_margin;
+
+       // Highlight selected item
+       if(id == mv_selection && (mv_flags[id] & GTV_AVAILABLE))
        {
                drawfill(rect_pos, rect_size, '1 1 1', 0.1, DRAWFLAG_NORMAL);
        }
+
+       // Highlight current vote
+       vector rgb = MapVote_RGB(id);
        if(id == mv_ownvote)
        {
                drawfill(rect_pos, rect_size, rgb, 0.1*alpha, DRAWFLAG_NORMAL);
                drawborderlines(autocvar_scoreboard_border_thickness, rect_pos, rect_size, rgb, alpha, DRAWFLAG_NORMAL);
        }
-       
+
+       vector offset = pos;
+
+       float title_gap = gtv_text_size.y * 1.4; // distance between the title and the description
+       pos.y += title_gap;
+       maxh -= title_gap;
+
+       // Evaluate the image size
+       vector image_size = '1 1 0' * gtv_text_size.x * 3;
+       if ( maxh < image_size.y )
+               image_size = '1 1 0' * maxh;
+       image_size *= 0.8;
+       float desc_padding = gtv_text_size.x * 0.6;
+       pos.x += image_size.x + desc_padding;
+       tsize -= image_size.x + desc_padding;
+
+       // Split the description into lines
        entity title;
        title = spawn();
-       title.message = MapVote_FormatMapItem(id, MapInfo_Type_ToText(MapInfo_Type_FromString(gtype)), 
-                                                                                 count, tsize, gtv_text_size);
-       title.origin = pos-offset;
-       
-       pos.y += gtv_text_size_small.y;
-       pos.y += gtv_text_size.y/2;
-       
-       maxh -= gtv_text_size.y;
-       
-       entity picent = spawn();
-       picent.origin = pos-offset;
-       picent.maxs = '1 1 0 ' * min(maxh, desc_padding) * 0.8;
-       
-       pos.x += desc_padding;
-       tsize -= desc_padding;
-       
-       string thelabel = GameTypeVote_DescriptionByID(id), ts;
+       title.message = MapVote_FormatMapItem(id, mv_pk3[id], _count, tsize, gtv_text_size);
+
+       string thelabel = mv_desc[id], ts;
        entity last = title;
        entity next = world;
-       if( thelabel != "") 
+       float nlines = 0;
+       if( thelabel != "")
        {
                float i,n = tokenizebyseparator(thelabel, "\n");
                for(i = 0; i < n && maxh > (nlines+1)*gtv_text_size_small.y; ++i)
@@ -137,15 +154,20 @@ void GameTypeVote_DrawGameTypeItem(vector pos, float maxh, float tsize, string g
                        }
                }
        }
-       
-       maxh -= max(nlines*gtv_text_size_small.y,picent.maxs.y);
+
+       // Center the contents in the bounding box
+       maxh -= max(nlines*gtv_text_size_small.y,image_size.y);
        if ( maxh > 0 )
                offset.y += maxh/2;
-       drawstring(title.origin+offset, title.message, gtv_text_size, rgb, alpha, DRAWFLAG_NORMAL); 
-       
+
+       // Draw the title
+       drawstring(offset, title.message, gtv_text_size, rgb, alpha, DRAWFLAG_NORMAL);
+
+       // Draw the icon
        if(pic != "")
-               drawpic(picent.origin+offset, pic, picent.maxs, '1 1 1', alpha, DRAWFLAG_NORMAL);
-       
+               drawpic('0 1 0'*title_gap+'0.5 0 0'*desc_padding+offset, pic, image_size, '1 1 1', alpha, DRAWFLAG_NORMAL);
+
+       // Draw the description
        for ( last = title.chain; last ; )
        {
                drawstring(last.origin+offset, last.message, gtv_text_size_small, '1 1 1', alpha, DRAWFLAG_NORMAL);
@@ -153,12 +175,12 @@ void GameTypeVote_DrawGameTypeItem(vector pos, float maxh, float tsize, string g
                last = last.chain;
                remove(next);
        }
-       
-       remove(picent);
+
+       // Cleanup
        remove(title);
 }
 
-void MapVote_DrawMapItem(vector pos, float isize, float tsize, string map, string pic, float count, int id)
+void MapVote_DrawMapItem(vector pos, float isize, float tsize, string map, string pic, float _count, int id)
 {
        vector img_size = '0 0 0';
        vector rgb;
@@ -169,17 +191,17 @@ void MapVote_DrawMapItem(vector pos, float isize, float tsize, string map, strin
 
        rgb = MapVote_RGB(id);
 
-       img_size_y = isize;
-       img_size_x = isize / 0.75; // 4:3 x can be stretched easily, height is defined in isize
+       img_size.y = isize;
+       img_size.x = isize / 0.75; // 4:3 x can be stretched easily, height is defined in isize
 
-       pos_y = pos.y + img_size.y;
+       pos.y = pos.y + img_size.y;
 
-       label = MapVote_FormatMapItem(id, map, count, tsize, hud_fontsize);
+       label = MapVote_FormatMapItem(id, map, _count, tsize, hud_fontsize);
 
        text_size = stringwidth(label, false, hud_fontsize);
 
        float theAlpha;
-       if (mv_avail[id] != GTV_AVAILABLE && mv_top2_alpha)
+       if (!(mv_flags[id] & GTV_AVAILABLE) && mv_top2_alpha)
                theAlpha = mv_top2_alpha;
        else
                theAlpha = 1;
@@ -187,8 +209,8 @@ void MapVote_DrawMapItem(vector pos, float isize, float tsize, string map, strin
        pos.x -= text_size*0.5;
        drawstring(pos, label, hud_fontsize, rgb, theAlpha, DRAWFLAG_NORMAL);
 
-       pos_x = pos.x + text_size*0.5 - img_size.x*0.5;
-       pos_y = pos.y - img_size.y;
+       pos.x = pos.x + text_size*0.5 - img_size.x*0.5;
+       pos.y = pos.y - img_size.y;
 
        pos += autocvar_scoreboard_border_thickness * '1 1 0';
        img_size -= (autocvar_scoreboard_border_thickness * 2) * '1 1 0';
@@ -209,11 +231,11 @@ void MapVote_DrawMapItem(vector pos, float isize, float tsize, string map, strin
        else
                drawborderlines(autocvar_scoreboard_border_thickness, pos, img_size, '0 0 0', theAlpha, DRAWFLAG_NORMAL);
 
-       if(id == mv_selection && mv_avail[id] == GTV_AVAILABLE)
+       if(id == mv_selection && (mv_flags[id] & GTV_AVAILABLE))
                drawfill(pos, img_size, '1 1 1', 0.1, DRAWFLAG_NORMAL);
 }
 
-void MapVote_DrawAbstain(vector pos, float isize, float tsize, float count, int id)
+void MapVote_DrawAbstain(vector pos, float isize, float tsize, float _count, int id)
 {
        vector rgb;
        float text_size;
@@ -221,9 +243,9 @@ void MapVote_DrawAbstain(vector pos, float isize, float tsize, float count, int
 
        rgb = MapVote_RGB(id);
 
-       pos_y = pos.y + hud_fontsize.y;
+       pos.y = pos.y + hud_fontsize.y;
 
-       label = MapVote_FormatMapItem(id, _("Don't care"), count, tsize, hud_fontsize);
+       label = MapVote_FormatMapItem(id, _("Don't care"), _count, tsize, hud_fontsize);
 
        text_size = stringwidth(label, false, hud_fontsize);
 
@@ -268,7 +290,7 @@ float MapVote_Selection(vector topleft, vector cellsize, float rows, float colum
 
        if ( mv_selection_keyboard )
                return mv_selection;
-       
+
        return mv_mouse_selection;
 }
 
@@ -290,9 +312,9 @@ void MapVote_Draw()
        if (!autocvar_hud_cursormode)
        {
                vector mpos = mv_mousepos + getmousepos();
-               mpos_x = bound(0, mpos.x, vid_conwidth);
-               mpos_y = bound(0, mpos.y, vid_conheight);
-               
+               mpos.x = bound(0, mpos.x, vid_conwidth);
+               mpos.y = bound(0, mpos.y, vid_conheight);
+
                if ( mpos.x != mv_mousepos.x || mpos.y != mv_mousepos.y )
                        mv_selection_keyboard = 0;
                mv_mousepos = mpos;
@@ -311,28 +333,28 @@ void MapVote_Draw()
 
        hud_fontsize = HUD_GetFontsize("hud_fontsize");
 
-       pos_y = ymin;
-       pos_z = 0;
+       pos.y = ymin;
+       pos.z = 0;
 
        draw_beginBoldFont();
        map = ((gametypevote) ? _("Decide the gametype") : _("Vote for a map"));
-       pos_x = center - stringwidth(map, false, '12 0 0');
+       pos.x = center - stringwidth(map, false, '12 0 0');
        drawstring(pos, map, '24 24 0', '1 1 1', 1, DRAWFLAG_NORMAL);
        pos.y += 26;
 
        if( mapvote_chosenmap != "" )
        {
-               pos_x = center - stringwidth(mapvote_chosenmap, false, hud_fontsize*1.5/2);
+               pos.x = center - stringwidth(mapvote_chosenmap, false, hud_fontsize*1.5/2);
                drawstring(pos, mapvote_chosenmap, hud_fontsize*1.5, '1 1 1', 1, DRAWFLAG_NORMAL);
                pos.y += hud_fontsize.y*2;
        }
 
        i = ceil(max(0, mv_timeout - time));
        map = sprintf(_("%d seconds left"), i);
-       pos_x = center - stringwidth(map, false, '8 0 0');
+       pos.x = center - stringwidth(map, false, '8 0 0');
        drawstring(pos, map, '16 16 0', '0 1 0', 1, DRAWFLAG_NORMAL);
        pos.y += 22;
-       pos_x = xmin;
+       pos.x = xmin;
        draw_endBoldFont();
 
        // base for multi-column stuff...
@@ -342,8 +364,8 @@ void MapVote_Draw()
 
        rows = ceil(mv_num_maps / mv_columns);
 
-       dist_x = (xmax - xmin) / mv_columns;
-       dist_y = (ymax - pos.y) / rows;
+       dist.x = (xmax - xmin) / mv_columns;
+       dist.y = (ymax - pos.y) / rows;
 
        if ( gametypevote )
        {
@@ -357,7 +379,7 @@ void MapVote_Draw()
                }
                else
                        dist.y += hud_fontsize.y;
-               pos_x = ( vid_conwidth - dist.x * mv_columns ) / 2;
+               pos.x = ( vid_conwidth - dist.x * mv_columns ) / 2;
        }
        else
        {
@@ -397,8 +419,8 @@ void MapVote_Draw()
 
        if(mv_abstain && i < mv_num_maps) {
                tmp = mv_votes[i];
-               pos_y = ymax + isize - hud_fontsize.y;
-               pos_x = (xmax+xmin)*0.5;
+               pos.y = ymax + isize - hud_fontsize.y;
+               pos.x = (xmax+xmin)*0.5;
                MapVote_DrawAbstain(pos, isize, xmax - xmin, tmp, i);
        }
 
@@ -407,7 +429,6 @@ void MapVote_Draw()
 
 void Cmd_MapVote_MapDownload(float argc)
 {
-       float id;
        entity pak;
 
        if(argc != 2 || !mv_pk3list)
@@ -416,7 +437,7 @@ void Cmd_MapVote_MapDownload(float argc)
                return;
        }
 
-       id = stof(argv(1));
+       int id = stof(argv(1));
        for(pak = mv_pk3list; pak; pak = pak.chain)
                if(pak.sv_entnum == id)
                        break;
@@ -483,25 +504,70 @@ void MapVote_ReadMask()
                        mask = ReadShort();
                else
                        mask = ReadLong();
-               
+
                for(i = 0, power = 1; i < mv_num_maps; ++i, power *= 2)
-                       mv_avail[i] = (mask & power) ? GTV_AVAILABLE : GTV_FORBIDDEN;
+               {
+                       if ( mask & power )
+                               mv_flags[i] |= GTV_AVAILABLE;
+                       else
+                               mv_flags[i] &= ~GTV_AVAILABLE;
+               }
        }
        else
        {
                for(i = 0; i < mv_num_maps; ++i )
-                       mv_avail[i] = ReadByte();
+                       mv_flags[i] = ReadByte();
        }
 }
 
-const int NUM_SSDIRS = 4;
-string ssdirs[NUM_SSDIRS];
-int n_ssdirs;
-void MapVote_Init()
+void MapVote_ReadOption(int i)
+{
+       string map = strzone(ReadString());
+       string pk3 = strzone(ReadString());
+       int j = bound(0, ReadByte(), n_ssdirs - 1);
+
+       mv_maps[i] = map;
+       mv_pk3[i] = pk3;
+       mv_flags[i] = GTV_AVAILABLE;
+
+       string pic = strzone(strcat(ssdirs[j], "/", map));
+       mv_pics[i] = pic;
+       mv_preview[i] = false;
+       MapVote_CheckPic(pic, pk3, i);
+}
+
+void GameTypeVote_ReadOption(int i)
 {
-       int i, j;
-       string map, pk3, s;
+       string gt = strzone(ReadString());
 
+       mv_maps[i] = gt;
+       mv_flags[i] = ReadByte();
+
+       string mv_picpath = sprintf("gfx/menu/%s/gametype_%s", autocvar_menu_skin, gt);
+       if(precache_pic(mv_picpath) == "")
+               mv_picpath = strcat("gfx/menu/default/gametype_", gt);
+       string pic = strzone(mv_picpath);
+       mv_pics[i] = pic;
+       mv_preview[i] = PreviewExists(pic);
+
+       if ( mv_flags[i] & GTV_CUSTOM )
+       {
+               string name = ReadString();
+               if ( strlen(name) < 1 )
+                       name = gt;
+               mv_pk3[i] = strzone(name);
+               mv_desc[i] = strzone(ReadString());
+       }
+       else
+       {
+               int type = MapInfo_Type_FromString(gt);
+               mv_pk3[i] = strzone(MapInfo_Type_ToText(type));
+               mv_desc[i] = MapInfo_Type_Description(type);
+       }
+}
+
+void MapVote_Init()
+{
        precache_sound ("misc/invshot.wav");
 
        mv_active = 1;
@@ -510,6 +576,7 @@ void MapVote_Init()
        mv_selection = -1;
        mv_selection_keyboard = 0;
 
+       string s;
        for(n_ssdirs = 0; ; ++n_ssdirs)
        {
                s = ReadString();
@@ -530,7 +597,7 @@ void MapVote_Init()
        mv_timeout = ReadCoord();
 
        gametypevote = ReadByte();
-       
+
        float mv_real_num_maps = mv_num_maps - mv_abstain;
 
        if(gametypevote)
@@ -541,7 +608,7 @@ void MapVote_Init()
 
                gtv_text_size = hud_fontsize*1.4;
                gtv_text_size_small = hud_fontsize*1.1;
-               
+
                if (mv_real_num_maps > 8 )
                        mv_columns = 3;
                else
@@ -560,8 +627,9 @@ void MapVote_Init()
        }
 
        MapVote_ReadMask();
+       int i;
        for(i = 0; i < mv_num_maps; ++i )
-               mv_avail_start[i] = mv_avail[i];
+               mv_flags_start[i] = mv_flags[i];
 
        // Assume mv_pk3list is world, there should only be 1 mapvote per round
        mv_pk3list = world; // I'm still paranoid!
@@ -570,32 +638,10 @@ void MapVote_Init()
        {
                mv_votes[i] = 0;
 
-               map = strzone(ReadString());
-               pk3 = strzone(ReadString());
-               j = bound(0, ReadByte(), n_ssdirs - 1);
-
-               mv_maps[i] = map;
-               mv_pk3[i] = pk3;
-               mv_avail[i] = ReadByte();
-
-               if(gametypevote)
-               {
-                       //map = strzone(strcat("gfx/menu/default/gametype_", map));
-                       //map = strzone(sprintf("gfx/menu/%s/gametype_%s", autocvar_menu_skin, map));
-                       string mv_picpath = sprintf("gfx/menu/%s/gametype_%s", autocvar_menu_skin, map);
-                       if(precache_pic(mv_picpath) == "")
-                               mv_picpath = strcat("gfx/menu/default/gametype_", map);
-                       map = strzone(mv_picpath);
-                       mv_pics[i] = map;
-                       mv_preview[i] = PreviewExists(map);
-               }
+               if ( gametypevote )
+                       GameTypeVote_ReadOption(i);
                else
-               {
-                       map = strzone(strcat(ssdirs[j], "/", map));
-                       mv_pics[i] = map;
-                       mv_preview[i] = false;
-                       MapVote_CheckPic(map, pk3, i);
-               }
+                       MapVote_ReadOption(i);
        }
 
        for(i = 0; i < n_ssdirs; ++i)
@@ -611,29 +657,29 @@ void MapVote_SendChoice(float index)
 int MapVote_MoveLeft(int pos)
 {
        int imp;
-       if ( pos < 0 ) 
+       if ( pos < 0 )
                imp = mv_num_maps - 1;
        else
                imp = pos < 1 ? mv_num_maps - 1 : pos - 1;
-       if ( mv_avail[imp] != GTV_AVAILABLE && imp != mv_ownvote )
+       if ( !(mv_flags[imp] & GTV_AVAILABLE) && imp != mv_ownvote )
                imp = MapVote_MoveLeft(imp);
        return imp;
 }
 int MapVote_MoveRight(int pos)
 {
        int imp;
-       if ( pos < 0 ) 
+       if ( pos < 0 )
                imp = 0;
        else
                imp = pos >= mv_num_maps - 1 ? 0 : pos + 1;
-       if ( mv_avail[imp] != GTV_AVAILABLE && imp != mv_ownvote )
+       if ( !(mv_flags[imp] & GTV_AVAILABLE) && imp != mv_ownvote )
                imp = MapVote_MoveRight(imp);
        return imp;
 }
 int MapVote_MoveUp(int pos)
 {
        int imp;
-       if ( pos < 0 ) 
+       if ( pos < 0 )
                imp = mv_num_maps - 1;
        else
        {
@@ -645,14 +691,14 @@ int MapVote_MoveUp(int pos)
                                imp -= mv_columns;
                }
        }
-       if ( mv_avail[imp] != GTV_AVAILABLE && imp != mv_ownvote )
+       if ( !(mv_flags[imp] & GTV_AVAILABLE) && imp != mv_ownvote )
                imp = MapVote_MoveUp(imp);
        return imp;
 }
 int MapVote_MoveDown(int pos)
 {
        int imp;
-       if ( pos < 0 ) 
+       if ( pos < 0 )
                imp = 0;
        else
        {
@@ -660,7 +706,7 @@ int MapVote_MoveDown(int pos)
                if ( imp >= mv_num_maps )
                        imp = imp % mv_columns;
        }
-       if ( mv_avail[imp] != GTV_AVAILABLE && imp != mv_ownvote )
+       if ( !(mv_flags[imp] & GTV_AVAILABLE) && imp != mv_ownvote )
                imp = MapVote_MoveDown(imp);
        return imp;
 }
@@ -674,8 +720,8 @@ float MapVote_InputEvent(float bInputType, float nPrimary, float nSecondary)
 
        if(bInputType == 3)
        {
-               mv_mousepos_x = nPrimary;
-               mv_mousepos_y = nSecondary;
+               mv_mousepos.x = nPrimary;
+               mv_mousepos.y = nSecondary;
                mv_selection_keyboard = 0;
                return true;
        }
@@ -753,7 +799,7 @@ void MapVote_UpdateVotes()
        int i;
        for(i = 0; i < mv_num_maps; ++i)
        {
-               if(mv_avail[i] == GTV_AVAILABLE)
+               if(mv_flags[i] & GTV_AVAILABLE)
                {
                        if(mv_detail)
                                mv_votes[i] = ReadByte();