1 /* bzflag
2  * Copyright (c) 1993-2021 Tim Riker
3  *
4  * This package is free software;  you can redistribute it and/or
5  * modify it under the terms of the license found in the file
6  * named COPYING that should have accompanied this file.
7  *
8  * THIS PACKAGE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
9  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
10  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
11  */
12 
13 #include "common.h"
14 #include "Team.h"
15 #include "AnsiCodes.h"
16 #include "BZDBCache.h"
17 #include "Pack.h"
18 
19 float           Team::tankColor[NumTeams][3] =
20 {
21     { 1.0f, 1.0f, 0.0f },   // rogue
22     { 1.0f, 0.0f, 0.0f },   // red
23     { 0.0f, 1.0f, 0.0f },   // green
24     { 0.1f, 0.2f, 1.0f },   // blue
25     { 1.0f, 0.0f, 1.0f },   // purple
26     { 1.0f, 1.0f, 1.0f },   // observer
27     { 0.8f, 0.8f, 0.8f },   // rabbit
28     { 1.0f, 0.5f, 0.0f }    // hunter orange
29 };
30 float           Team::radarColor[NumTeams][3] =
31 {
32     { 1.0f, 1.0f, 0.0f },   // rogue
33     { 1.0f, 0.15f, 0.15f }, // red
34     { 0.2f, 0.9f, 0.2f },   // green
35     { 0.08f, 0.25, 1.0f},   // blue
36     { 1.0f, 0.4f, 1.0f },   // purple
37     { 1.0f, 1.0f, 1.0f },   // observer
38     { 1.0f, 1.0f, 1.0f },   // rabbit
39     { 1.0f, 0.5f, 0.0f }    // hunter orange
40 };
41 float           Team::shotColor[NumTeams][3];
42 
43 
Team()44 Team::Team()
45 {
46     size = 0;
47     won = 0;
48     lost = 0;
49 }
50 
pack(void * buf) const51 void*           Team::pack(void* buf) const
52 {
53     buf = nboPackUShort(buf, uint16_t(size));
54     buf = nboPackUShort(buf, uint16_t(won));
55     buf = nboPackUShort(buf, uint16_t(lost));
56     return buf;
57 }
58 
unpack(const void * buf)59 const void*     Team::unpack(const void* buf)
60 {
61     uint16_t inSize, inWon, inLost;
62     buf = nboUnpackUShort(buf, inSize);
63     buf = nboUnpackUShort(buf, inWon);
64     buf = nboUnpackUShort(buf, inLost);
65     size = (unsigned short)inSize;
66     won = (unsigned short)inWon;
67     lost = (unsigned short)inLost;
68     return buf;
69 }
70 
getImagePrefix(TeamColor team)71 const std::string  Team::getImagePrefix(TeamColor team)
72 {
73     switch (team)
74     {
75     case RedTeam:
76         return BZDB.get("redTeamPrefix");
77     case GreenTeam:
78         return BZDB.get("greenTeamPrefix");
79     case BlueTeam:
80         return BZDB.get("blueTeamPrefix");
81     case PurpleTeam:
82         return BZDB.get("purpleTeamPrefix");
83     case RabbitTeam:
84         return BZDB.get("rabbitTeamPrefix");
85     case HunterTeam:
86         return BZDB.get("hunterTeamPrefix");
87     case ObserverTeam:
88         return BZDB.get("observerTeamPrefix");
89     default:
90         return BZDB.get("rogueTeamPrefix");
91     }
92 }
93 
getName(TeamColor team)94 const char*     Team::getName(TeamColor team) // const
95 {
96     switch (team)
97     {
98     case AutomaticTeam:
99         return "Automatic";
100     case RogueTeam:
101         return "Rogue";
102     case RedTeam:
103         return "Red Team";
104     case GreenTeam:
105         return "Green Team";
106     case BlueTeam:
107         return "Blue Team";
108     case PurpleTeam:
109         return "Purple Team";
110     case ObserverTeam:
111         return "Observer";
112     case RabbitTeam:
113         return "Rabbit";
114     case HunterTeam:
115         return "Hunter";
116     case NoTeam:
117         return "No Team??";
118     default:
119         return "Invalid team";
120     }
121 }
122 
getTeam(const std::string & name)123 TeamColor   Team::getTeam(const std::string &name) // const
124 {
125     if (name == Team::getName(AutomaticTeam))
126         return AutomaticTeam;
127     for (int i = 0; i < NumTeams; i++)
128     {
129         if (name == Team::getName((TeamColor)i))
130             return (TeamColor)i;
131     }
132     return NoTeam;
133 }
134 
getTankColor(TeamColor team)135 const float*        Team::getTankColor(TeamColor team) // const
136 {
137     if (int(team) < 0)
138         return tankColor[0];
139     return tankColor[int(team)];
140 }
141 
getRadarColor(TeamColor team)142 const float*        Team::getRadarColor(TeamColor team) // const
143 {
144     if (int(team) < 0)
145         return radarColor[0];
146     return radarColor[int(team)];
147 }
148 
getShotColor(TeamColor team)149 const float*        Team::getShotColor(TeamColor team) // const
150 {
151     if (int(team) < 0)
152         return shotColor[0];
153     return shotColor[int(team)];
154 }
155 
getAnsiCode(TeamColor team)156 const std::string   Team::getAnsiCode(TeamColor team) // const
157 {
158     return rgbToAnsi(getTankColor(team));
159 }
160 
isColorTeam(TeamColor team)161 bool        Team::isColorTeam(TeamColor team) // const
162 {
163     return team >= RedTeam  && team <= PurpleTeam;
164 }
165 
setColors(TeamColor team,const float * tank,const float * radar)166 void            Team::setColors(TeamColor team,
167                                 const float* tank,
168                                 const float* radar)
169 {
170     const int teamIndex = int(team);
171     // ignore bogus team color
172     if (teamIndex < 0)
173         return;
174 
175     for (int i = 0; i <= 2; i++)
176     {
177         tankColor[teamIndex][i] = tank[i];
178         radarColor[teamIndex][i] = radar[i];
179         shotColor[teamIndex][i] = addBrightness(tank[i]);
180     }
181 }
182 
updateShotColors()183 void            Team::updateShotColors()
184 {
185     for (int teamIndex = 0; teamIndex < NumTeams; teamIndex++)
186     {
187         for (int i = 0; i <= 2; i++)
188             shotColor[teamIndex][i] = addBrightness(tankColor[teamIndex][i]);
189     }
190 }
191 
addBrightness(const float color)192 float Team::addBrightness(const float color)
193 {
194     if (BZDBCache::shotBrightness == 0.0f)
195         return color;
196 
197     float brightness = BZDBCache::shotBrightness;
198 
199     if (brightness > 0.0f)
200         brightness *= pow(1.0f - color, 4.0f);
201     else
202         brightness *= color;
203 
204     // Make sure that the resulting color doesn't get below 0 or above 1
205     if (brightness + color > 1.0f)
206         return 1.0f;
207     else if (brightness + color < 0.0f)
208         return 0.0f;
209 
210     return brightness + color;
211 }
212 
213 // Local Variables: ***
214 // mode: C++ ***
215 // tab-width: 4 ***
216 // c-basic-offset: 4 ***
217 // indent-tabs-mode: nil ***
218 // End: ***
219 // ex: shiftwidth=4 tabstop=4
220