/*
* The Mana World
* Copyright 2004 The Mana World Development Team
*
* This file is part of The Mana World.
*
* The Mana World is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* any later version.
*
* The Mana World is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with The Mana World; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* $Id$
*/
#include "game.h"
#include <fstream>
#include <physfs.h>
#include <sstream>
#include <string>
#include <guichan/sdl/sdlinput.hpp>
#include "beingmanager.h"
#include "configuration.h"
#include "engine.h"
#include "flooritemmanager.h"
#include "graphics.h"
#include "joystick.h"
#include "localplayer.h"
#include "log.h"
#include "npc.h"
#include "gui/buy.h"
#include "gui/buysell.h"
#include "gui/chargedialog.h"
#include "gui/chat.h"
#include "gui/confirm_dialog.h"
#include "gui/equipmentwindow.h"
#include "gui/help.h"
#include "gui/inventorywindow.h"
#include "gui/minimap.h"
#include "gui/npclistdialog.h"
#include "gui/npc_text.h"
#include "gui/sell.h"
#include "gui/setup.h"
#include "gui/skill.h"
#include "gui/menuwindow.h"
#include "gui/status.h"
#include "gui/ministatus.h"
#include "gui/trade.h"
#include "gui/debugwindow.h"
#include "net/beinghandler.h"
#include "net/buysellhandler.h"
#include "net/chathandler.h"
#include "net/equipmenthandler.h"
#include "net/inventoryhandler.h"
#include "net/itemhandler.h"
#include "net/network.h"
#include "net/npchandler.h"
#include "net/playerhandler.h"
#include "net/skillhandler.h"
#include "net/tradehandler.h"
#include "resources/imagewriter.h"
extern Graphics *graphics;
extern gcn::SDLInput *guiInput;
class Map;
std::string map_path;
bool done = false;
volatile int tick_time;
volatile int fps = 0, frame = 0;
Engine *engine = NULL;
Joystick *joystick;
extern Window *weightNotice;
extern Window *deathNotice;
ConfirmDialog *exitConfirm = NULL;
ChatWindow *chatWindow;
MenuWindow *menuWindow;
StatusWindow *statusWindow;
MiniStatusWindow *miniStatusWindow;
BuyDialog *buyDialog;
SellDialog *sellDialog;
BuySellDialog *buySellDialog;
InventoryWindow *inventoryWindow;
NpcListDialog *npcListDialog;
NpcTextDialog *npcTextDialog;
SkillDialog *skillDialog;
//NewSkillDialog *newSkillWindow;
Setup* setupWindow;
Minimap *minimap;
EquipmentWindow *equipmentWindow;
ChargeDialog *chargeDialog;
TradeWindow *tradeWindow;
//BuddyWindow *buddyWindow;
HelpWindow *helpWindow;
DebugWindow *debugWindow;
BeingManager *beingManager = NULL;
FloorItemManager *floorItemManager = NULL;
const int MAX_TIME = 10000;
/**
* Listener used for exitting handling.
*/
namespace {
struct ExitListener : public gcn::ActionListener {
void action(const std::string &eventId, gcn::Widget *widget) {
if (eventId == "yes") {
done = true;
}
exitConfirm = NULL;
}
} exitListener;
}
/**
* Advances game logic counter.
*/
Uint32 nextTick(Uint32 interval, void *param)
{
tick_time++;
if (tick_time == MAX_TIME) tick_time = 0;
return interval;
}
/**
* Updates fps.
*/
Uint32 nextSecond(Uint32 interval, void *param)
{
fps = frame;
frame = 0;
return interval;
}
int get_elapsed_time(int start_time)
{
if (start_time <= tick_time) {
return (tick_time - start_time) * 10;
}
else {
return (tick_time + (MAX_TIME - start_time)) * 10;
}
}
/**
* Create all the various globally accessible gui windows
*/
void createGuiWindows()
{
// Create dialogs
chatWindow = new ChatWindow;
menuWindow = new MenuWindow();
statusWindow = new StatusWindow(player_node);
miniStatusWindow = new MiniStatusWindow();
buyDialog = new BuyDialog;
sellDialog = new SellDialog;
buySellDialog = new BuySellDialog();
inventoryWindow = new InventoryWindow();
npcTextDialog = new NpcTextDialog();
npcListDialog = new NpcListDialog();
skillDialog = new SkillDialog();
//newSkillWindow = new NewSkillDialog();
setupWindow = new Setup();
minimap = new Minimap();
equipmentWindow = new EquipmentWindow(player_node->mEquipment.get());
chargeDialog = new ChargeDialog();
tradeWindow = new TradeWindow;
//buddyWindow = new BuddyWindow();
helpWindow = new HelpWindow();
debugWindow = new DebugWindow();
// Initialize window positions
int screenW = graphics->getWidth();
int screenH = graphics->getHeight();
chargeDialog->setPosition(
screenW - 5 - chargeDialog->getWidth(),
screenH - chargeDialog->getHeight() - 15);
tradeWindow->setPosition(screenW - statusWindow->getWidth() -
tradeWindow->getWidth() - 10,
inventoryWindow->getY() + inventoryWindow->getHeight());
/*buddyWindow->setPosition(10,
minimap->getHeight() + 30);*/
// Set initial window visibility
chatWindow->setSticky(true);
miniStatusWindow->setSticky(true);
menuWindow->setSticky(true);
chatWindow->setVisible(true);
miniStatusWindow->setVisible(true);
statusWindow->setVisible(false);
menuWindow->setVisible(true);
buyDialog->setVisible(false);
sellDialog->setVisible(false);
buySellDialog->setVisible(false);
inventoryWindow->setVisible(false);
npcTextDialog->setVisible(false);
npcListDialog->setVisible(false);
skillDialog->setVisible(false);
//newSkillWindow->setVisible(false);
setupWindow->setVisible(false);
equipmentWindow->setVisible(false);
chargeDialog->setVisible(false);
tradeWindow->setVisible(false);
//buddyWindow->setVisible(false);
helpWindow->setVisible(false);
debugWindow->setVisible(false);
}
/**
* Destroy all the globally accessible gui windows
*/
void destroyGuiWindows()
{
delete chatWindow;
delete statusWindow;
delete miniStatusWindow;
delete menuWindow;
delete buyDialog;
delete sellDialog;
delete buySellDialog;
delete inventoryWindow;
delete npcListDialog;
delete npcTextDialog;
delete skillDialog;
delete setupWindow;
delete minimap;
delete equipmentWindow;
delete chargeDialog;
//delete newSkillWindow;
delete tradeWindow;
//delete buddyWindow;
delete helpWindow;
delete debugWindow;
}
Game::Game():
mBeingHandler(new BeingHandler()),
mBuySellHandler(new BuySellHandler()),
mChatHandler(new ChatHandler()),
mEquipmentHandler(new EquipmentHandler()),
mInventoryHandler(new InventoryHandler()),
mItemHandler(new ItemHandler()),
mNpcHandler(new NPCHandler()),
mPlayerHandler(new PlayerHandler()),
mSkillHandler(new SkillHandler()),
mTradeHandler(new TradeHandler())
{
createGuiWindows();
engine = new Engine;
beingManager = new BeingManager;
floorItemManager = new FloorItemManager();
// Initialize timers
tick_time = 0;
SDL_AddTimer(10, nextTick, NULL); // Logic counter
SDL_AddTimer(1000, nextSecond, NULL); // Seconds counter
// Initialize beings
beingManager->setPlayer(player_node);
Joystick::init();
// TODO: The user should be able to choose which one to use
// Open the first device
if (Joystick::getNumberOfJoysticks() > 0)
{
joystick = new Joystick(0);
}
Network::registerHandler(mBeingHandler.get());
Network::registerHandler(mBuySellHandler.get());
Network::registerHandler(mChatHandler.get());
Network::registerHandler(mEquipmentHandler.get());
Network::registerHandler(mInventoryHandler.get());
Network::registerHandler(mItemHandler.get());
Network::registerHandler(mNpcHandler.get());
Network::registerHandler(mPlayerHandler.get());
Network::registerHandler(mSkillHandler.get());
Network::registerHandler(mTradeHandler.get());
}
Game::~Game()
{
delete engine;
delete player_node;
destroyGuiWindows();
delete beingManager;
delete floorItemManager;
delete joystick;
beingManager = NULL;
floorItemManager = NULL;
joystick = NULL;
}
bool saveScreenshot(SDL_Surface *screenshot)
{
static unsigned int screenshotCount = 0;
// Search for an unused screenshot name
std::stringstream filename;
std::fstream testExists;
bool found = false;
do {
screenshotCount++;
filename.str("");
#if (defined __USE_UNIX98 || defined __FreeBSD__)
filename << PHYSFS_getUserDir() << "/";
#endif
filename << "TMW_Screenshot_" << screenshotCount << ".png";
testExists.open(filename.str().c_str(), std::ios::in);
found = !testExists.is_open();
testExists.close();
} while (!found);
return ImageWriter::writePNG(screenshot, filename.str());
}
void Game::logic()
{
int gameTime = tick_time;
int drawTime = tick_time * 10;
int delta = 0;
int fpsLimit = 0;
while (!done)
{
// Handle all necessary game logic
while (get_elapsed_time(gameTime) > 0)
{
handleInput();
engine->logic();
gameTime++;
}
gameTime = tick_time;
fpsLimit = (int)config.getValue("fpslimit", 50);
delta = fpsLimit ? 1000 / fpsLimit : 0;
// Update the screen when application is active, delay otherwise
if (SDL_GetAppState() & SDL_APPACTIVE &&
(abs(tick_time * 10 - drawTime) >= delta))
{
frame++;
engine->draw(graphics);
graphics->updateScreen();
drawTime += delta;
}
else
{
SDL_Delay(10);
}
// Handle network stuff
Network::flush();
}
}
void Game::handleInput()
{
// Get the state of the keyboard keys
Uint8* keys;
keys = SDL_GetKeyState(NULL);
if (joystick != NULL)
{
joystick->update();
}
// Events
SDL_Event event;
while (SDL_PollEvent(&event))
{
bool used = false;
// Keyboard events (for discontinuous keys)
if (event.type == SDL_KEYDOWN)
{
gcn::Window *requestedWindow = NULL;
switch (event.key.keysym.sym)
{
case SDLK_F1:
// In-game Help
if (helpWindow->isVisible())
{
helpWindow->setVisible(false);
}
else
{
helpWindow->loadHelp("index");
helpWindow->requestMoveToTop();
}
used = true;
break;
case SDLK_F2: requestedWindow = statusWindow; break;
case SDLK_F3: requestedWindow = inventoryWindow; break;
case SDLK_F4: requestedWindow = equipmentWindow; break;
case SDLK_F5: requestedWindow = skillDialog; break;
case SDLK_F6: requestedWindow = minimap; break;
case SDLK_F7: requestedWindow = chatWindow; break;
//case SDLK_F8: requestedWindow = buddyWindow; break;
case SDLK_F9: requestedWindow = setupWindow; break;
case SDLK_F10: requestedWindow = debugWindow; break;
//case SDLK_F11: requestedWindow = newSkillWindow; break;
case SDLK_RETURN:
// Input chat window
if (chatWindow->isFocused() ||
deathNotice != NULL ||
weightNotice != NULL)
{
break;
}
// Quit by pressing Enter if the exit confirm is there
if (exitConfirm)
{
done = true;
}
// Close the Browser if opened
else if (helpWindow->isVisible())
{
helpWindow->setVisible(false);
}
// Close the config window, cancelling changes if opened
else if (setupWindow->isVisible())
{
setupWindow->action("cancel", NULL);
}
// Else, open the chat edit box
else
{
chatWindow->requestChatFocus();
used = true;
}
break;
// Attempt to hide all windows
case SDLK_h:
chatWindow->setVisible(false);
miniStatusWindow->setVisible(false);
statusWindow->setVisible(false);
menuWindow->setVisible(false);
buyDialog->setVisible(false);
sellDialog->setVisible(false);
buySellDialog->setVisible(false);
inventoryWindow->setVisible(false);
npcTextDialog->setVisible(false);
npcListDialog->setVisible(false);
skillDialog->setVisible(false);
//newSkillWindow->setVisible(false);
setupWindow->setVisible(false);
equipmentWindow->setVisible(false);
chargeDialog->setVisible(false);
tradeWindow->setVisible(false);
//buddyWindow->setVisible(false);
helpWindow->setVisible(false);
debugWindow->setVisible(false);
break;
// Picking up items on the floor
case SDLK_g:
case SDLK_z:
if (!chatWindow->isFocused())
{
Uint16 x = player_node->mX / 32, y = player_node->mY / 32;
FloorItem *item = floorItemManager->findByCoordinates(x, y);
// If none below the player, try the tile in front of
// the player
if (!item) {
if (player_node->mDirection & Being::UP)
y--;
if (player_node->mDirection & Being::DOWN)
y++;
if (player_node->mDirection & Being::LEFT)
x--;
if (player_node->mDirection & Being::RIGHT)
x++;
item = floorItemManager->findByCoordinates(x, y);
}
if (item)
player_node->pickUp(item);
used = true;
}
break;
// Quitting confirmation dialog
case SDLK_ESCAPE:
if (!exitConfirm) {
exitConfirm = new ConfirmDialog(
"Quit", "Are you sure you want to quit?");
exitConfirm->addActionListener(&exitListener);
}
exitConfirm->requestMoveToTop();
break;
default:
break;
}
if (requestedWindow)
{
requestedWindow->setVisible(!requestedWindow->isVisible());
if (requestedWindow->isVisible())
{
requestedWindow->requestMoveToTop();
}
used = true;
}
// Keys pressed together with Alt/Meta
// Emotions and some internal gui windows
if (event.key.keysym.mod & KMOD_ALT)
{
switch (event.key.keysym.sym)
{
case SDLK_s:
// Player sit action
player_node->toggleSit();
used = true;
break;
case SDLK_p:
// Screenshot (picture, hence the p)
{
SDL_Surface *screenshot = graphics->getScreenshot();
if (!saveScreenshot(screenshot))
{
logger->log("Error: could not save Screenshot.");
}
SDL_FreeSurface(screenshot);
}
used = true;
break;
default:
break;
case SDLK_f:
// Find path to mouse (debug purpose)
engine->toggleDebugPath();
used = true;
break;
}
// Emotions
Uint8 emotion;
switch (event.key.keysym.sym)
{
case SDLK_1: emotion = 1; break;
case SDLK_2: emotion = 2; break;
case SDLK_3: emotion = 3; break;
case SDLK_4: emotion = 4; break;
case SDLK_5: emotion = 5; break;
case SDLK_6: emotion = 6; break;
case SDLK_7: emotion = 7; break;
case SDLK_8: emotion = 8; break;
case SDLK_9: emotion = 9; break;
case SDLK_0: emotion = 10; break;
default: emotion = 0; break;
}
if (emotion)
{
player_node->emote(emotion);
used = true;
}
}
}
// Quit event
else if (event.type == SDL_QUIT)
{
done = true;
}
// Push input to GUI when not used
if (!used) {
guiInput->pushInput(event);
}
} // End while
// Moving player around
if (player_node->mAction != Being::DEAD &&
current_npc == 0 &&
!chatWindow->isFocused())
{
Uint16 x = player_node->mX / 32, y = player_node->mY / 32;
unsigned char Direction = 0;
// Translate pressed keys to movement and direction
if (keys[SDLK_UP] || keys[SDLK_KP8] ||
keys[SDLK_KP7] || keys[SDLK_KP9] ||
joystick && joystick->isUp())
{
Direction |= Being::UP;
}
else if (keys[SDLK_DOWN] || keys[SDLK_KP2] ||
keys[SDLK_KP1] || keys[SDLK_KP3] ||
joystick && joystick->isDown())
{
Direction |= Being::DOWN;
}
if (keys[SDLK_LEFT] || keys[SDLK_KP4] ||
keys[SDLK_KP1] || keys[SDLK_KP7] ||
joystick && joystick->isLeft())
{
Direction |= Being::LEFT;
}
else if (keys[SDLK_RIGHT] || keys[SDLK_KP6] ||
keys[SDLK_KP3] || keys[SDLK_KP9] ||
joystick && joystick->isRight())
{
Direction |= Being::RIGHT;
}
player_node->walk(Direction);
// Attacking monsters
if (keys[SDLK_LCTRL] || keys[SDLK_RCTRL] ||
joystick && joystick->buttonPressed(0))
{
Being *target = NULL;
bool newTarget = keys[SDLK_LSHIFT] || keys[SDLK_RSHIFT];
// A set target has highest priority
if (newTarget || !player_node->getTarget())
{
Uint16 targetX = x, targetY = y;
if (player_node->mDirection & Being::UP)
targetY--;
if (player_node->mDirection & Being::DOWN)
targetY++;
if (player_node->mDirection & Being::LEFT)
targetX--;
if (player_node->mDirection & Being::RIGHT)
targetX++;
// Attack priority is: Monster, Player, auto target
target = beingManager->findBeing(
targetX, targetY, Being::MONSTER);
if (!target)
target = beingManager->findBeing(
targetX, targetY, Being::PLAYER);
}
player_node->attack(target, newTarget);
}
if (joystick)
{
if (joystick->buttonPressed(1))
{
FloorItem *item = floorItemManager->findByCoordinates(x, y);
if (item)
player_node->pickUp(item);
}
else if (joystick->buttonPressed(2))
{
player_node->toggleSit();
}
}
}
}