Skip to content

Commit d19c8b8

Browse files
committedJul 20, 2013
Add set_breath and get_breath to lua API.
1 parent ab145c8 commit d19c8b8

14 files changed

+195
-19
lines changed
 

‎doc/lua_api.txt

+5
Original file line numberDiff line numberDiff line change
@@ -1547,6 +1547,11 @@ Player-only: (no-op for other objects)
15471547
- get_look_yaw(): yaw in radians (wraps around pretty randomly as of now)
15481548
- set_look_pitch(radians): sets look pitch
15491549
- set_look_yaw(radians): sets look yaw
1550+
- get_breath() : returns players breath
1551+
- set_breath(value) : sets players breath
1552+
values: 0 player is drowning,
1553+
1-10 number of bubbles remain,
1554+
11 bubbles bar is not shown
15501555
- set_inventory_formspec(formspec)
15511556
^ Redefine player's inventory form
15521557
^ Should usually be called in on_joinplayer

‎src/client.cpp

+30-2
Original file line numberDiff line numberDiff line change
@@ -692,9 +692,14 @@ void Client::step(float dtime)
692692
m_client_event_queue.push_back(event);
693693
}
694694
}
695+
else if(event.type == CEE_PLAYER_BREATH)
696+
{
697+
u16 breath = event.player_breath.amount;
698+
sendBreath(breath);
699+
}
695700
}
696701
}
697-
702+
698703
/*
699704
Print some info
700705
*/
@@ -1579,6 +1584,15 @@ void Client::ProcessData(u8 *data, u32 datasize, u16 sender_peer_id)
15791584
m_client_event_queue.push_back(event);
15801585
}
15811586
}
1587+
else if(command == TOCLIENT_BREATH)
1588+
{
1589+
std::string datastring((char*)&data[2], datasize-2);
1590+
std::istringstream is(datastring, std::ios_base::binary);
1591+
Player *player = m_env.getLocalPlayer();
1592+
assert(player != NULL);
1593+
u16 breath = readU16(is);
1594+
player->setBreath(breath) ;
1595+
}
15821596
else if(command == TOCLIENT_MOVE_PLAYER)
15831597
{
15841598
std::string datastring((char*)&data[2], datasize-2);
@@ -2359,6 +2373,20 @@ void Client::sendDamage(u8 damage)
23592373
Send(0, data, true);
23602374
}
23612375

2376+
void Client::sendBreath(u16 breath)
2377+
{
2378+
DSTACK(__FUNCTION_NAME);
2379+
std::ostringstream os(std::ios_base::binary);
2380+
2381+
writeU16(os, TOSERVER_BREATH);
2382+
writeU16(os, breath);
2383+
// Make data buffer
2384+
std::string s = os.str();
2385+
SharedBuffer<u8> data((u8*)s.c_str(), s.size());
2386+
// Send as reliable
2387+
Send(0, data, true);
2388+
}
2389+
23622390
void Client::sendRespawn()
23632391
{
23642392
DSTACK(__FUNCTION_NAME);
@@ -2694,7 +2722,7 @@ u16 Client::getBreath()
26942722
{
26952723
Player *player = m_env.getLocalPlayer();
26962724
assert(player != NULL);
2697-
return player->breath;
2725+
return player->getBreath();
26982726
}
26992727

27002728
bool Client::getChatMessage(std::wstring &message)

‎src/client.h

+1
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,7 @@ class Client : public con::PeerHandler, public InventoryManager, public IGameDef
306306
void sendChangePassword(const std::wstring oldpassword,
307307
const std::wstring newpassword);
308308
void sendDamage(u8 damage);
309+
void sendBreath(u16 breath);
309310
void sendRespawn();
310311

311312
ClientEnvironment& getEnv()

‎src/clientserver.h

+12
Original file line numberDiff line numberDiff line change
@@ -488,6 +488,12 @@ enum ToClientCommand
488488
u16 len
489489
u8[len] value
490490
*/
491+
492+
TOCLIENT_BREATH = 0x4e,
493+
/*
494+
u16 command
495+
u16 breath
496+
*/
491497
};
492498

493499
enum ToServerCommand
@@ -711,6 +717,12 @@ enum ToServerCommand
711717
/*
712718
u16 command
713719
*/
720+
721+
TOSERVER_BREATH = 0x42,
722+
/*
723+
u16 command
724+
u16 breath
725+
*/
714726
};
715727

716728
#endif

‎src/content_sao.cpp

+11
Original file line numberDiff line numberDiff line change
@@ -935,6 +935,7 @@ PlayerSAO::PlayerSAO(ServerEnvironment *env_, Player *player_, u16 peer_id_,
935935
m_moved(false),
936936
m_inventory_not_sent(false),
937937
m_hp_not_sent(false),
938+
m_breath_not_sent(false),
938939
m_wielded_item_not_sent(false),
939940
m_physics_override_speed(1),
940941
m_physics_override_jump(1),
@@ -1370,6 +1371,16 @@ void PlayerSAO::setHP(s16 hp)
13701371
}
13711372
}
13721373

1374+
u16 PlayerSAO::getBreath() const
1375+
{
1376+
return m_player->getBreath();
1377+
}
1378+
1379+
void PlayerSAO::setBreath(u16 breath)
1380+
{
1381+
m_player->setBreath(breath);
1382+
}
1383+
13731384
void PlayerSAO::setArmorGroups(const ItemGroupList &armor_groups)
13741385
{
13751386
m_armor_groups = armor_groups;

‎src/content_sao.h

+3-1
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,8 @@ class PlayerSAO : public ServerActiveObject
162162
void rightClick(ServerActiveObject *clicker);
163163
s16 getHP() const;
164164
void setHP(s16 hp);
165-
165+
u16 getBreath() const;
166+
void setBreath(u16 breath);
166167
void setArmorGroups(const ItemGroupList &armor_groups);
167168
void setAnimation(v2f frame_range, float frame_speed, float frame_blend);
168169
void setBonePosition(std::string bone, v3f position, v3f rotation);
@@ -282,6 +283,7 @@ class PlayerSAO : public ServerActiveObject
282283
bool m_moved;
283284
bool m_inventory_not_sent;
284285
bool m_hp_not_sent;
286+
bool m_breath_not_sent;
285287
bool m_wielded_item_not_sent;
286288

287289
float m_physics_override_speed;

‎src/environment.cpp

+29-11
Original file line numberDiff line numberDiff line change
@@ -2242,15 +2242,19 @@ void ClientEnvironment::step(float dtime)
22422242
v3s16 p = floatToInt(pf + v3f(0, BS*1.6, 0), BS);
22432243
MapNode n = m_map->getNodeNoEx(p);
22442244
ContentFeatures c = m_gamedef->ndef()->get(n);
2245-
2246-
if(c.isLiquid() && c.drowning){
2247-
if(lplayer->breath > 10)
2248-
lplayer->breath = 11;
2249-
if(lplayer->breath > 0)
2250-
lplayer->breath -= 1;
2245+
if(c.isLiquid() && c.drowning && lplayer->hp > 0){
2246+
u16 breath = lplayer->getBreath();
2247+
if(breath > 10){
2248+
breath = 11;
2249+
}
2250+
if(breath > 0){
2251+
breath -= 1;
2252+
}
2253+
lplayer->setBreath(breath);
2254+
updateLocalPlayerBreath(breath);
22512255
}
22522256

2253-
if(lplayer->breath == 0){
2257+
if(lplayer->getBreath() == 0){
22542258
damageLocalPlayer(1, true);
22552259
}
22562260
}
@@ -2262,10 +2266,16 @@ void ClientEnvironment::step(float dtime)
22622266
v3s16 p = floatToInt(pf + v3f(0, BS*1.6, 0), BS);
22632267
MapNode n = m_map->getNodeNoEx(p);
22642268
ContentFeatures c = m_gamedef->ndef()->get(n);
2265-
2266-
if(!c.isLiquid() || !c.drowning){
2267-
if(lplayer->breath <= 10)
2268-
lplayer->breath += 1;
2269+
if (!lplayer->hp){
2270+
lplayer->setBreath(11);
2271+
}
2272+
else if(!c.isLiquid() || !c.drowning){
2273+
u16 breath = lplayer->getBreath();
2274+
if(breath <= 10){
2275+
breath += 1;
2276+
lplayer->setBreath(breath);
2277+
updateLocalPlayerBreath(breath);
2278+
}
22692279
}
22702280
}
22712281

@@ -2528,6 +2538,14 @@ void ClientEnvironment::damageLocalPlayer(u8 damage, bool handle_hp)
25282538
m_client_event_queue.push_back(event);
25292539
}
25302540

2541+
void ClientEnvironment::updateLocalPlayerBreath(u16 breath)
2542+
{
2543+
ClientEnvEvent event;
2544+
event.type = CEE_PLAYER_BREATH;
2545+
event.player_breath.amount = breath;
2546+
m_client_event_queue.push_back(event);
2547+
}
2548+
25312549
/*
25322550
Client likes to call these
25332551
*/

‎src/environment.h

+6-1
Original file line numberDiff line numberDiff line change
@@ -395,7 +395,8 @@ class ClientSimpleObject;
395395
enum ClientEnvEventType
396396
{
397397
CEE_NONE,
398-
CEE_PLAYER_DAMAGE
398+
CEE_PLAYER_DAMAGE,
399+
CEE_PLAYER_BREATH
399400
};
400401

401402
struct ClientEnvEvent
@@ -408,6 +409,9 @@ struct ClientEnvEvent
408409
u8 amount;
409410
bool send_to_server;
410411
} player_damage;
412+
struct{
413+
u16 amount;
414+
} player_breath;
411415
};
412416
};
413417

@@ -462,6 +466,7 @@ class ClientEnvironment : public Environment
462466
*/
463467

464468
void damageLocalPlayer(u8 damage, bool handle_hp=true);
469+
void updateLocalPlayerBreath(u16 breath);
465470

466471
/*
467472
Client likes to call these

‎src/player.cpp

+8-2
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,10 @@ Player::Player(IGameDef *gamedef):
3636
camera_barely_in_ceiling(false),
3737
inventory(gamedef->idef()),
3838
hp(PLAYER_MAX_HP),
39-
breath(-1),
4039
peer_id(PEER_ID_INEXISTENT),
4140
// protected
4241
m_gamedef(gamedef),
42+
m_breath(-1),
4343
m_pitch(0),
4444
m_yaw(0),
4545
m_speed(0,0,0),
@@ -177,11 +177,12 @@ void Player::serialize(std::ostream &os)
177177
args.setFloat("yaw", m_yaw);
178178
args.setV3F("position", m_position);
179179
args.setS32("hp", hp);
180+
args.setS32("breath", m_breath);
180181

181182
args.writeLines(os);
182183

183184
os<<"PlayerArgsEnd\n";
184-
185+
185186
inventory.serialize(os);
186187
}
187188

@@ -213,6 +214,11 @@ void Player::deSerialize(std::istream &is, std::string playername)
213214
}catch(SettingNotFoundException &e){
214215
hp = 20;
215216
}
217+
try{
218+
m_breath = args.getS32("breath");
219+
}catch(SettingNotFoundException &e){
220+
m_breath = 11;
221+
}
216222

217223
inventory.deSerialize(is);
218224

‎src/player.h

+12-2
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,16 @@ class Player
160160
return m_yaw;
161161
}
162162

163+
u16 getBreath()
164+
{
165+
return m_breath;
166+
}
167+
168+
virtual void setBreath(u16 breath)
169+
{
170+
m_breath = breath;
171+
}
172+
163173
f32 getRadPitch()
164174
{
165175
return -1.0 * m_pitch * core::DEGTORAD;
@@ -249,13 +259,12 @@ class Player
249259
float physics_override_gravity;
250260

251261
u16 hp;
252-
u16 breath;
253262

254263
float hurt_tilt_timer;
255264
float hurt_tilt_strength;
256265

257266
u16 peer_id;
258-
267+
259268
std::string inventory_formspec;
260269

261270
PlayerControl control;
@@ -274,6 +283,7 @@ class Player
274283
IGameDef *m_gamedef;
275284

276285
char m_name[PLAYERNAME_SIZE];
286+
u16 m_breath;
277287
f32 m_pitch;
278288
f32 m_yaw;
279289
v3f m_speed;

‎src/script/lua_api/l_object.cpp

+29
Original file line numberDiff line numberDiff line change
@@ -702,6 +702,33 @@ int ObjectRef::l_set_look_yaw(lua_State *L)
702702
return 1;
703703
}
704704

705+
// set_breath(self, breath)
706+
int ObjectRef::l_set_breath(lua_State *L)
707+
{
708+
NO_MAP_LOCK_REQUIRED;
709+
ObjectRef *ref = checkobject(L, 1);
710+
PlayerSAO* co = getplayersao(ref);
711+
if(co == NULL) return 0;
712+
u16 breath = luaL_checknumber(L, 2);
713+
// Do it
714+
co->setBreath(breath);
715+
co->m_breath_not_sent = true;
716+
return 0;
717+
}
718+
719+
// get_breath(self)
720+
int ObjectRef::l_get_breath(lua_State *L)
721+
{
722+
NO_MAP_LOCK_REQUIRED;
723+
ObjectRef *ref = checkobject(L, 1);
724+
PlayerSAO* co = getplayersao(ref);
725+
if(co == NULL) return 0;
726+
// Do it
727+
u16 breath = co->getBreath();
728+
lua_pushinteger (L, breath);
729+
return 1;
730+
}
731+
705732
// set_inventory_formspec(self, formspec)
706733
int ObjectRef::l_set_inventory_formspec(lua_State *L)
707734
{
@@ -1098,6 +1125,8 @@ const luaL_reg ObjectRef::methods[] = {
10981125
luamethod(ObjectRef, get_look_yaw),
10991126
luamethod(ObjectRef, set_look_yaw),
11001127
luamethod(ObjectRef, set_look_pitch),
1128+
luamethod(ObjectRef, get_breath),
1129+
luamethod(ObjectRef, set_breath),
11011130
luamethod(ObjectRef, set_inventory_formspec),
11021131
luamethod(ObjectRef, get_inventory_formspec),
11031132
luamethod(ObjectRef, get_player_control),

‎src/script/lua_api/l_object.h

+6
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,12 @@ class ObjectRef
179179
// set_look_yaw(self, radians)
180180
static int l_set_look_yaw(lua_State *L);
181181

182+
// set_breath(self, breath)
183+
static int l_set_breath(lua_State *L);
184+
185+
// get_breath(self, breath)
186+
static int l_get_breath(lua_State *L);
187+
182188
// set_inventory_formspec(self, formspec)
183189
static int l_set_inventory_formspec(lua_State *L);
184190

0 commit comments

Comments
 (0)
Please sign in to comment.