1 // Emacs style mode select   -*- C++ -*-
2 //-----------------------------------------------------------------------------
3 //
4 // $Id: d_netinfo.cpp 4688 2014-03-25 16:55:35Z dr_sean $
5 //
6 // Copyright (C) 1998-2006 by Randy Heit (ZDoom).
7 // Copyright (C) 2006-2014 by The Odamex Team.
8 //
9 // This program is free software; you can redistribute it and/or
10 // modify it under the terms of the GNU General Public License
11 // as published by the Free Software Foundation; either version 2
12 // of the License, or (at your option) any later version.
13 //
14 // This program is distributed in the hope that it will be useful,
15 // but WITHOUT ANY WARRANTY; without even the implied warranty of
16 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 // GNU General Public License for more details.
18 //
19 // DESCRIPTION:
20 //	D_NETINFO
21 //
22 //-----------------------------------------------------------------------------
23 
24 
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <math.h>
28 #include <cstring>
29 
30 #include "doomtype.h"
31 #include "doomdef.h"
32 #include "doomstat.h"
33 #include "d_netinf.h"
34 #include "d_net.h"
35 #include "v_palette.h"
36 #include "v_video.h"
37 #include "i_system.h"
38 #include "r_draw.h"
39 #include "r_state.h"
40 #include "st_stuff.h"
41 #include "cl_main.h"
42 
43 #include "p_ctf.h"
44 
45 // The default preference ordering when the player runs out of one type of ammo.
46 // Vanilla Doom compatible.
47 const byte UserInfo::weapon_prefs_default[NUMWEAPONS] = {
48 	0, // wp_fist
49 	4, // wp_pistol
50 	5, // wp_shotgun
51 	6, // wp_chaingun
52 	1, // wp_missile
53 	8, // wp_plasma
54 	2, // wp_bfg
55 	3, // wp_chainsaw
56 	7  // wp_supershotgun
57 };
58 
59 EXTERN_CVAR (cl_autoaim)
60 EXTERN_CVAR (cl_name)
61 EXTERN_CVAR (cl_color)
62 EXTERN_CVAR (cl_gender)
63 EXTERN_CVAR (cl_team)
64 EXTERN_CVAR (cl_unlag)
65 EXTERN_CVAR (cl_updaterate)
66 EXTERN_CVAR (cl_switchweapon)
67 EXTERN_CVAR (cl_weaponpref_fst)
68 EXTERN_CVAR (cl_weaponpref_csw)
69 EXTERN_CVAR (cl_weaponpref_pis)
70 EXTERN_CVAR (cl_weaponpref_sg)
71 EXTERN_CVAR (cl_weaponpref_ssg)
72 EXTERN_CVAR (cl_weaponpref_cg)
73 EXTERN_CVAR (cl_weaponpref_rl)
74 EXTERN_CVAR (cl_weaponpref_pls)
75 EXTERN_CVAR (cl_weaponpref_bfg)
76 EXTERN_CVAR (cl_predictweapons)
77 
78 enum
79 {
80 	INFO_Name,
81 	INFO_Autoaim,
82 	INFO_Color,
83 	INFO_Team,
84 	INFO_Gender,
85     INFO_Unlag
86 };
87 
88 
89 
90 
D_GenderByName(const char * gender)91 gender_t D_GenderByName (const char *gender)
92 {
93 	if (!stricmp (gender, "female"))
94 		return GENDER_FEMALE;
95 	else if (!stricmp (gender, "cyborg"))
96 		return GENDER_NEUTER;
97 	else
98 		return GENDER_MALE;
99 }
100 
101 //
102 //	[Toke - Teams] D_TeamToInt
103 //	Convert team string to team integer
104 //
D_TeamByName(const char * team)105 team_t D_TeamByName (const char *team)
106 {
107 	if (!stricmp (team, "blue"))
108 		return TEAM_BLUE;
109 
110 	else if (!stricmp (team, "red"))
111 		return TEAM_RED;
112 
113 	else return TEAM_NONE;
114 }
115 
116 
117 static cvar_t *weaponpref_cvar_map[NUMWEAPONS] = {
118 	&cl_weaponpref_fst, &cl_weaponpref_pis, &cl_weaponpref_sg, &cl_weaponpref_cg,
119 	&cl_weaponpref_rl, &cl_weaponpref_pls, &cl_weaponpref_bfg, &cl_weaponpref_csw,
120 	&cl_weaponpref_ssg };
121 
122 //
123 // D_PrepareWeaponPreferenceUserInfo
124 //
125 // Copies the weapon order preferences from the cl_weaponpref_* cvars
126 // to the userinfo struct for the consoleplayer.
127 //
D_PrepareWeaponPreferenceUserInfo()128 void D_PrepareWeaponPreferenceUserInfo()
129 {
130 	byte *prefs = consoleplayer().userinfo.weapon_prefs;
131 
132 	for (size_t i = 0; i < NUMWEAPONS; i++)
133 	{
134 		// sanitize the weapon preferences
135 		if (weaponpref_cvar_map[i]->asInt() < 0)
136 			weaponpref_cvar_map[i]->ForceSet(0.0f);
137 		if (weaponpref_cvar_map[i]->asInt() >= NUMWEAPONS)
138 			weaponpref_cvar_map[i]->ForceSet(float(NUMWEAPONS - 1));
139 
140 		prefs[i] = weaponpref_cvar_map[i]->asInt();
141 	}
142 }
143 
D_SetupUserInfo(void)144 void D_SetupUserInfo(void)
145 {
146 	UserInfo* coninfo = &consoleplayer().userinfo;
147 
148 	std::string netname = cl_name.str();
149 	if (netname.length() > MAXPLAYERNAME)
150 		netname.erase(MAXPLAYERNAME);
151 
152 	coninfo->netname			= netname;
153 	coninfo->team				= D_TeamByName (cl_team.cstring()); // [Toke - Teams]
154 	coninfo->color				= V_GetColorFromString (NULL, cl_color.cstring());
155 	coninfo->gender				= D_GenderByName (cl_gender.cstring());
156 	coninfo->aimdist			= (fixed_t)(cl_autoaim * 16384.0);
157 	coninfo->unlag				= (cl_unlag != 0);
158 	coninfo->update_rate		= cl_updaterate;
159 	coninfo->predict_weapons	= (cl_predictweapons != 0);
160 
161 	// sanitize the weapon switching choice
162 	if (cl_switchweapon < 0 || cl_switchweapon > 2)
163 		cl_switchweapon.ForceSet(float(WPSW_ALWAYS));
164 	coninfo->switchweapon	= (weaponswitch_t)cl_switchweapon.asInt();
165 
166 	// Copies the updated cl_weaponpref* cvars to coninfo->weapon_prefs[]
167 	D_PrepareWeaponPreferenceUserInfo();
168 
169 	// update color translation
170 	if (!demoplayback && !connected)
171 		R_BuildPlayerTranslation(consoleplayer_id, coninfo->color);
172 }
173 
D_UserInfoChanged(cvar_t * cvar)174 void D_UserInfoChanged (cvar_t *cvar)
175 {
176 	if (gamestate != GS_STARTUP && !connected)
177 		D_SetupUserInfo();
178 
179 	if (connected)
180 		CL_SendUserInfo();
181 }
182 
operator <<(FArchive & arc,UserInfo & info)183 FArchive &operator<< (FArchive &arc, UserInfo &info)
184 {
185 	arc.Write(info.netname.c_str(), MAXPLAYERNAME);
186 	arc << byte(0);		// ensure the string is properly terminated
187 
188 	arc.Write(&info.team, sizeof(info.team));  // [Toke - Teams]
189 	arc.Write(&info.gender, sizeof(info.gender));
190 
191 	arc << info.aimdist << info.color;
192 
193 	// [SL] place holder for deprecated skins
194 	unsigned int skin = 0;
195 	arc << skin;
196 
197 	arc << info.unlag << info.update_rate;
198 
199 	arc.Write(&info.switchweapon, sizeof(info.switchweapon));
200 	arc.Write(info.weapon_prefs, sizeof(info.weapon_prefs));
201  	arc << 0;
202 
203 	return arc;
204 }
205 
operator >>(FArchive & arc,UserInfo & info)206 FArchive &operator>> (FArchive &arc, UserInfo &info)
207 {
208 	int dummy;
209 
210 	char netname[MAXPLAYERNAME+1];
211 	arc.Read(netname, sizeof(netname));
212 	info.netname = netname;
213 
214 	arc.Read(&info.team, sizeof(info.team));  // [Toke - Teams]
215 	arc.Read(&info.gender, sizeof(info.gender));
216 
217 	arc >> info.aimdist >> info.color;
218 
219 	// [SL] place holder for deprecated skins
220 	unsigned int skin;
221 	arc >> skin;
222 
223 	arc >> info.unlag >> info.update_rate;
224 
225 	arc.Read(&info.switchweapon, sizeof(info.switchweapon));
226 	arc.Read(info.weapon_prefs, sizeof(info.weapon_prefs));
227 	arc >> dummy;
228 
229 	return arc;
230 }
231 
232 VERSION_CONTROL (d_netinfo_cpp, "$Id: d_netinfo.cpp 4688 2014-03-25 16:55:35Z dr_sean $")
233 
234