Skip to content

Commit 96fe1de

Browse files
committedMay 25, 2013
Add ObjectRef.hud_set_hotbar_itemcount and add TOCLIENT_HUD_SET_PARAM
1 parent 730d316 commit 96fe1de

File tree

12 files changed

+89
-4
lines changed

12 files changed

+89
-4
lines changed
 

‎doc/lua_api.txt

+2
Original file line numberDiff line numberDiff line change
@@ -1399,6 +1399,8 @@ Player-only: (no-op for other objects)
13991399
^ flags: (is visible) hotbar, healthbar, crosshair, wielditem
14001400
^ pass a table containing a true/false value of each flag to be set or unset
14011401
^ if a flag is nil, the flag is not modified
1402+
- hud_set_hotbar_itemcount(count): sets number of items in builtin hotbar
1403+
^ count: number of items, must be between 1 and 23
14021404

14031405
InvRef: Reference to an inventory
14041406
methods:

‎src/client.cpp

+17
Original file line numberDiff line numberDiff line change
@@ -2136,6 +2136,23 @@ void Client::ProcessData(u8 *data, u32 datasize, u16 sender_peer_id)
21362136
player->hud_flags &= ~mask;
21372137
player->hud_flags |= flags;
21382138
}
2139+
else if(command == TOCLIENT_HUD_SET_PARAM)
2140+
{
2141+
std::string datastring((char *)&data[2], datasize - 2);
2142+
std::istringstream is(datastring, std::ios_base::binary);
2143+
2144+
Player *player = m_env.getLocalPlayer();
2145+
assert(player != NULL);
2146+
2147+
u16 param = readU16(is);
2148+
std::string value = deSerializeString(is);
2149+
2150+
if(param == HUD_PARAM_HOTBAR_ITEMCOUNT && value.size() == 4){
2151+
s32 hotbar_itemcount = readS32((u8*) value.c_str());
2152+
if(hotbar_itemcount > 0 && hotbar_itemcount <= HUD_HOTBAR_ITEMCOUNT_MAX)
2153+
player->hud_hotbar_itemcount = hotbar_itemcount;
2154+
}
2155+
}
21392156
else
21402157
{
21412158
infostream<<"Client: Ignoring unknown command "

‎src/clientserver.h

+8
Original file line numberDiff line numberDiff line change
@@ -480,6 +480,14 @@ enum ToClientCommand
480480
u32 flags
481481
u32 mask
482482
*/
483+
484+
TOCLIENT_HUD_SET_PARAM = 0x4d,
485+
/*
486+
u16 command
487+
u16 param
488+
u16 len
489+
u8[len] value
490+
*/
483491
};
484492

485493
enum ToServerCommand

‎src/game.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -1987,7 +1987,7 @@ void the_game(
19871987
{
19881988
s32 wheel = input->getMouseWheel();
19891989
u16 max_item = MYMIN(PLAYER_INVENTORY_SIZE-1,
1990-
hud.hotbar_itemcount-1);
1990+
player->hud_hotbar_itemcount-1);
19911991

19921992
if(wheel < 0)
19931993
{
@@ -2011,7 +2011,7 @@ void the_game(
20112011
const KeyPress *kp = NumberKey + (i + 1) % 10;
20122012
if(input->wasKeyDown(*kp))
20132013
{
2014-
if(i < PLAYER_INVENTORY_SIZE && i < hud.hotbar_itemcount)
2014+
if(i < PLAYER_INVENTORY_SIZE && i < player->hud_hotbar_itemcount)
20152015
{
20162016
new_playeritem = i;
20172017

‎src/hud.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@ Hud::Hud(video::IVideoDriver *driver, gui::IGUIEnvironment* guienv,
4343
screensize = v2u32(0, 0);
4444
displaycenter = v2s32(0, 0);
4545
hotbar_imagesize = 48;
46-
hotbar_itemcount = 8;
4746

4847
tsrc = gamedef->getTextureSource();
4948

@@ -286,6 +285,7 @@ void Hud::drawHotbar(v2s32 centerlowerpos, s32 halfheartcount, u16 playeritem) {
286285
return;
287286
}
288287

288+
s32 hotbar_itemcount = player->hud_hotbar_itemcount;
289289
s32 padding = hotbar_imagesize / 12;
290290
s32 width = hotbar_itemcount * (hotbar_imagesize + padding * 2);
291291
v2s32 pos = centerlowerpos - v2s32(width / 2, hotbar_imagesize + padding * 2);

‎src/hud.h

+5-1
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,11 @@ with this program; if not, write to the Free Software Foundation, Inc.,
3636
#define HUD_FLAG_CROSSHAIR_VISIBLE (1 << 2)
3737
#define HUD_FLAG_WIELDITEM_VISIBLE (1 << 3)
3838

39+
#define HUD_PARAM_HOTBAR_ITEMCOUNT 1
40+
41+
#define HUD_HOTBAR_ITEMCOUNT_DEFAULT 8
42+
#define HUD_HOTBAR_ITEMCOUNT_MAX 23
43+
3944
class Player;
4045

4146
enum HudElementType {
@@ -102,7 +107,6 @@ class Hud {
102107
v2u32 screensize;
103108
v2s32 displaycenter;
104109
s32 hotbar_imagesize;
105-
s32 hotbar_itemcount;
106110

107111
video::SColor crosshair_argb;
108112
video::SColor selectionbox_argb;

‎src/player.cpp

+2
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,8 @@ Player::Player(IGameDef *gamedef):
8181

8282
hud_flags = HUD_FLAG_HOTBAR_VISIBLE | HUD_FLAG_HEALTHBAR_VISIBLE |
8383
HUD_FLAG_CROSSHAIR_VISIBLE | HUD_FLAG_WIELDITEM_VISIBLE;
84+
85+
hud_hotbar_itemcount = HUD_HOTBAR_ITEMCOUNT_DEFAULT;
8486
}
8587

8688
Player::~Player()

‎src/player.h

+1
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,7 @@ class Player
250250

251251
std::vector<HudElement *> hud;
252252
u32 hud_flags;
253+
s32 hud_hotbar_itemcount;
253254

254255
protected:
255256
IGameDef *m_gamedef;

‎src/script/lua_api/l_object.cpp

+18
Original file line numberDiff line numberDiff line change
@@ -978,6 +978,23 @@ int ObjectRef::l_hud_set_flags(lua_State *L)
978978
return 1;
979979
}
980980

981+
// hud_set_hotbar_itemcount(self, hotbar_itemcount)
982+
int ObjectRef::l_hud_set_hotbar_itemcount(lua_State *L)
983+
{
984+
ObjectRef *ref = checkobject(L, 1);
985+
Player *player = getplayer(ref);
986+
if (player == NULL)
987+
return 0;
988+
989+
s32 hotbar_itemcount = lua_tonumber(L, 2);
990+
991+
if (!STACK_TO_SERVER(L)->hudSetHotbarItemcount(player, hotbar_itemcount))
992+
return 0;
993+
994+
lua_pushboolean(L, true);
995+
return 1;
996+
}
997+
981998
ObjectRef::ObjectRef(ServerActiveObject *object):
982999
m_object(object)
9831000
{
@@ -1089,6 +1106,7 @@ const luaL_reg ObjectRef::methods[] = {
10891106
luamethod(ObjectRef, hud_change),
10901107
luamethod(ObjectRef, hud_get),
10911108
luamethod(ObjectRef, hud_set_flags),
1109+
luamethod(ObjectRef, hud_set_hotbar_itemcount),
10921110
{0,0}
10931111
};
10941112

‎src/script/lua_api/l_object.h

+3
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,9 @@ class ObjectRef
209209
// hud_set_flags(self, flags)
210210
static int l_hud_set_flags(lua_State *L);
211211

212+
// hud_set_hotbar_itemcount(self, hotbar_itemcount)
213+
static int l_hud_set_hotbar_itemcount(lua_State *L);
214+
212215
public:
213216
ObjectRef(ServerActiveObject *object);
214217

‎src/server.cpp

+28
Original file line numberDiff line numberDiff line change
@@ -3687,6 +3687,22 @@ void Server::SendHUDSetFlags(u16 peer_id, u32 flags, u32 mask)
36873687
m_con.Send(peer_id, 0, data, true);
36883688
}
36893689

3690+
void Server::SendHUDSetParam(u16 peer_id, u16 param, const std::string &value)
3691+
{
3692+
std::ostringstream os(std::ios_base::binary);
3693+
3694+
// Write command
3695+
writeU16(os, TOCLIENT_HUD_SET_PARAM);
3696+
writeU16(os, param);
3697+
os<<serializeString(value);
3698+
3699+
// Make data buffer
3700+
std::string s = os.str();
3701+
SharedBuffer<u8> data((u8 *)s.c_str(), s.size());
3702+
// Send as reliable
3703+
m_con.Send(peer_id, 0, data, true);
3704+
}
3705+
36903706
void Server::BroadcastChatMessage(const std::wstring &message)
36913707
{
36923708
for(std::map<u16, RemoteClient*>::iterator
@@ -4684,6 +4700,18 @@ bool Server::hudSetFlags(Player *player, u32 flags, u32 mask) {
46844700
return true;
46854701
}
46864702

4703+
bool Server::hudSetHotbarItemcount(Player *player, s32 hotbar_itemcount) {
4704+
if (!player)
4705+
return false;
4706+
if (hotbar_itemcount <= 0 || hotbar_itemcount > HUD_HOTBAR_ITEMCOUNT_MAX)
4707+
return false;
4708+
4709+
std::ostringstream os(std::ios::binary);
4710+
writeS32(os, hotbar_itemcount);
4711+
SendHUDSetParam(player->peer_id, HUD_PARAM_HOTBAR_ITEMCOUNT, os.str());
4712+
return true;
4713+
}
4714+
46874715
void Server::notifyPlayers(const std::wstring msg)
46884716
{
46894717
BroadcastChatMessage(msg);

‎src/server.h

+2
Original file line numberDiff line numberDiff line change
@@ -541,6 +541,7 @@ class Server : public con::PeerHandler, public MapEventReceiver,
541541
bool hudRemove(Player *player, u32 id);
542542
bool hudChange(Player *player, u32 id, HudElementStat stat, void *value);
543543
bool hudSetFlags(Player *player, u32 flags, u32 mask);
544+
bool hudSetHotbarItemcount(Player *player, s32 hotbar_itemcount);
544545

545546
private:
546547

@@ -585,6 +586,7 @@ class Server : public con::PeerHandler, public MapEventReceiver,
585586
void SendHUDRemove(u16 peer_id, u32 id);
586587
void SendHUDChange(u16 peer_id, u32 id, HudElementStat stat, void *value);
587588
void SendHUDSetFlags(u16 peer_id, u32 flags, u32 mask);
589+
void SendHUDSetParam(u16 peer_id, u16 param, const std::string &value);
588590

589591
/*
590592
Send a node removal/addition event to all clients except ignore_id.

0 commit comments

Comments
 (0)
Please sign in to comment.