Allow defining player's inventory form in Lua
[oweals/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         inventory.addList("craft", 9);
49         inventory.addList("craftpreview", 1);
50         inventory.addList("craftresult", 1);
51
52         // Can be redefined via Lua
53         inventory_formspec =  "invsize[8,7.5;]"
54                 //"image[1,0.6;1,2;player.png]"
55                 "list[current_player;main;0,3.5;8,4;]"
56                 "list[current_player;craft;3,0;3,3;]"
57                 "list[current_player;craftpreview;7,1;1,1;]";
58 }
59
60 Player::~Player()
61 {
62 }
63
64 // Y direction is ignored
65 void Player::accelerate(v3f target_speed, f32 max_increase)
66 {
67         v3f d_wanted = target_speed - m_speed;
68         d_wanted.Y = 0;
69         f32 dl_wanted = d_wanted.getLength();
70         f32 dl = dl_wanted;
71         if(dl > max_increase)
72                 dl = max_increase;
73         
74         v3f d = d_wanted.normalize() * dl;
75
76         m_speed.X += d.X;
77         m_speed.Z += d.Z;
78         //m_speed += d;
79
80 #if 0 // old code
81         if(m_speed.X < target_speed.X - max_increase)
82                 m_speed.X += max_increase;
83         else if(m_speed.X > target_speed.X + max_increase)
84                 m_speed.X -= max_increase;
85         else if(m_speed.X < target_speed.X)
86                 m_speed.X = target_speed.X;
87         else if(m_speed.X > target_speed.X)
88                 m_speed.X = target_speed.X;
89
90         if(m_speed.Z < target_speed.Z - max_increase)
91                 m_speed.Z += max_increase;
92         else if(m_speed.Z > target_speed.Z + max_increase)
93                 m_speed.Z -= max_increase;
94         else if(m_speed.Z < target_speed.Z)
95                 m_speed.Z = target_speed.Z;
96         else if(m_speed.Z > target_speed.Z)
97                 m_speed.Z = target_speed.Z;
98 #endif
99 }
100
101 v3s16 Player::getLightPosition() const
102 {
103         return floatToInt(m_position + v3f(0,BS+BS/2,0), BS);
104 }
105
106 void Player::serialize(std::ostream &os)
107 {
108         // Utilize a Settings object for storing values
109         Settings args;
110         args.setS32("version", 1);
111         args.set("name", m_name);
112         //args.set("password", m_password);
113         args.setFloat("pitch", m_pitch);
114         args.setFloat("yaw", m_yaw);
115         args.setV3F("position", m_position);
116         args.setS32("hp", hp);
117
118         args.writeLines(os);
119
120         os<<"PlayerArgsEnd\n";
121         
122         inventory.serialize(os);
123 }
124
125 void Player::deSerialize(std::istream &is)
126 {
127         Settings args;
128         
129         for(;;)
130         {
131                 if(is.eof())
132                         throw SerializationError
133                                         ("Player::deSerialize(): PlayerArgsEnd not found");
134                 std::string line;
135                 std::getline(is, line);
136                 std::string trimmedline = trim(line);
137                 if(trimmedline == "PlayerArgsEnd")
138                         break;
139                 args.parseConfigLine(line);
140         }
141
142         //args.getS32("version"); // Version field value not used
143         std::string name = args.get("name");
144         updateName(name.c_str());
145         setPitch(args.getFloat("pitch"));
146         setYaw(args.getFloat("yaw"));
147         setPosition(args.getV3F("position"));
148         try{
149                 hp = args.getS32("hp");
150         }catch(SettingNotFoundException &e){
151                 hp = 20;
152         }
153
154         inventory.deSerialize(is);
155
156         if(inventory.getList("craftpreview") == NULL)
157         {
158                 // Convert players without craftpreview
159                 inventory.addList("craftpreview", 1);
160
161                 bool craftresult_is_preview = true;
162                 if(args.exists("craftresult_is_preview"))
163                         craftresult_is_preview = args.getBool("craftresult_is_preview");
164                 if(craftresult_is_preview)
165                 {
166                         // Clear craftresult
167                         inventory.getList("craftresult")->changeItem(0, ItemStack());
168                 }
169         }
170 }
171
172 /*
173         RemotePlayer
174 */
175
176
177
178
179
180 void RemotePlayer::setPosition(const v3f &position)
181 {
182         Player::setPosition(position);
183         if(m_sao)
184                 m_sao->setBasePosition(position);
185 }