Advertisement
Guest User

Untitled

a guest
Jan 8th, 2012
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.02 KB | None | 0 0
  1. diff -Naur -x '*.o' -x .git -x CMakeFiles -x lua -x sqlite -x '*.s' -x cmake_install.cmake -x jthread -x Makefile ../git/minetest.modif/src/content_cao_lua.cpp src/content_cao_lua.cpp
  2. --- ../git/minetest.modif/src/content_cao_lua.cpp 2012-01-09 01:35:04.000000000 +0100
  3. +++ src/content_cao_lua.cpp 2012-01-09 02:25:56.000000000 +0100
  4. @@ -43,6 +43,8 @@
  5. {
  6. if(gamedef == NULL)
  7. ClientActiveObject::registerType(LuaEntityCAO::getType(), LuaEntityCAO::create);
  8. +
  9. + this->m_Parent = NULL;
  10. }
  11.  
  12. void LuaEntityCAO::initialize(const std::string &data)
  13. @@ -177,33 +179,45 @@
  14.  
  15. void LuaEntityCAO::step(float dtime, ClientEnvironment *env)
  16. {
  17. - if(m_prop->physical){
  18. - core::aabbox3d<f32> box = m_prop->collisionbox;
  19. - box.MinEdge *= BS;
  20. - box.MaxEdge *= BS;
  21. - collisionMoveResult moveresult;
  22. - f32 pos_max_d = BS*0.25; // Distance per iteration
  23. - v3f p_pos = m_position;
  24. - v3f p_velocity = m_velocity;
  25. - IGameDef *gamedef = env->getGameDef();
  26. - moveresult = collisionMovePrecise(&env->getMap(), gamedef,
  27. - pos_max_d, box, dtime, p_pos, p_velocity);
  28. - // Apply results
  29. - m_position = p_pos;
  30. - m_velocity = p_velocity;
  31. -
  32. - bool is_end_position = moveresult.collides;
  33. - pos_translator.update(m_position, is_end_position, dtime);
  34. - pos_translator.translate(dtime);
  35. - updateNodePos();
  36. + //if liked movement is handled by parent entity
  37. + if(this->m_Parent == NULL) {
  38. + if(m_prop->physical){
  39. + core::aabbox3d<f32> box = m_prop->collisionbox;
  40. + box.MinEdge *= BS;
  41. + box.MaxEdge *= BS;
  42. + collisionMoveResult moveresult;
  43. + f32 pos_max_d = BS*0.25; // Distance per iteration
  44. + v3f p_pos = m_position;
  45. + v3f p_velocity = m_velocity;
  46. + IGameDef *gamedef = env->getGameDef();
  47. + moveresult = collisionMovePrecise(&env->getMap(), gamedef,
  48. + pos_max_d, box, dtime, p_pos, p_velocity);
  49. + // Apply results
  50. + m_position = p_pos;
  51. + m_velocity = p_velocity;
  52. +
  53. + bool is_end_position = moveresult.collides;
  54. + pos_translator.update(m_position, is_end_position, dtime);
  55. + pos_translator.translate(dtime);
  56. + updateNodePos();
  57.  
  58. - m_velocity += dtime * m_acceleration;
  59. - } else {
  60. - m_position += dtime * m_velocity + 0.5 * dtime * dtime * m_acceleration;
  61. - m_velocity += dtime * m_acceleration;
  62. - pos_translator.update(m_position, pos_translator.aim_is_end, pos_translator.anim_time);
  63. - pos_translator.translate(dtime);
  64. - updateNodePos();
  65. + m_velocity += dtime * m_acceleration;
  66. + } else {
  67. + m_position += dtime * m_velocity + 0.5 * dtime * dtime * m_acceleration;
  68. + m_velocity += dtime * m_acceleration;
  69. + pos_translator.update(m_position, pos_translator.aim_is_end, pos_translator.anim_time);
  70. + pos_translator.translate(dtime);
  71. + updateNodePos();
  72. + }
  73. + }
  74. +
  75. + for(std::list<ClientActiveObject*>::iterator i = this->m_LinkedObjects.begin();
  76. + i != this->m_LinkedObjects.end(); i++) {
  77. + LuaEntityCAO* toupdate = dynamic_cast<LuaEntityCAO*>(*i);
  78. +
  79. + if (toupdate != NULL) {
  80. + toupdate->setPosition(this->m_position,dtime);
  81. + }
  82. }
  83.  
  84. m_anim_timer += dtime;
  85. @@ -352,4 +366,92 @@
  86.  
  87. updateTexturePos();
  88. }
  89. + else if(cmd == AO_Message_type::Link) // Link entity
  90. + {
  91. + //Object to link entity to
  92. + u16 object_id = readU16(is);
  93. + //offset against linked object
  94. + v3f offset = readV3F1000(is);
  95. +
  96. + ClientActiveObject* parent = m_env->getActiveObject(object_id);
  97. +
  98. + if (parent != NULL) {
  99. + this->linkEntity(offset,parent);
  100. + }
  101. + else {
  102. + errorstream << "Invalid object to link to!" << std::endl;
  103. + }
  104. +
  105. + }
  106. + else if(cmd == AO_Message_type::UnLink) // UnLink entity
  107. + {
  108. + if (this->m_Parent == NULL) {
  109. + errorstream << "Unlinking object not linked!" << std::endl;
  110. + }
  111. +
  112. + this->unlinkEntity();
  113. + }
  114. + }
  115. +
  116. +bool LuaEntityCAO::linkEntity(v3f offset, ClientActiveObject* parent) {
  117. +
  118. +
  119. + //already linked unlink first
  120. + if (this->m_Parent != NULL) {
  121. + return false;
  122. + }
  123. +
  124. + //TODO add linkchain support
  125. + if (this->m_LinkedObjects.size() > 0) {
  126. + return false;
  127. + }
  128. +
  129. + LuaEntityCAO * parent_lua = dynamic_cast<LuaEntityCAO*>(parent);
  130. +
  131. + if (parent_lua == NULL) {
  132. + return false;
  133. + }
  134. +
  135. + parent_lua->link(this);
  136. + this->m_linkOffset = offset;
  137. + this->m_Parent = parent;
  138. + return true;
  139. +}
  140. +
  141. +bool LuaEntityCAO::unlinkEntity() {
  142. + if (this->m_Parent != NULL) {
  143. +
  144. + //tell parent we do unlink
  145. + LuaEntityCAO * parent = dynamic_cast<LuaEntityCAO*>(this->m_Parent);
  146. +
  147. + if (parent != NULL) {
  148. + parent->unlink(this);
  149. + this->m_Parent = NULL;
  150. + return true;
  151. + }
  152. }
  153. +
  154. + return false;
  155. +}
  156. +
  157. +void LuaEntityCAO::link(ClientActiveObject* entity) {
  158. +
  159. + //TODO check if entity is already linkt (shouldn't be the case but just to be sure)
  160. + this->m_LinkedObjects.push_back(entity);
  161. +}
  162. +
  163. +void LuaEntityCAO::unlink(ClientActiveObject* entity) {
  164. + this->m_LinkedObjects.remove(entity);
  165. +}
  166. +
  167. +void LuaEntityCAO::setPosition(v3f toset, float dtime){
  168. +
  169. + if (this->m_Parent != NULL) {
  170. + this->m_position = toset + this->m_linkOffset;
  171. +
  172. + pos_translator.update(m_position, pos_translator.aim_is_end, pos_translator.anim_time);
  173. + pos_translator.translate(dtime);
  174. + updateNodePos();
  175. + }
  176. +
  177. +}
  178. diff -Naur -x '*.o' -x .git -x CMakeFiles -x lua -x sqlite -x '*.s' -x cmake_install.cmake -x jthread -x Makefile ../git/minetest.modif/src/content_cao_lua.h src/content_cao_lua.h
  179. --- ../git/minetest.modif/src/content_cao_lua.h 2012-01-09 01:34:56.000000000 +0100
  180. +++ src/content_cao_lua.h 2012-01-08 22:41:06.000000000 +0100
  181. @@ -20,6 +20,7 @@
  182. #ifndef CONTENT_COA_LUA_H_
  183. #define CONTENT_COA_LUA_H_
  184.  
  185. +#include <list>
  186. #include "content_cao.h"
  187. #include "luaentity_common.h"
  188.  
  189. @@ -44,6 +45,11 @@
  190. float m_anim_framelength;
  191. float m_anim_timer;
  192.  
  193. + ClientActiveObject* m_Parent;
  194. + v3f m_linkOffset;
  195. +
  196. + std::list<ClientActiveObject*> m_LinkedObjects;
  197. +
  198. public:
  199. LuaEntityCAO(IGameDef *gamedef, ClientEnvironment *env);
  200.  
  201. @@ -61,6 +67,8 @@
  202. inline v3f getPosition()
  203. { return pos_translator.vect_show; }
  204.  
  205. + void setPosition(v3f toset, float dtime);
  206. +
  207. void addToScene(scene::ISceneManager *smgr, ITextureSource *tsrc,
  208. IrrlichtDevice *irr);
  209.  
  210. @@ -79,6 +87,14 @@
  211. void updateTextures(const std::string &mod);
  212.  
  213. void processMessage(const std::string &data);
  214. +
  215. + //user driven functions (exported by lua)
  216. + bool linkEntity(v3f offset, ClientActiveObject* parent);
  217. + bool unlinkEntity();
  218. +
  219. + //internal communication between entitys NOT to be used by user
  220. + void link(ClientActiveObject* entity);
  221. + void unlink(ClientActiveObject* entity);
  222. };
  223.  
  224.  
  225. diff -Naur -x '*.o' -x .git -x CMakeFiles -x lua -x sqlite -x '*.s' -x cmake_install.cmake -x jthread -x Makefile ../git/minetest.modif/src/content_sao_lua.cpp src/content_sao_lua.cpp
  226. --- ../git/minetest.modif/src/content_sao_lua.cpp 2012-01-09 01:35:04.000000000 +0100
  227. +++ src/content_sao_lua.cpp 2012-01-09 02:26:31.000000000 +0100
  228. @@ -37,7 +37,8 @@
  229. m_last_sent_position(0,0,0),
  230. m_last_sent_velocity(0,0,0),
  231. m_last_sent_position_timer(0),
  232. - m_last_sent_move_precision(0)
  233. + m_last_sent_move_precision(0),
  234. + m_Linked(false)
  235. {
  236. // Only register type if no environment supplied
  237. if(env == NULL){
  238. @@ -277,6 +278,10 @@
  239.  
  240. void LuaEntitySAO::sendPosition(bool do_interpolate, bool is_movement_end)
  241. {
  242. + //don't send position updates if linked
  243. + if (this->m_Linked) {
  244. + return;
  245. + }
  246. m_last_sent_move_precision = m_base_position.getDistanceFrom(
  247. m_last_sent_position);
  248. m_last_sent_position_timer = 0;
  249. @@ -311,3 +316,36 @@
  250. m_messages_out.push_back(aom);
  251. }
  252.  
  253. +bool LuaEntitySAO::linkEntity(ServerActiveObject* parent,v3f offset) {
  254. + //check if entity is in correct state
  255. + if (this->m_Linked == true) {
  256. + return false;
  257. + }
  258. +
  259. + this->m_Linked = true;
  260. + std::ostringstream os(std::ios::binary);
  261. + writeU8(os, AO_Message_type::Link);
  262. + // parameters
  263. + writeU16(os, parent->getId());
  264. + writeV3F1000(os, offset);
  265. + // create message and add to list
  266. + ActiveObjectMessage aom(getId(), false, os.str());
  267. + m_messages_out.push_back(aom);
  268. + return true;
  269. +}
  270. +
  271. +bool LuaEntitySAO::unlinkEntity() {
  272. + //check if entity is in correct state
  273. + if (this->m_Linked == false) {
  274. + return false;
  275. + }
  276. +
  277. + this->m_Linked = false;
  278. + std::ostringstream os(std::ios::binary);
  279. + writeU8(os, AO_Message_type::UnLink);
  280. + // create message and add to list
  281. + ActiveObjectMessage aom(getId(), false, os.str());
  282. + m_messages_out.push_back(aom);
  283. + return true;
  284. +}
  285. +
  286. diff -Naur -x '*.o' -x .git -x CMakeFiles -x lua -x sqlite -x '*.s' -x cmake_install.cmake -x jthread -x Makefile ../git/minetest.modif/src/content_sao_lua.h src/content_sao_lua.h
  287. --- ../git/minetest.modif/src/content_sao_lua.h 2012-01-09 01:34:56.000000000 +0100
  288. +++ src/content_sao_lua.h 2012-01-08 22:12:16.000000000 +0100
  289. @@ -54,6 +54,9 @@
  290. void setSprite(v2s16 p, int num_frames, float framelength,
  291. bool select_horiz_by_yawpitch);
  292. std::string getName();
  293. +
  294. + bool linkEntity(ServerActiveObject* parent,v3f offset);
  295. + bool unlinkEntity();
  296. private:
  297. void sendPosition(bool do_interpolate, bool is_movement_end);
  298.  
  299. @@ -70,6 +73,8 @@
  300. v3f m_last_sent_velocity;
  301. float m_last_sent_position_timer;
  302. float m_last_sent_move_precision;
  303. +
  304. + bool m_Linked;
  305. };
  306.  
  307.  
  308. diff -Naur -x '*.o' -x .git -x CMakeFiles -x lua -x sqlite -x '*.s' -x cmake_install.cmake -x jthread -x Makefile ../git/minetest.modif/src/scriptapi.cpp src/scriptapi.cpp
  309. --- ../git/minetest.modif/src/scriptapi.cpp 2012-01-09 01:34:56.000000000 +0100
  310. +++ src/scriptapi.cpp 2012-01-09 02:15:46.000000000 +0100
  311. @@ -2176,6 +2176,58 @@
  312. return 1;
  313. }
  314.  
  315. + // link(parent, offset)
  316. + static int l_link(lua_State *L)
  317. + {
  318. + ObjectRef *ref_child = checkobject(L, 1);
  319. + ObjectRef *ref_parent = checkobject(L, 2);
  320. + v3f offset = checkFloatPos(L, 3);
  321. + std::cout << "parameters read" << std::endl;
  322. +
  323. + ServerActiveObject *child = getobject(ref_child);
  324. + ServerActiveObject *parent = getobject(ref_parent);
  325. +
  326. + if (child == NULL) return 0;
  327. + if (parent == NULL) return 0;
  328. +
  329. +
  330. + LuaEntitySAO* child_lua = dynamic_cast<LuaEntitySAO*>(child);
  331. + LuaEntitySAO* parent_lua = dynamic_cast<LuaEntitySAO*>(parent);
  332. +
  333. + if (child_lua == NULL) return 0;
  334. + if (parent_lua == NULL) return 0;
  335. +
  336. + if (child_lua->linkEntity(parent_lua,offset)) {
  337. + lua_pushboolean(L, true);
  338. + return 1;
  339. + }
  340. + else {
  341. + return 0;
  342. + }
  343. + }
  344. +
  345. + // unlink()
  346. + static int l_unlink(lua_State *L)
  347. + {
  348. + ObjectRef *ref = checkobject(L, 1);
  349. +
  350. + ServerActiveObject *obj = getobject(ref);
  351. +
  352. + if (obj == NULL) return 0;
  353. +
  354. + LuaEntitySAO* tolink = dynamic_cast<LuaEntitySAO*>(obj);
  355. +
  356. + if (tolink == NULL) return 0;
  357. +
  358. + if (tolink->unlinkEntity()) {
  359. + lua_pushboolean(L, true);
  360. + return 1;
  361. + }
  362. + else {
  363. + return 0;
  364. + }
  365. + }
  366. +
  367. public:
  368. ObjectRef(ServerActiveObject *object):
  369. m_object(object)
  370. @@ -2273,6 +2325,8 @@
  371. method(ObjectRef, get_look_dir),
  372. method(ObjectRef, get_look_pitch),
  373. method(ObjectRef, get_look_yaw),
  374. + method(ObjectRef, link),
  375. + method(ObjectRef, unlink),
  376. {0,0}
  377. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement