summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/being.cpp9
-rw-r--r--src/being.h7
-rw-r--r--src/client.cpp68
-rw-r--r--src/gamehandler.cpp38
-rw-r--r--src/main.cpp2
-rw-r--r--src/state.cpp13
6 files changed, 99 insertions, 38 deletions
diff --git a/src/being.cpp b/src/being.cpp
index 6c97fa98..be0b312d 100644
--- a/src/being.cpp
+++ b/src/being.cpp
@@ -310,6 +310,15 @@ bool Being::delInventory(unsigned int itemId)
return false;
}
+bool Being::hasItem(unsigned int itemId) {
+ for (std::vector<unsigned int>::iterator i = inventory.begin();
+ i != inventory.end(); i++) {
+ if (*i == itemId)
+ return true;
+ }
+ return false;
+}
+
bool Being::equip(unsigned int itemId, unsigned char slot)
{
// currently this is too simplistic and doesn't check enough
diff --git a/src/being.h b/src/being.h
index b8e53010..cb1e5456 100644
--- a/src/being.h
+++ b/src/being.h
@@ -291,6 +291,13 @@ class Being: public Object
bool delInventory(unsigned int itemId);
/**
+ * Check if character has an item
+ *
+ * @return true if being has item, false otherwise
+ */
+ bool hasItem(unsigned int itemId);
+
+ /**
* Equip item with ID in equipment slot
*
* @return Equip success/failure
diff --git a/src/client.cpp b/src/client.cpp
index 7669b112..c3870b49 100644
--- a/src/client.cpp
+++ b/src/client.cpp
@@ -5,11 +5,9 @@
#include "defines.h"
#include "messageout.h"
-int main(int argc, char *argv[])
-{
+int main(int argc, char *argv[]) {
// Initialize SDL
- if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER) == -1)
- {
+ if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER) == -1) {
printf("SDL_Init: %s\n", SDL_GetError());
exit(1);
}
@@ -18,8 +16,7 @@ int main(int argc, char *argv[])
atexit(SDL_Quit);
// Initialize SDL_net
- if (SDLNet_Init() == -1)
- {
+ if (SDLNet_Init() == -1) {
printf("SDLNet_Init: %s\n", SDLNet_GetError());
exit(2);
}
@@ -28,15 +25,13 @@ int main(int argc, char *argv[])
IPaddress ip;
TCPsocket tcpsock;
- if (SDLNet_ResolveHost(&ip, "localhost", 9601) == -1)
- {
+ if (SDLNet_ResolveHost(&ip, "localhost", 9601) == -1) {
printf("SDLNet_ResolveHost: %s\n", SDLNet_GetError());
exit(1);
}
tcpsock = SDLNet_TCP_Open(&ip);
- if (!tcpsock)
- {
+ if (!tcpsock) {
printf("SDLNet_TCP_Open: %s\n", SDLNet_GetError());
exit(2);
}
@@ -46,9 +41,8 @@ int main(int argc, char *argv[])
int answer = 1;
char line[256] = "";
- while (answer != 0)
- {
-
+ while (answer != 0) {
+ bool responseRequired = true;
MessageOut msg;
printf ("0) Quit\n");
@@ -57,6 +51,8 @@ int main(int argc, char *argv[])
printf ("3) Chat\n");
printf ("4) Create character\n");
printf ("5) Character selection\n");
+ printf ("6) Move character\n");
+ printf ("7) Equip item\n");
printf ("Choose your option: ");
std::cin >> answer;
@@ -74,6 +70,7 @@ int main(int argc, char *argv[])
std::cin >> line;
msg.writeString(line);
break;
+
case 2:
// Login
msg.writeShort(CMSG_LOGIN);
@@ -84,6 +81,7 @@ int main(int argc, char *argv[])
std::cin >> line;
msg.writeString(line);
break;
+
case 3:
// Chat
msg.writeShort(CMSG_SAY);
@@ -91,7 +89,9 @@ int main(int argc, char *argv[])
std::cin >> line;
msg.writeString(line);
msg.writeShort(0);
+ responseRequired = false;
break;
+
case 4:
{
// Create character
@@ -111,6 +111,36 @@ int main(int argc, char *argv[])
msg.writeByte(atoi(line));
} break;
+ case 6:
+ {
+ // Move character
+ long x, y;
+ std::cout << "X: ";
+ std::cin >> x;
+ std::cout << "Y: ";
+ std::cin >> y;
+
+ msg.writeShort(CMSG_WALK);
+ msg.writeLong(x);
+ msg.writeLong(y);
+
+ responseRequired = false;
+ } break;
+
+ case 7:
+ {
+ // Equip
+ unsigned int itemId;
+ unsigned int slot;
+ std::cout << "Item ID: ";
+ std::cin >> itemId;
+ std::cout << "Slot: ";
+ std::cin >> slot;
+ msg.writeShort(CMSG_EQUIP);
+ msg.writeLong(itemId);
+ msg.writeByte(slot);
+ } break;
+
default:
continue;
}
@@ -121,21 +151,19 @@ int main(int argc, char *argv[])
printf("%x ", msg.getPacket()->data[i]);
}
printf("\n\n");
-
+
SDLNet_TCP_Send(tcpsock, msg.getPacket()->data,
msg.getPacket()->length);
-
- if (answer != 3) {
+
+ if (responseRequired) {
char data[1024];
int recvLength = SDLNet_TCP_Recv(tcpsock, data, 1024);
printf("Received:\n");
- if (recvLength != -1)
- {
+ if (recvLength != -1) {
for (unsigned int i = 0; i < recvLength; i++) {
printf("%x ", data[i]);
}
- }
- else {
+ } else {
printf("ERROR!");
}
printf("\n\n");
diff --git a/src/gamehandler.cpp b/src/gamehandler.cpp
index cfeda536..7d525b5f 100644
--- a/src/gamehandler.cpp
+++ b/src/gamehandler.cpp
@@ -46,28 +46,33 @@ void GameHandler::receiveMessage(NetComputer &computer, MessageIn &message)
result.writeShort(SMSG_PICKUP_RESPONSE);
result.writeByte(PICKUP_OK);
} break;
-
+
+ case CMSG_USE_ITEM:
case CMSG_USE_OBJECT:
{
unsigned int itemId = message.readLong();
- // use item
- // this should execute a script which will do the appropriate action
-
- // respond
result.writeShort(SMSG_USE_RESPONSE);
- result.writeByte(USE_OK);
+
+ if (computer.getCharacter()->hasItem(itemId)) {
+ // use item
+ // this should execute a script which will do the appropriate action
+ // (the script will determine if the item is 1 use only)
+ result.writeByte(USE_OK);
+ } else {
+ result.writeByte(USE_FAIL);
+ }
} break;
case CMSG_TARGET:
{
-
+ // nothing at the moment
} break;
case CMSG_WALK:
{
- int x = message.readLong();
- int y = message.readLong();
+ long x = message.readLong();
+ long y = message.readLong();
// simplistic "teleport" walk
computer.getCharacter()->setX(x);
@@ -77,16 +82,19 @@ void GameHandler::receiveMessage(NetComputer &computer, MessageIn &message)
} break;
case CMSG_START_TRADE:
- break;
+ {
+ // nothing at the moment
+ } break;
case CMSG_START_TALK:
- break;
+ {
+ // nothing at the moment
+ } break;
case CMSG_REQ_TRADE:
- break;
-
- case CMSG_USE_ITEM:
- break;
+ {
+ // nothing at the moment
+ } break;
case CMSG_EQUIP:
{
diff --git a/src/main.cpp b/src/main.cpp
index 337d7cd2..bb6a0e96 100644
--- a/src/main.cpp
+++ b/src/main.cpp
@@ -227,12 +227,12 @@ int main(int argc, char *argv[])
connectionHandler->registerHandler(CMSG_PICKUP, gameHandler);
connectionHandler->registerHandler(CMSG_USE_OBJECT, gameHandler);
+ connectionHandler->registerHandler(CMSG_USE_ITEM, gameHandler); // NOTE: this is probably redundant (CMSG_USE_OBJECT)
connectionHandler->registerHandler(CMSG_TARGET, gameHandler);
connectionHandler->registerHandler(CMSG_WALK, gameHandler);
connectionHandler->registerHandler(CMSG_START_TRADE, gameHandler);
connectionHandler->registerHandler(CMSG_START_TALK, gameHandler);
connectionHandler->registerHandler(CMSG_REQ_TRADE, gameHandler);
- connectionHandler->registerHandler(CMSG_USE_ITEM, gameHandler); // NOTE: this is probably redundant (CMSG_USE_OBJECT)
connectionHandler->registerHandler(CMSG_EQUIP, gameHandler);
session->startListen(connectionHandler.get(), SERVER_PORT);
diff --git a/src/state.cpp b/src/state.cpp
index 72448eab..a169e4c3 100644
--- a/src/state.cpp
+++ b/src/state.cpp
@@ -31,6 +31,15 @@ namespace tmwserv
void State::update(ConnectionHandler &connectionHandler)
{
// update game state (update AI, etc.)
+ for (std::map<std::string, Beings>::iterator i = beings.begin();
+ i != beings.end();
+ i++) {
+ for (Beings::iterator b = i->second.begin();
+ b != i->second.end();
+ b++) {
+ b->get()->update();
+ }
+ }
// notify clients about changes in the game world (only on their maps)
// NOTE: This isn't finished ;)
@@ -38,11 +47,11 @@ void State::update(ConnectionHandler &connectionHandler)
i != beings.end();
i++) {
//
- for (std::vector<BeingPtr>::iterator b = i->second.begin();
+ for (Beings::iterator b = i->second.begin();
b != i->second.end();
b++) {
// send info about other players
- for (std::vector<BeingPtr>::iterator b2 = i->second.begin();
+ for (Beings::iterator b2 = i->second.begin();
b2 != i->second.end();
b2++) {
if (b != b2) {