1 
2 /* Battle Tanks Game
3  * Copyright (C) 2006-2009 Battle Tanks team
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License
7  * as published by the Free Software Foundation; either version 2
8  * of the License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
18  */
19 
20 /*
21  * Additional rights can be granted beyond the GNU General Public License
22  * on the terms provided in the Exception. If you modify this file,
23  * you may extend this exception to your version of the file,
24  * but you are not obligated to do so. If you do not wish to provide this
25  * exception without modification, you must delete this exception statement
26  * from your version and license this file solely under the GPL without exception.
27 */
28 #include "start_server_menu.h"
29 #include "button.h"
30 #include "mrt/logger.h"
31 #include "menu.h"
32 #include "menu_config.h"
33 #include "map_picker.h"
34 #include "game.h"
35 #include "game_monitor.h"
36 #include "map_desc.h"
37 #include "player_manager.h"
38 #include "i18n.h"
39 #include "rt_config.h"
40 #include "config.h"
41 #include "box.h"
42 
StartServerMenu(const int w,const int h)43 StartServerMenu::StartServerMenu(const int w, const int h)  {
44 	_map_picker = new MapPicker(w, h);
45 	int y1, y2;
46 	_map_picker->getBaseSize(y1, y2);
47 	add(0, y1, new Box("menu/background_box.png", w, y2 - y1 - 16));
48 
49 	int cw, ch;
50 	_map_picker->get_size(cw, ch);
51 
52 	_back = new Button("big", I18n->get("menu", "back"));
53 	int bw, bh;
54 	_back->get_size(bw, bh);
55 	add(64, h - (h - ch) / 2 - bh / 2, _back);
56 
57 	_start = new Button("big", I18n->get("menu", "start"));
58 	_start->get_size(bw, bh);
59 	add(w - 64 - bw, h - (h - ch) / 2 - bh / 2, _start);
60 
61 	add(0, 0, _map_picker);
62 }
63 
start()64 void StartServerMenu::start() {
65 	LOG_DEBUG(("starting the game"));
66 
67 	const MapDesc &map = _map_picker->getCurrentMap();
68 	if (map.slots < 1) {
69 		GameMonitor->displayMessage("menu", "no-slots-in-map", 1);
70 		return;
71 	}
72 	int idx;
73 	Config->get("menu.default-game-mode", idx, 0);
74 
75 	switch(idx) {
76 	case 3: //ctf
77 		if (!map.supports_ctf)
78 			throw_ex(("start: map does not support ctf, but menu requested mode %d", idx));
79 
80 		LOG_DEBUG(("starting map in CTF mode. good luck."));
81 		RTConfig->game_type = GameTypeCTF;
82 		RTConfig->teams = 2;
83 		break;
84 
85 	case 1: { //team dethmatch
86 		int teams;
87 		Config->get("multiplayer.teams", teams, 0);
88 		if (teams <= 0)
89 			throw_ex(("start: requested team deathmatch, but teams == %d", teams));
90 
91 		RTConfig->game_type = GameTypeTeamDeathMatch;
92 		RTConfig->teams = teams;
93 		} break;
94 
95 	case 0:
96 		if (map.game_type != GameTypeDeathMatch)
97 			throw_ex(("menu game type == deathmatch, map game type: %d", (int)map.game_type));
98 		RTConfig->game_type = map.game_type;
99 		break;
100 
101 	case 2:
102 		if (map.game_type != GameTypeCooperative)
103 			throw_ex(("menu game type == cooperative, map game type: %d", (int)map.game_type));
104 		RTConfig->game_type = map.game_type;
105 		break;
106 
107 	default:
108 		throw_ex(("unsupported game type %d", idx));
109 	}
110 
111 	if (RTConfig->game_type != GameTypeCooperative && RTConfig->game_type != GameTypeRacing) {
112 		int tl;
113 		Config->get("multiplayer.time-limit", tl, 0);
114 		RTConfig->time_limit = (float)tl;
115 	} else {
116 		RTConfig->time_limit = 0;
117 	}
118 
119 	Game->clear();
120 	PlayerManager->start_server();
121 	GameMonitor->loadMap(NULL, map.name);
122 
123 	_map_picker->fillSlots();
124 
125 	MenuConfig->save();
126 
127 	//_parent->back();
128 	return;
129 }
130 
tick(const float dt)131 void StartServerMenu::tick(const float dt) {
132 	Container::tick(dt);
133 	if (_back->changed()) {
134 		LOG_DEBUG(("[back] clicked"));
135 		_back->reset();
136 		hide();
137 		MenuConfig->save();
138 	}
139 	if (_start->changed()) {
140 		_start->reset();
141 		start();
142 	}
143 
144 }
145 
onKey(const SDL_keysym sym)146 bool StartServerMenu::onKey(const SDL_keysym sym) {
147 	if (Container::onKey(sym))
148 		return true;
149 
150 	switch(sym.sym) {
151 
152 	case SDLK_KP_ENTER:
153 	case SDLK_RETURN:
154 		start();
155 		return true;
156 
157 	case SDLK_ESCAPE:
158 		hide();
159 		MenuConfig->save();
160 		return true;
161 
162 	default: ;
163 	}
164 	return false;
165 }
166 
167 
~StartServerMenu()168 StartServerMenu::~StartServerMenu() {}
169