Skip to content

Commit d3f0ce6

Browse files
committedApr 25, 2013
Generalize hud_builtin_enable into hud_set_flags
1 parent d83602d commit d3f0ce6

12 files changed

+85
-107
lines changed
 

‎doc/lua_api.txt

+4-3
Original file line numberDiff line numberDiff line change
@@ -1434,9 +1434,10 @@ Player-only: (no-op for other objects)
14341434
- hud_change(id, stat, value): change a value of a previously added HUD element
14351435
^ element stat values: position, name, scale, text, number, item, dir
14361436
- hud_get(id): gets the HUD element definition structure of the specified ID
1437-
- hud_builtin_enable(what, flag): enable or disable built-in HUD items
1438-
^ what: "hotbar", "healthbar", "crosshair", "wielditem"
1439-
^ flag: true/false
1437+
- hud_set_flags(flags): sets specified HUD flags to true/false
1438+
^ flags: (is visible) hotbar, healthbar, crosshair, wielditem
1439+
^ pass a table containing a true/false value of each flag to be set or unset
1440+
^ if a flag is nil, the flag is not modified
14401441

14411442
InvRef: Reference to an inventory
14421443
methods:

‎src/client.cpp

+10-10
Original file line numberDiff line numberDiff line change
@@ -2097,8 +2097,8 @@ void Client::ProcessData(u8 *data, u32 datasize, u16 sender_peer_id)
20972097
u32 id = readU32(is);
20982098
u8 stat = (HudElementStat)readU8(is);
20992099

2100-
if (stat == HUD_STAT_POS || stat == HUD_STAT_SCALE
2101-
|| stat == HUD_STAT_ALIGN || stat == HUD_STAT_OFFSET)
2100+
if (stat == HUD_STAT_POS || stat == HUD_STAT_SCALE ||
2101+
stat == HUD_STAT_ALIGN || stat == HUD_STAT_OFFSET)
21022102
v2fdata = readV2F1000(is);
21032103
else if (stat == HUD_STAT_NAME || stat == HUD_STAT_TEXT)
21042104
sdata = deSerializeString(is);
@@ -2114,19 +2114,19 @@ void Client::ProcessData(u8 *data, u32 datasize, u16 sender_peer_id)
21142114
event.hudchange.data = intdata;
21152115
m_client_event_queue.push_back(event);
21162116
}
2117-
else if(command == TOCLIENT_HUD_BUILTIN_ENABLE)
2117+
else if(command == TOCLIENT_HUD_SET_FLAGS)
21182118
{
21192119
std::string datastring((char *)&data[2], datasize - 2);
21202120
std::istringstream is(datastring, std::ios_base::binary);
21212121

2122-
u32 id = readU8(is);
2123-
bool flag = (readU8(is) ? true : false);
2122+
Player *player = m_env.getLocalPlayer();
2123+
assert(player != NULL);
21242124

2125-
ClientEvent event;
2126-
event.type = CE_HUD_BUILTIN_ENABLE;
2127-
event.hudbuiltin.id = (HudBuiltinElement)id;
2128-
event.hudbuiltin.flag = flag;
2129-
m_client_event_queue.push_back(event);
2125+
u32 flags = readU32(is);
2126+
u32 mask = readU32(is);
2127+
2128+
player->hud_flags &= ~mask;
2129+
player->hud_flags |= flags;
21302130
}
21312131
else
21322132
{

‎src/client.h

+1-6
Original file line numberDiff line numberDiff line change
@@ -163,8 +163,7 @@ enum ClientEventType
163163
CE_DELETE_PARTICLESPAWNER,
164164
CE_HUDADD,
165165
CE_HUDRM,
166-
CE_HUDCHANGE,
167-
CE_HUD_BUILTIN_ENABLE
166+
CE_HUDCHANGE
168167
};
169168

170169
struct ClientEvent
@@ -244,10 +243,6 @@ struct ClientEvent
244243
std::string *sdata;
245244
u32 data;
246245
} hudchange;
247-
struct{
248-
u32 id;
249-
u32 flag;
250-
} hudbuiltin;
251246
};
252247
};
253248

‎src/clientserver.h

+3-3
Original file line numberDiff line numberDiff line change
@@ -474,11 +474,11 @@ enum ToClientCommand
474474
u32 data]
475475
*/
476476

477-
TOCLIENT_HUD_BUILTIN_ENABLE = 0x4c,
477+
TOCLIENT_HUD_SET_FLAGS = 0x4c,
478478
/*
479479
u16 command
480-
u8 id
481-
u8 flag
480+
u32 flags
481+
u32 mask
482482
*/
483483
};
484484

‎src/game.cpp

+3-12
Original file line numberDiff line numberDiff line change
@@ -2186,14 +2186,6 @@ void the_game(
21862186
delete event.hudchange.v2fdata;
21872187
delete event.hudchange.sdata;
21882188
}
2189-
else if (event.type == CE_HUD_BUILTIN_ENABLE) {
2190-
u32 bit = (u32)event.hudbuiltin.id;
2191-
u32 mask = 1 << bit;
2192-
if (event.hudbuiltin.flag)
2193-
player->hud_flags |= mask;
2194-
else
2195-
player->hud_flags &= ~mask;
2196-
}
21972189
}
21982190
}
21992191

@@ -3078,7 +3070,7 @@ void the_game(
30783070
/*
30793071
Wielded tool
30803072
*/
3081-
if(show_hud && (player->hud_flags & HUD_DRAW_WIELDITEM))
3073+
if(show_hud && (player->hud_flags & HUD_FLAG_WIELDITEM_VISIBLE))
30823074
{
30833075
// Warning: This clears the Z buffer.
30843076
camera.drawWieldedTool();
@@ -3102,7 +3094,7 @@ void the_game(
31023094
/*
31033095
Draw crosshair
31043096
*/
3105-
if (show_hud && (player->hud_flags & HUD_DRAW_CROSSHAIR))
3097+
if (show_hud)
31063098
hud.drawCrosshair();
31073099

31083100
} // timer
@@ -3117,8 +3109,7 @@ void the_game(
31173109
if (show_hud)
31183110
{
31193111
hud.drawHotbar(v2s32(displaycenter.X, screensize.Y),
3120-
client.getHP(), client.getPlayerItem(),
3121-
player->hud_flags);
3112+
client.getHP(), client.getPlayerItem());
31223113
}
31233114

31243115
/*

‎src/hud.cpp

+9-7
Original file line numberDiff line numberDiff line change
@@ -277,7 +277,7 @@ void Hud::drawStatbar(v2s32 pos, u16 corner, u16 drawdir, std::string texture, s
277277
}
278278

279279

280-
void Hud::drawHotbar(v2s32 centerlowerpos, s32 halfheartcount, u16 playeritem, u32 flags) {
280+
void Hud::drawHotbar(v2s32 centerlowerpos, s32 halfheartcount, u16 playeritem) {
281281
InventoryList *mainlist = inventory->getList("main");
282282
if (mainlist == NULL) {
283283
errorstream << "draw_hotbar(): mainlist == NULL" << std::endl;
@@ -288,19 +288,21 @@ void Hud::drawHotbar(v2s32 centerlowerpos, s32 halfheartcount, u16 playeritem, u
288288
s32 width = hotbar_itemcount * (hotbar_imagesize + padding * 2);
289289
v2s32 pos = centerlowerpos - v2s32(width / 2, hotbar_imagesize + padding * 2);
290290

291-
if (flags & HUD_DRAW_HOTBAR)
291+
if (player->hud_flags & HUD_FLAG_HOTBAR_VISIBLE)
292292
drawItem(pos, hotbar_imagesize, hotbar_itemcount, mainlist, playeritem + 1, 0);
293-
if (flags & HUD_DRAW_HEALTHBAR)
293+
if (player->hud_flags & HUD_FLAG_HEALTHBAR_VISIBLE)
294294
drawStatbar(pos - v2s32(0, 4), HUD_CORNER_LOWER, HUD_DIR_LEFT_RIGHT,
295295
"heart.png", halfheartcount, v2s32(0, 0));
296296
}
297297

298298

299299
void Hud::drawCrosshair() {
300-
driver->draw2DLine(displaycenter - v2s32(10, 0),
301-
displaycenter + v2s32(10, 0), crosshair_argb);
302-
driver->draw2DLine(displaycenter - v2s32(0, 10),
303-
displaycenter + v2s32(0, 10), crosshair_argb);
300+
if (player->hud_flags & HUD_FLAG_CROSSHAIR_VISIBLE) {
301+
driver->draw2DLine(displaycenter - v2s32(10, 0),
302+
displaycenter + v2s32(10, 0), crosshair_argb);
303+
driver->draw2DLine(displaycenter - v2s32(0, 10),
304+
displaycenter + v2s32(0, 10), crosshair_argb);
305+
}
304306
}
305307

306308

‎src/hud.h

+7-14
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,10 @@ with this program; if not, write to the Free Software Foundation, Inc.,
3131
#define HUD_CORNER_LOWER 1
3232
#define HUD_CORNER_CENTER 2
3333

34-
#define HUD_DRAW_HOTBAR (1 << 0)
35-
#define HUD_DRAW_HEALTHBAR (1 << 1)
36-
#define HUD_DRAW_CROSSHAIR (1 << 2)
37-
#define HUD_DRAW_WIELDITEM (1 << 3)
34+
#define HUD_FLAG_HOTBAR_VISIBLE (1 << 0)
35+
#define HUD_FLAG_HEALTHBAR_VISIBLE (1 << 1)
36+
#define HUD_FLAG_CROSSHAIR_VISIBLE (1 << 2)
37+
#define HUD_FLAG_WIELDITEM_VISIBLE (1 << 3)
3838

3939
class Player;
4040

@@ -71,14 +71,6 @@ struct HudElement {
7171
};
7272

7373

74-
enum HudBuiltinElement {
75-
HUD_BUILTIN_HOTBAR = 0,
76-
HUD_BUILTIN_HEALTHBAR,
77-
HUD_BUILTIN_CROSSHAIR,
78-
HUD_BUILTIN_WIELDITEM
79-
};
80-
81-
8274
inline u32 hud_get_free_id(Player *player) {
8375
size_t size = player->hud.size();
8476
for (size_t i = 0; i != size; i++) {
@@ -123,9 +115,10 @@ class Hud {
123115
void drawItem(v2s32 upperleftpos, s32 imgsize, s32 itemcount,
124116
InventoryList *mainlist, u16 selectitem, u16 direction);
125117
void drawLuaElements();
126-
void drawStatbar(v2s32 pos, u16 corner, u16 drawdir, std::string texture, s32 count, v2s32 offset);
118+
void drawStatbar(v2s32 pos, u16 corner, u16 drawdir,
119+
std::string texture, s32 count, v2s32 offset);
127120

128-
void drawHotbar(v2s32 centerlowerpos, s32 halfheartcount, u16 playeritem, u32 flags);
121+
void drawHotbar(v2s32 centerlowerpos, s32 halfheartcount, u16 playeritem);
129122
void resizeHotbar();
130123

131124
void drawCrosshair();

‎src/player.cpp

+17-19
Original file line numberDiff line numberDiff line change
@@ -54,35 +54,33 @@ Player::Player(IGameDef *gamedef):
5454
inventory.addList("craftresult", 1);
5555

5656
// Can be redefined via Lua
57-
inventory_formspec = "size[8,7.5]"
57+
inventory_formspec = "size[8,7.5]"
5858
//"image[1,0.6;1,2;player.png]"
5959
"list[current_player;main;0,3.5;8,4;]"
6060
"list[current_player;craft;3,0;3,3;]"
6161
"list[current_player;craftpreview;7,1;1,1;]";
6262

6363
// Initialize movement settings at default values, so movement can work if the server fails to send them
64-
movement_acceleration_default = 3 * BS;
65-
movement_acceleration_air = 2 * BS;
66-
movement_acceleration_fast = 10 * BS;
67-
movement_speed_walk = 4 * BS;
68-
movement_speed_crouch = 1.35 * BS;
69-
movement_speed_fast = 20 * BS;
70-
movement_speed_climb = 2 * BS;
71-
movement_speed_jump = 6.5 * BS;
72-
movement_liquid_fluidity = 1 * BS;
73-
movement_liquid_fluidity_smooth = 0.5 * BS;
74-
movement_liquid_sink = 10 * BS;
75-
movement_gravity = 9.81 * BS;
64+
movement_acceleration_default = 3 * BS;
65+
movement_acceleration_air = 2 * BS;
66+
movement_acceleration_fast = 10 * BS;
67+
movement_speed_walk = 4 * BS;
68+
movement_speed_crouch = 1.35 * BS;
69+
movement_speed_fast = 20 * BS;
70+
movement_speed_climb = 2 * BS;
71+
movement_speed_jump = 6.5 * BS;
72+
movement_liquid_fluidity = 1 * BS;
73+
movement_liquid_fluidity_smooth = 0.5 * BS;
74+
movement_liquid_sink = 10 * BS;
75+
movement_gravity = 9.81 * BS;
7676

7777
// Movement overrides are multipliers and must be 1 by default
78-
physics_override_speed = 1;
79-
physics_override_jump = 1;
78+
physics_override_speed = 1;
79+
physics_override_jump = 1;
8080
physics_override_gravity = 1;
8181

82-
hud_flags = HUD_DRAW_HOTBAR
83-
| HUD_DRAW_HEALTHBAR
84-
| HUD_DRAW_CROSSHAIR
85-
| HUD_DRAW_WIELDITEM;
82+
hud_flags = HUD_FLAG_HOTBAR_VISIBLE | HUD_FLAG_HEALTHBAR_VISIBLE |
83+
HUD_FLAG_CROSSHAIR_VISIBLE | HUD_FLAG_WIELDITEM_VISIBLE;
8684
}
8785

8886
Player::~Player()

‎src/scriptapi_object.cpp

+19-22
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,10 @@ struct EnumString es_HudElementStat[] =
5454

5555
struct EnumString es_HudBuiltinElement[] =
5656
{
57-
{HUD_BUILTIN_HOTBAR, "hotbar"},
58-
{HUD_BUILTIN_HEALTHBAR, "healthbar"},
59-
{HUD_BUILTIN_CROSSHAIR, "crosshair"},
60-
{HUD_BUILTIN_WIELDITEM, "wielditem"},
57+
{HUD_FLAG_HOTBAR_VISIBLE, "hotbar"},
58+
{HUD_FLAG_HEALTHBAR_VISIBLE, "healthbar"},
59+
{HUD_FLAG_CROSSHAIR_VISIBLE, "crosshair"},
60+
{HUD_FLAG_WIELDITEM_VISIBLE, "wielditem"},
6161
{0, NULL},
6262
};
6363

@@ -911,30 +911,29 @@ int ObjectRef::l_hud_get(lua_State *L)
911911
return 1;
912912
}
913913

914-
// hud_builtin_enable(self, id, flag)
915-
int ObjectRef::l_hud_builtin_enable(lua_State *L)
914+
// hud_set_flags(self, flags)
915+
int ObjectRef::l_hud_set_flags(lua_State *L)
916916
{
917917
ObjectRef *ref = checkobject(L, 1);
918918
Player *player = getplayer(ref);
919919
if (player == NULL)
920920
return 0;
921921

922-
HudBuiltinElement id;
923-
int id_i;
922+
u32 flags = 0;
923+
u32 mask = 0;
924+
bool flag;
924925

925-
std::string s(lua_tostring(L, 2));
926-
927-
// Return nil if component is not in enum
928-
if (!string_to_enum(es_HudBuiltinElement, id_i, s))
926+
const EnumString *esp = es_HudBuiltinElement;
927+
for (int i = 0; esp[i].str; i++) {
928+
if (getboolfield(L, 2, esp[i].str, flag)) {
929+
flags |= esp[i].num * flag;
930+
mask |= esp[i].num;
931+
}
932+
}
933+
if (!get_server(L)->hudSetFlags(player, flags, mask))
929934
return 0;
930-
931-
id = (HudBuiltinElement)id_i;
932935

933-
bool flag = (bool)lua_toboolean(L, 3);
934-
935-
bool ok = get_server(L)->hudBuiltinEnable(player, id, flag);
936-
937-
lua_pushboolean(L, (int)ok);
936+
lua_pushboolean(L, true);
938937
return 1;
939938
}
940939

@@ -1048,9 +1047,7 @@ const luaL_reg ObjectRef::methods[] = {
10481047
luamethod(ObjectRef, hud_remove),
10491048
luamethod(ObjectRef, hud_change),
10501049
luamethod(ObjectRef, hud_get),
1051-
luamethod(ObjectRef, hud_builtin_enable),
1052-
//luamethod(ObjectRef, hud_lock_next_bar),
1053-
//luamethod(ObjectRef, hud_unlock_bar),
1050+
luamethod(ObjectRef, hud_set_flags),
10541051
{0,0}
10551052
};
10561053

‎src/scriptapi_object.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -202,8 +202,8 @@ class ObjectRef
202202
// hud_get(self, id)
203203
static int l_hud_get(lua_State *L);
204204

205-
// hud_builtin_enable(self, id, flag)
206-
static int l_hud_builtin_enable(lua_State *L);
205+
// hud_set_flags(self, flags)
206+
static int l_hud_set_flags(lua_State *L);
207207

208208
public:
209209
ObjectRef(ServerActiveObject *object);

‎src/server.cpp

+7-7
Original file line numberDiff line numberDiff line change
@@ -3675,18 +3675,18 @@ void Server::SendHUDChange(u16 peer_id, u32 id, HudElementStat stat, void *value
36753675
m_con.Send(peer_id, 0, data, true);
36763676
}
36773677

3678-
void Server::SendHUDBuiltinEnable(u16 peer_id, u32 id, bool flag)
3678+
void Server::SendHUDSetFlags(u16 peer_id, u32 flags, u32 mask)
36793679
{
36803680
std::ostringstream os(std::ios_base::binary);
36813681

36823682
// Write command
3683-
writeU16(os, TOCLIENT_HUD_BUILTIN_ENABLE);
3684-
writeU8(os, id);
3685-
writeU8(os, (flag ? 1 : 0));
3683+
writeU16(os, TOCLIENT_HUD_SET_FLAGS);
3684+
writeU32(os, flags);
3685+
writeU32(os, mask);
36863686

36873687
// Make data buffer
36883688
std::string s = os.str();
3689-
SharedBuffer<u8> data((u8*)s.c_str(), s.size());
3689+
SharedBuffer<u8> data((u8 *)s.c_str(), s.size());
36903690
// Send as reliable
36913691
m_con.Send(peer_id, 0, data, true);
36923692
}
@@ -4680,11 +4680,11 @@ bool Server::hudChange(Player *player, u32 id, HudElementStat stat, void *data)
46804680
return true;
46814681
}
46824682

4683-
bool Server::hudBuiltinEnable(Player *player, u32 id, bool flag) {
4683+
bool Server::hudSetFlags(Player *player, u32 flags, u32 mask) {
46844684
if (!player)
46854685
return false;
46864686

4687-
SendHUDBuiltinEnable(player->peer_id, id, flag);
4687+
SendHUDSetFlags(player->peer_id, flags, mask);
46884688
return true;
46894689
}
46904690

0 commit comments

Comments
 (0)
Please sign in to comment.