Merge branch 'master' into terencehill/menu_listbox_changes
[xonotic/xonotic-data.pk3dir.git] / qcsrc / menu / xonotic / dialog_singleplayer.qc
1 #ifndef DIALOG_SINGLEPLAYER_H
2 #define DIALOG_SINGLEPLAYER_H
3 #include "dialog.qc"
4 CLASS(XonoticSingleplayerDialog, XonoticDialog)
5         METHOD(XonoticSingleplayerDialog, fill, void(entity))
6         ATTRIB(XonoticSingleplayerDialog, title, string, _("Singleplayer"))
7         ATTRIB(XonoticSingleplayerDialog, color, vector, SKINCOLOR_DIALOG_SINGLEPLAYER)
8         ATTRIB(XonoticSingleplayerDialog, intendedWidth, float, 0.80)
9         ATTRIB(XonoticSingleplayerDialog, rows, float, 24)
10         ATTRIB(XonoticSingleplayerDialog, columns, float, 5)
11         ATTRIB(XonoticSingleplayerDialog, campaignBox, entity, NULL)
12 ENDCLASS(XonoticSingleplayerDialog)
13 #endif
14
15 #ifdef IMPLEMENTATION
16
17 void InstantAction_LoadMap(entity btn, entity dummy)
18 {
19         float pmin = 2, pmax = 16, pstep = 1;
20
21         cvar_set("timelimit_override", "10");
22         cvar_set("g_lms_lives_override", "9");
23
24         if(random() < 0.4) // 40% are DM
25         {
26                 MapInfo_SwitchGameType(MAPINFO_TYPE_DEATHMATCH);
27                 pmin = 2;
28                 pmax = 8;
29                 pstep = 1;
30         }
31         else if(random() < 0.5) // half of the remaining 60%, i.e. 30%, are CTF
32         {
33                 MapInfo_SwitchGameType(MAPINFO_TYPE_CTF);
34                 pmin = 4;
35                 pmax = 12;
36                 pstep = 2;
37         }
38         else if(random() < 0.5) // half of the remaining 30%, i.e. 15%, are TDM
39         {
40                 MapInfo_SwitchGameType(MAPINFO_TYPE_TEAM_DEATHMATCH);
41                 pmin = 4;
42                 pmax = 8;
43                 pstep = 2;
44         }
45         else if(random() < 0.666) // 2/3 of the remaining 15%, i.e. 10%, are KH
46         {
47                 MapInfo_SwitchGameType(MAPINFO_TYPE_KEYHUNT);
48                 pmin = 6;
49                 pmax = 6;
50                 pstep = 6; // works both for 2 and 3 teams
51                 // TODO find team count of map, set pstep=2 or 3, and use 2v2(v2) games at least
52         }
53         else // somehow distribute the remaining 5%
54         {
55                 float r;
56                 r = floor(random() * 4);
57                 switch(r)
58                 {
59                         default:
60                         case 0:
61                                 MapInfo_SwitchGameType(MAPINFO_TYPE_LMS);
62                                 pmin = 2;
63                                 pmax = 6;
64                                 pstep = 1;
65                                 cvar_set("timelimit_override", "-1");
66                                 break;
67                         case 1:
68                                 MapInfo_SwitchGameType(MAPINFO_TYPE_DOMINATION);
69                                 pmin = 2;
70                                 pmax = 8;
71                                 pstep = 2;
72                                 break;
73                         case 2:
74                                 MapInfo_SwitchGameType(MAPINFO_TYPE_ONSLAUGHT);
75                                 pmin = 6;
76                                 pmax = 16;
77                                 pstep = 2;
78                                 break;
79                         case 3:
80                                 MapInfo_SwitchGameType(MAPINFO_TYPE_ASSAULT);
81                                 pmin = 4;
82                                 pmax = 16;
83                                 pstep = 2;
84                                 break;
85                         // CA, Freezetag: bot AI does not work, add them once it does
86                 }
87         }
88
89         // find random map
90         MapInfo_FilterGametype(MapInfo_CurrentGametype(), MapInfo_CurrentFeatures(), MapInfo_RequiredFlags(), MapInfo_ForbiddenFlags(), 0);
91         string s;
92         do
93         {
94                 float m;
95                 m = floor(random() * MapInfo_count);
96                 s = MapInfo_BSPName_ByID(m);
97         }
98         while(!fexists(sprintf("maps/%s.waypoints", s)));
99         MapInfo_LoadMap(s, 1);
100
101         // configure bots
102         float p;
103         pmin = pstep * ceil(pmin / pstep);
104         pmax = pstep * floor(pmax / pstep);
105         p = pmin + pstep * floor(random() * ((pmax - pmin) / pstep + 1));
106         cvar_set("bot_number", ftos(p - 1));
107
108         // make sure we go back to menu
109         cvar_set("lastlevel", "1");
110 }
111
112 void XonoticSingleplayerDialog_fill(entity me)
113 {
114         entity e, btnPrev, btnNext, lblTitle;
115
116         me.TR(me);
117                 me.TDempty(me, (me.columns - 3) / 2);
118                 me.TD(me, 2, 3, e = makeXonoticBigButton(_("Instant action! (random map with bots)"), '0 0 0'));
119                         e.onClick = InstantAction_LoadMap;
120                         e.onClickEntity = NULL;
121         me.TR(me);
122         me.TR(me);
123         me.TR(me);
124                 me.TD(me, 1, 1, btnPrev = makeXonoticButton("<<", '0 0 0'));
125                 me.TD(me, 1, me.columns - 2, lblTitle = makeXonoticTextLabel(0.5, _("???")));
126                 me.TD(me, 1, 1, btnNext = makeXonoticButton(">>", '0 0 0'));
127         me.TR(me);
128                 me.TD(me, me.rows - 6, me.columns, me.campaignBox = makeXonoticCampaignList());
129                         btnPrev.onClick = MultiCampaign_Prev;
130                         btnPrev.onClickEntity = me.campaignBox;
131                         btnNext.onClick = MultiCampaign_Next;
132                         btnNext.onClickEntity = me.campaignBox;
133                         me.campaignBox.buttonNext = btnNext;
134                         me.campaignBox.buttonPrev = btnPrev;
135                         me.campaignBox.labelTitle = lblTitle;
136
137         me.gotoRC(me, me.rows - 2, 0);
138                 me.TD(me, 1, 2, e = makeXonoticTextLabel(0.5, _("Campaign Difficulty:")));
139                 me.TD(me, 1, 1, e = makeXonoticRadioButton(1, "g_campaign_skill", "-2", ZCTX(_("CSKL^Easy"))));
140                 me.TD(me, 1, 1, e = makeXonoticRadioButton(1, "g_campaign_skill", "-1", ZCTX(_("CSKL^Medium"))));
141                 me.TD(me, 1, 1, e = makeXonoticRadioButton(1, "g_campaign_skill", "0", ZCTX(_("CSKL^Hard"))));
142                 me.TR(me);
143                 me.TD(me, 1, me.columns, e = makeXonoticButton(_("Start Singleplayer!"), '0 0 0'));
144                         e.onClick = CampaignList_LoadMap;
145                         e.onClickEntity = me.campaignBox;
146 }
147 #endif