]> git.lizzy.rs Git - minetest.git/blob - src/player.cpp
Add shutdown hook interface to Lua API
[minetest.git] / src / player.cpp
1 /*
2 Minetest-c55
3 Copyright (C) 2010-2011 celeron55, Perttu Ahola <celeron55@gmail.com>
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published by
7 the Free Software Foundation; either version 2.1 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 GNU Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20 #include "player.h"
21 #include "constants.h"
22 #include "gamedef.h"
23 #include "connection.h" // PEER_ID_INEXISTENT
24 #include "settings.h"
25 #include "content_sao.h"
26 #include "util/numeric.h"
27
28 Player::Player(IGameDef *gamedef):
29         touching_ground(false),
30         in_water(false),
31         in_water_stable(false),
32         is_climbing(false),
33         swimming_up(false),
34         camera_barely_in_ceiling(false),
35         inventory(gamedef->idef()),
36         hp(PLAYER_MAX_HP),
37         peer_id(PEER_ID_INEXISTENT),
38 // protected
39         m_gamedef(gamedef),
40         m_pitch(0),
41         m_yaw(0),
42         m_speed(0,0,0),
43         m_position(0,0,0)
44 {
45         updateName("<not set>");
46         inventory.clear();
47         inventory.addList("main", PLAYER_INVENTORY_SIZE);
48         InventoryList *craft = inventory.addList("craft", 9);
49         craft->setWidth(3);
50         inventory.addList("craftpreview", 1);
51         inventory.addList("craftresult", 1);
52
53         // Can be redefined via Lua
54         inventory_formspec =  "size[8,7.5]"
55                 //"image[1,0.6;1,2;player.png]"
56                 "list[current_player;main;0,3.5;8,4;]"
57                 "list[current_player;craft;3,0;3,3;]"
58                 "list[current_player;craftpreview;7,1;1,1;]";
59 }
60
61 Player::~Player()
62 {
63 }
64
65 // Y direction is ignored
66 void Player::accelerate(v3f target_speed, f32 max_increase)
67 {
68         v3f d_wanted = target_speed - m_speed;
69         d_wanted.Y = 0;
70         f32 dl_wanted = d_wanted.getLength();
71         f32 dl = dl_wanted;
72         if(dl > max_increase)
73                 dl = max_increase;
74         
75         v3f d = d_wanted.normalize() * dl;
76
77         m_speed.X += d.X;
78         m_speed.Z += d.Z;
79         //m_speed += d;
80
81 #if 0 // old code
82         if(m_speed.X < target_speed.X - max_increase)
83                 m_speed.X += max_increase;
84         else if(m_speed.X > target_speed.X + max_increase)
85                 m_speed.X -= max_increase;
86         else if(m_speed.X < target_speed.X)
87                 m_speed.X = target_speed.X;
88         else if(m_speed.X > target_speed.X)
89                 m_speed.X = target_speed.X;
90
91         if(m_speed.Z < target_speed.Z - max_increase)
92                 m_speed.Z += max_increase;
93         else if(m_speed.Z > target_speed.Z + max_increase)
94                 m_speed.Z -= max_increase;
95         else if(m_speed.Z < target_speed.Z)
96                 m_speed.Z = target_speed.Z;
97         else if(m_speed.Z > target_speed.Z)
98                 m_speed.Z = target_speed.Z;
99 #endif
100 }
101
102 v3s16 Player::getLightPosition() const
103 {
104         return floatToInt(m_position + v3f(0,BS+BS/2,0), BS);
105 }
106
107 void Player::serialize(std::ostream &os)
108 {
109         // Utilize a Settings object for storing values
110         Settings args;
111         args.setS32("version", 1);
112         args.set("name", m_name);
113         //args.set("password", m_password);
114         args.setFloat("pitch", m_pitch);
115         args.setFloat("yaw", m_yaw);
116         args.setV3F("position", m_position);
117         args.setS32("hp", hp);
118
119         args.writeLines(os);
120
121         os<<"PlayerArgsEnd\n";
122         
123         inventory.serialize(os);
124 }
125
126 void Player::deSerialize(std::istream &is)
127 {
128         Settings args;
129         
130         for(;;)
131         {
132                 if(is.eof())
133                         throw SerializationError
134                                         ("Player::deSerialize(): PlayerArgsEnd not found");
135                 std::string line;
136                 std::getline(is, line);
137                 std::string trimmedline = trim(line);
138                 if(trimmedline == "PlayerArgsEnd")
139                         break;
140                 args.parseConfigLine(line);
141         }
142
143         //args.getS32("version"); // Version field value not used
144         std::string name = args.get("name");
145         updateName(name.c_str());
146         setPitch(args.getFloat("pitch"));
147         setYaw(args.getFloat("yaw"));
148         setPosition(args.getV3F("position"));
149         try{
150                 hp = args.getS32("hp");
151         }catch(SettingNotFoundException &e){
152                 hp = 20;
153         }
154
155         inventory.deSerialize(is);
156
157         if(inventory.getList("craftpreview") == NULL)
158         {
159                 // Convert players without craftpreview
160                 inventory.addList("craftpreview", 1);
161
162                 bool craftresult_is_preview = true;
163                 if(args.exists("craftresult_is_preview"))
164                         craftresult_is_preview = args.getBool("craftresult_is_preview");
165                 if(craftresult_is_preview)
166                 {
167                         // Clear craftresult
168                         inventory.getList("craftresult")->changeItem(0, ItemStack());
169                 }
170         }
171 }
172
173 /*
174         RemotePlayer
175 */
176
177
178
179
180
181 void RemotePlayer::setPosition(const v3f &position)
182 {
183         Player::setPosition(position);
184         if(m_sao)
185                 m_sao->setBasePosition(position);
186 }