Furnaces #193

Merged
merged 2 commits into from Apr 5, 2012
View
50 plugins/passiveMobs/passiveMobs.cpp
@@ -43,6 +43,7 @@
#define MINESERVER_C_API
#include "../../src/plugin_api.h"
+#include "../../src/tools.h"
#include "passiveMobs.h"
@@ -209,8 +210,9 @@ void timer200Function()
{
double x,y,z;
int w;
- mineserver->mob.getMobPositionW(MyMobs[i]->ID,&x,&y,&z,&w);
-
+ // get position and world
+ mineserver->mob.getMobPositionW(MyMobs[i]->ID, &x, &y, &z, &w);
+ // kill dead mobs
if(mineserver->mob.getHealth(MyMobs[i]->ID) == 0)
{
if (MyMobs[i]->deSpawn < 12)
@@ -229,7 +231,9 @@ void timer200Function()
{
MyMobs[i]->deSpawn=0;
}
+ // get the nearest user
int nearest = 10000;
+ double nearest_x = .0, nearest_y = .0, nearest_z = .0;
for (int j = 0; j < mineserver->user.getCount(); j++)
{
const char * const name = mineserver->user.getUserNumbered(j);
@@ -239,45 +243,57 @@ void timer200Function()
if (w != pmap) { continue; }
int distance = abs(int(px-x)) + abs(int(py-y)) + abs(int(pz-z));
- if(distance < nearest) { nearest = distance; }
+ if(distance < nearest) {
+ nearest = distance;
+ nearest_x = px;
+ nearest_y = py;
+ nearest_z = pz;
+ }
}
if(nearest < 0 || nearest > 200)
{
+ // if there is no user here, despawn the mob
mineserver->mob.despawnMob(MyMobs[i]->ID);
MyMobs.erase(MyMobs.begin()+i);
continue;
}
- int action = rand()%100;
- double yaw, pitch;
+ // do something, my little mob
+ int action = rand() % 150;
+ double yaw, pitch, head_yaw;
float forward = 0;
- mineserver->mob.getLook(MyMobs[i]->ID, &yaw, &pitch);
+ mineserver->mob.getLook(MyMobs[i]->ID, &yaw, &pitch, &head_yaw);
if (action < 5)
{
yaw += 30;
}
- else if (action <10)
+ else if (action < 10)
{
yaw += 15;
- while (yaw >= 360){ yaw-=360; }
forward = 0.3;
}
- else if (action <15)
+ else if (action < 15)
{
yaw -= 30;
}
- else if (action <20)
+ else if (action < 20)
{
yaw -= 15;
- while(yaw <= 0) { yaw += 360; }
forward = 0.3;
}
else if (action < 30)
{
- forward =+ 0.6;
+ forward = 0.6;
+ }
+ else if (action < 40)
+ {
+ // for now, just look around stupidly
+ head_yaw += rand() % 40;
}
else if (action < 50)
{
- forward =- 0.6;;
+ forward = -0.6;
+ // turn around!
+ yaw -= 180;
}
MyMobs[i]->velocity += forward;
if (MyMobs[i]->velocity > 2.0){ MyMobs[i]->velocity = 2.0; }
@@ -287,6 +303,12 @@ void timer200Function()
if (yaw <= 0) { yaw += 360; }
if (yaw >= 360) { yaw -= 360; }
+ // TODO: make it look at the player if he's near enough.
+ // (nearest_x, nearest_y, nearest_z).
+/* if(nearest_z != z) {
+ head_yaw = RADIANS_TO_DEGREES(tan((nearest_x - x) / (nearest_z - z)));
+ }*/
+
if (forward>0.1 && rand()%6 == 3)
{
float incz = cos((yaw*PI)/180)* forward;
@@ -300,7 +322,7 @@ void timer200Function()
}
fallMob(&x,&y,&z,w); // Even if they dont move, make them fall
}
- mineserver->mob.setLook(MyMobs[i]->ID, yaw, pitch);
+ mineserver->mob.setLook(MyMobs[i]->ID, yaw, pitch, head_yaw);
}
}
View
6 src/furnace.cpp
@@ -33,6 +33,7 @@
#include "map.h"
#include "tools.h"
#include "config.h"
+#include "protocol.h"
Creation createList[2258];
bool configIsRead = false;
@@ -286,8 +287,9 @@ void Furnace::sendToAllUsers()
{
if (m_data->items[j].getType() != -1)
{
- inv[openinv]->users[user]->buffer << (int8_t)PACKET_SET_SLOT << (int8_t)WINDOW_FURNACE << (int16_t)j << (int16_t)m_data->items[j].getType()
- << (int8_t)(m_data->items[j].getCount()) << (int16_t)m_data->items[j].getHealth();
+ Item& item = m_data->items[j];
+ inv[openinv]->users[user]->buffer << Protocol::setSlotHeader(WINDOW_FURNACE, j)
+ << Protocol::slot(item.getType(), item.getCount(), item.getHealth());
}
}
View
2 src/inventory.cpp
@@ -532,7 +532,7 @@ bool Inventory::windowClick(User* user, int8_t windowID, int16_t slot, int8_t ri
}
}
- Item* slotItem;
+ Item* slotItem = NULL;
furnaceDataPtr tempFurnace;
switch (windowID)
View
19 src/mob.cpp
@@ -170,7 +170,24 @@ void Mob::look(int16_t yaw, int16_t pitch)
pitch = pitch % 360;
int8_t y_byte = (int8_t)((yaw * 1.0) / 360.0 * 256.0);
int8_t p_byte = (int8_t)((pitch * 1.0) / 360.0 * 256.0);
+ if(y_byte != this->yaw || p_byte != this->pitch)
+ {
+ User::sendAll(Protocol::entityLook(UID, yaw, pitch));
+ }
this->pitch = p_byte;
this->yaw = y_byte;
- User::sendAll(Protocol::entityLook(UID, yaw, pitch));
+}
+
+void Mob::headLook(int16_t head_yaw)
+{
+ while(head_yaw < 0) {
+ head_yaw += 360;
+ }
+ head_yaw = head_yaw % 360;
+ int8_t h_byte = (int8_t)((head_yaw * 1.0) / 360.0 * 256.0);
+ if(h_byte != this->head_yaw)
+ {
+ User::sendAll(Protocol::entityHeadLook(UID, head_yaw));
+ }
+ this->head_yaw = h_byte;
}
View
1 src/mob.h
@@ -74,6 +74,7 @@ class Mob
void moveTo(double to_x, double to_y, double to_z, int to_map = -1);
void look(int16_t yaw, int16_t pitch);
+ void headLook(int16_t head_yaw);
};
typedef std::tr1::shared_ptr<Mob> MobPtr;
View
6 src/packets.cpp
@@ -59,12 +59,6 @@
#include "mob.h"
#include "utf8.h"
-#ifdef WIN32
-#define M_PI 3.141592653589793238462643
-#endif
-#define DEGREES_TO_RADIANS(x) ((x) / 180.0 * M_PI)
-#define RADIANS_TO_DEGREES(x) ((x) / M_PI * 180.0)
-
void PacketHandler::init()
{
packets[PACKET_KEEP_ALIVE] = Packets(0, &PacketHandler::keep_alive);
View
1 src/packets.h
@@ -89,6 +89,7 @@ enum
PACKET_ENTITY_LOOK = 0x20,
PACKET_ENTITY_LOOK_RELATIVE_MOVE = 0x21,
PACKET_ENTITY_TELEPORT = 0x22,
+ PACKET_ENTITY_HEAD_LOOK = 0x23,
PACKET_DEATH_ANIMATION = 0x26,
PACKET_ENTITY_METADATA = 0x28,
PACKET_REMOVE_ENTITY_EFFECT = 0x2a,
View
9 src/plugin_api.cpp
@@ -838,7 +838,7 @@ int mob_getType(size_t uid)
return m->type;
}
-bool mob_getLook(int uid, double* rot, double* pitch)
+bool mob_getLook(int uid, double* rot, double* pitch, double* head_yaw)
{
MobPtr m = Mineserver::get()->mobs()->getMobByID(uid);
if (m != NULL)
@@ -851,17 +851,22 @@ bool mob_getLook(int uid, double* rot, double* pitch)
{
*pitch = (double)((m->pitch * 1.0) * 360.0 / 256.0);
}
+ if (head_yaw != NULL)
+ {
+ *head_yaw = (double)((m->head_yaw * 1.0) * 360.0 / 256.0);
+ }
return true;
}
return false;
}
-bool mob_setLook(int uid, double rot, double pitch)
+bool mob_setLook(int uid, double rot, double pitch, double head_yaw)
{
MobPtr m = Mineserver::get()->mobs()->getMobByID(uid);
if (m != NULL)
{
m->look((int16_t)rot, (int16_t)pitch);
+ m->headLook((int16_t)head_yaw);
return true;
}
return false;
View
4 src/plugin_api.h
@@ -171,8 +171,8 @@ struct mob_pointer_struct
int (*getHealth)(int uid);
void (*setHealth)(int uid, int mobHealth);
int (*getType)(size_t uid);
- bool (*getLook)(int uid, double* yaw, double* pitch);
- bool (*setLook)(int uid, double yaw, double pitch);
+ bool (*getLook)(int uid, double* yaw, double* pitch, double *head_yaw);
+ bool (*setLook)(int uid, double yaw, double pitch, double head_yaw);
void (*moveAnimal)(const char* userIn, size_t mobID);
void (*animateMob)(const char* userIn, size_t mobID, int animID);
void (*animateDamage)(const char* userIn, size_t mobID, int animID);
View
9 src/protocol.h
@@ -125,13 +125,20 @@ class Protocol
return ret;
}
+ static Packet entityHeadLook(int eid, int head_yaw)
+ {
+ Packet ret;
+ ret << (int8_t)PACKET_ENTITY_HEAD_LOOK << eid << (int8_t)head_yaw;
+ return ret;
+ }
+
static Packet entityLook(int eid, int yaw, int pitch)
{
Packet ret;
ret << (int8_t)PACKET_ENTITY_LOOK << (int32_t)eid << (int8_t)yaw << (int8_t)pitch;
return ret;
}
-
+
static Packet entityLook(int eid, double yaw, double pitch)
{
return entityLook(eid, angleToByte(yaw), angleToByte(pitch));
View
6 src/tools.h
@@ -39,6 +39,12 @@
#include <arpa/inet.h>
#endif
+#ifdef WIN32
+#define M_PI 3.141592653589793238462643
+#endif
+#define DEGREES_TO_RADIANS(x) ((x) / 180.0 * M_PI)
+#define RADIANS_TO_DEGREES(x) ((x) / M_PI * 180.0)
+
void putSint64(uint8_t* buf, int64_t value);
void putSint32(uint8_t* buf, int32_t value);
void putSint16(uint8_t* buf, short value);