| 1 |
// ____ _ __ |
| 2 |
// / __ )____ _____ | | / /___ ___________ |
| 3 |
// / __ / __ \/ ___/ | | /| / / __ `/ ___/ ___/ |
| 4 |
// / /_/ / /_/ (__ ) | |/ |/ / /_/ / / (__ ) |
| 5 |
// /_____/\____/____/ |__/|__/\__,_/_/ /____/ |
| 6 |
// |
| 7 |
// A futuristic real-time strategy game. |
| 8 |
// This file is part of Bos Wars. |
| 9 |
// |
| 10 |
/**@name mainloop.cpp - The main game loop. */ |
| 11 |
// |
| 12 |
// (c) Copyright 1998-2008 by Lutz Sammer and Jimmy Salmon |
| 13 |
// |
| 14 |
// This program is free software; you can redistribute it and/or modify |
| 15 |
// it under the terms of the GNU General Public License as published by |
| 16 |
// the Free Software Foundation; only version 2 of the License. |
| 17 |
// |
| 18 |
// This program is distributed in the hope that it will be useful, |
| 19 |
// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 20 |
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 21 |
// GNU General Public License for more details. |
| 22 |
// |
| 23 |
// You should have received a copy of the GNU General Public License |
| 24 |
// along with this program; if not, write to the Free Software |
| 25 |
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA |
| 26 |
// 02111-1307, USA. |
| 27 |
// |
| 28 |
|
| 29 |
//@{ |
| 30 |
|
| 31 |
//---------------------------------------------------------------------------- |
| 32 |
// Includes |
| 33 |
//---------------------------------------------------------------------------- |
| 34 |
|
| 35 |
#include <string.h> |
| 36 |
#include <stdio.h> |
| 37 |
#include <stdlib.h> |
| 38 |
#if defined(DEBUG) |
| 39 |
#include <setjmp.h> |
| 40 |
#endif |
| 41 |
|
| 42 |
#include "stratagus.h" |
| 43 |
#include "video.h" |
| 44 |
#include "map.h" |
| 45 |
#include "font.h" |
| 46 |
#include "sound.h" |
| 47 |
#include "sound_server.h" |
| 48 |
#include "unitsound.h" |
| 49 |
#include "unittype.h" |
| 50 |
#include "player.h" |
| 51 |
#include "unit.h" |
| 52 |
#include "cursor.h" |
| 53 |
#include "minimap.h" |
| 54 |
#include "actions.h" |
| 55 |
#include "missile.h" |
| 56 |
#include "interface.h" |
| 57 |
#include "menus.h" |
| 58 |
#include "network.h" |
| 59 |
#include "ui.h" |
| 60 |
#include "unit.h" |
| 61 |
#include "trigger.h" |
| 62 |
#include "results.h" |
| 63 |
#include "settings.h" |
| 64 |
#include "replay.h" |
| 65 |
#include "pathfinder.h" |
| 66 |
#include "editor.h" |
| 67 |
#include "sound.h" |
| 68 |
#include "script.h" |
| 69 |
#include "particle.h" |
| 70 |
|
| 71 |
#include <guichan.h> |
| 72 |
void DrawGuichanWidgets(); |
| 73 |
|
| 74 |
//---------------------------------------------------------------------------- |
| 75 |
// Variables |
| 76 |
//---------------------------------------------------------------------------- |
| 77 |
|
| 78 |
/// variable set when we are scrolling via keyboard |
| 79 |
int KeyScrollState = ScrollNone; |
| 80 |
|
| 81 |
/// variable set when we are scrolling via mouse |
| 82 |
int MouseScrollState = ScrollNone; |
| 83 |
|
| 84 |
EventCallback GameCallbacks; /// Game callbacks |
| 85 |
EventCallback EditorCallbacks; /// Editor callbacks |
| 86 |
|
| 87 |
//---------------------------------------------------------------------------- |
| 88 |
// Functions |
| 89 |
//---------------------------------------------------------------------------- |
| 90 |
|
| 91 |
/** |
| 92 |
** Handle scrolling area. |
| 93 |
** |
| 94 |
** @param state Scroll direction/state. |
| 95 |
** @param fast Flag scroll faster. |
| 96 |
** |
| 97 |
** @todo Support dynamic acceleration of scroll speed. |
| 98 |
** @todo If the scroll key is longer pressed the area is scrolled faster. |
| 99 |
*/ |
| 100 |
void DoScrollArea(int state, bool fast) |
| 101 |
{ |
| 102 |
CViewport *vp; |
| 103 |
int stepx; |
| 104 |
int stepy; |
| 105 |
static int remx = 0; // FIXME: docu |
| 106 |
static int remy = 0; // FIXME: docu |
| 107 |
|
| 108 |
if (state == ScrollNone) { |
| 109 |
return; |
| 110 |
} |
| 111 |
|
| 112 |
vp = UI.SelectedViewport; |
| 113 |
|
| 114 |
if (fast) { |
| 115 |
stepx = vp->MapWidth / 2 * TileSizeX * FRAMES_PER_SECOND; |
| 116 |
stepy = vp->MapHeight / 2 * TileSizeY * FRAMES_PER_SECOND; |
| 117 |
} else {// dynamic: let these variables increase upto fast.. |
| 118 |
// FIXME: pixels per second should be configurable |
| 119 |
stepx = TileSizeX * FRAMES_PER_SECOND; |
| 120 |
stepy = TileSizeY * FRAMES_PER_SECOND; |
| 121 |
} |
| 122 |
if ((state & (ScrollLeft | ScrollRight)) && |
| 123 |
(state & (ScrollLeft | ScrollRight)) != (ScrollLeft | ScrollRight)) { |
| 124 |
stepx = stepx * 100 * 100 / VideoSyncSpeed / FRAMES_PER_SECOND / (SkipFrames + 1); |
| 125 |
remx += stepx - (stepx / 100) * 100; |
| 126 |
stepx /= 100; |
| 127 |
if (remx > 100) { |
| 128 |
++stepx; |
| 129 |
remx -= 100; |
| 130 |
} |
| 131 |
} else { |
| 132 |
stepx = 0; |
| 133 |
} |
| 134 |
if ((state & (ScrollUp | ScrollDown)) && |
| 135 |
(state & (ScrollUp | ScrollDown)) != (ScrollUp | ScrollDown)) { |
| 136 |
stepy = stepy * 100 * 100 / VideoSyncSpeed / FRAMES_PER_SECOND / (SkipFrames + 1); |
| 137 |
remy += stepy - (stepy / 100) * 100; |
| 138 |
stepy /= 100; |
| 139 |
if (remy > 100) { |
| 140 |
++stepy; |
| 141 |
remy -= 100; |
| 142 |
} |
| 143 |
} else { |
| 144 |
stepy = 0; |
| 145 |
} |
| 146 |
|
| 147 |
if (state & ScrollUp) { |
| 148 |
stepy = -stepy; |
| 149 |
} |
| 150 |
if (state & ScrollLeft) { |
| 151 |
stepx = -stepx; |
| 152 |
} |
| 153 |
vp->Set(vp->MapX, vp->MapY, vp->OffsetX + stepx, vp->OffsetY + stepy); |
| 154 |
|
| 155 |
// This recalulates some values |
| 156 |
HandleMouseMove(CursorX, CursorY); |
| 157 |
} |
| 158 |
|
| 159 |
/** |
| 160 |
** Draw map area |
| 161 |
*/ |
| 162 |
void DrawMapArea(void) |
| 163 |
{ |
| 164 |
// Draw all of the viewports |
| 165 |
for (CViewport *vp = UI.Viewports; vp < UI.Viewports + UI.NumViewports; ++vp) { |
| 166 |
// Center viewport on tracked unit |
| 167 |
if (vp->Unit) { |
| 168 |
if (vp->Unit->Destroyed || |
| 169 |
vp->Unit->Orders[0]->Action == UnitActionDie) { |
| 170 |
vp->Unit = NoUnitP; |
| 171 |
} else { |
| 172 |
vp->Center(vp->Unit->X, vp->Unit->Y, |
| 173 |
vp->Unit->IX + TileSizeX / 2, vp->Unit->IY + TileSizeY / 2); |
| 174 |
} |
| 175 |
} |
| 176 |
|
| 177 |
vp->Draw(); |
| 178 |
} |
| 179 |
} |
| 180 |
|
| 181 |
/** |
| 182 |
** Display update. |
| 183 |
** |
| 184 |
** This functions updates everything on screen. The map, the gui, the |
| 185 |
** cursors. |
| 186 |
*/ |
| 187 |
void UpdateDisplay(void) |
| 188 |
{ |
| 189 |
if (GameRunning || Editor.Running == EditorEditing) { |
| 190 |
DrawMapArea(); |
| 191 |
DrawMessages(); |
| 192 |
|
| 193 |
if (CursorState == CursorStateRectangle) { |
| 194 |
DrawCursor(); |
| 195 |
} |
| 196 |
|
| 197 |
if (!BigMapMode) { |
| 198 |
for (int i = 0; i < (int)UI.Fillers.size(); ++i) { |
| 199 |
UI.Fillers[i].G->DrawClip(UI.Fillers[i].X, UI.Fillers[i].Y); |
| 200 |
} |
| 201 |
DrawMenuButtonArea(); |
| 202 |
|
| 203 |
UI.Minimap.Draw(UI.SelectedViewport->MapX, UI.SelectedViewport->MapY); |
| 204 |
UI.Minimap.DrawCursor(UI.SelectedViewport->MapX, |
| 205 |
UI.SelectedViewport->MapY); |
| 206 |
|
| 207 |
UI.InfoPanel.Draw(); |
| 208 |
UI.ButtonPanel.Draw(); |
| 209 |
DrawResources(); |
| 210 |
UI.StatusLine.Draw(); |
| 211 |
} |
| 212 |
|
| 213 |
DrawCosts(); |
| 214 |
DrawTimer(); |
| 215 |
} |
| 216 |
|
| 217 |
DrawPieMenu(); // draw pie menu only if needed |
| 218 |
|
| 219 |
DrawGuichanWidgets(); |
| 220 |
|
| 221 |
if (CursorState != CursorStateRectangle) { |
| 222 |
DrawCursor(); |
| 223 |
} |
| 224 |
|
| 225 |
// |
| 226 |
// Update changes to display. |
| 227 |
// |
| 228 |
Invalidate(); |
| 229 |
} |
| 230 |
|
| 231 |
static void InitGameCallbacks(void) |
| 232 |
{ |
| 233 |
GameCallbacks.ButtonPressed = HandleButtonDown; |
| 234 |
GameCallbacks.ButtonReleased = HandleButtonUp; |
| 235 |
GameCallbacks.MouseMoved = HandleMouseMove; |
| 236 |
GameCallbacks.MouseExit = HandleMouseExit; |
| 237 |
GameCallbacks.KeyPressed = HandleKeyDown; |
| 238 |
GameCallbacks.KeyReleased = HandleKeyUp; |
| 239 |
GameCallbacks.KeyRepeated = HandleKeyRepeat; |
| 240 |
GameCallbacks.NetworkEvent = NetworkEvent; |
| 241 |
} |
| 242 |
|
| 243 |
/** |
| 244 |
** Game main loop. |
| 245 |
** |
| 246 |
** Unit actions. |
| 247 |
** Missile actions. |
| 248 |
** Players (AI). |
| 249 |
** Cyclic events (color cycle,...) |
| 250 |
** Display update. |
| 251 |
** Input/Network/Sound. |
| 252 |
*/ |
| 253 |
void GameMainLoop(void) |
| 254 |
{ |
| 255 |
int player; |
| 256 |
const EventCallback *old_callbacks; |
| 257 |
|
| 258 |
InitGameCallbacks(); |
| 259 |
|
| 260 |
old_callbacks = GetCallbacks(); |
| 261 |
SetCallbacks(&GameCallbacks); |
| 262 |
|
| 263 |
SetVideoSync(); |
| 264 |
GameCursor = UI.Point.Cursor; |
| 265 |
GameRunning = true; |
| 266 |
|
| 267 |
CParticleManager::init(); |
| 268 |
|
| 269 |
MultiPlayerReplayEachCycle(); |
| 270 |
|
| 271 |
CclCommand("GameStarting()"); |
| 272 |
|
| 273 |
while (GameRunning) { |
| 274 |
|
| 275 |
// Can't find a better place. |
| 276 |
SaveGameLoading = false; |
| 277 |
// |
| 278 |
// Game logic part |
| 279 |
// |
| 280 |
if (!GamePaused && NetworkInSync && !SkipGameCycle) { |
| 281 |
SinglePlayerReplayEachCycle(); |
| 282 |
++GameCycle; |
| 283 |
MultiPlayerReplayEachCycle(); |
| 284 |
NetworkCommands(); // Get network commands |
| 285 |
UnitActions(); // handle units |
| 286 |
MissileActions(); // handle missiles |
| 287 |
PlayersEachCycle(); // handle players |
| 288 |
UpdateTimer(); // update game timer |
| 289 |
|
| 290 |
// |
| 291 |
// Work todo each second. |
| 292 |
// Split into different frames, to reduce cpu time. |
| 293 |
// Increment charge of spell casting units. |
| 294 |
// Update mini-map. |
| 295 |
// Update map fog of war. |
| 296 |
// Call AI. |
| 297 |
// Check game goals. |
| 298 |
// Check rescue of units. |
| 299 |
// |
| 300 |
switch (GameCycle % CYCLES_PER_SECOND) { |
| 301 |
case 0: // At cycle 0, start all ai players... |
| 302 |
if (GameCycle == 0) { |
| 303 |
for (player = 0; player < NumPlayers; ++player) { |
| 304 |
PlayersEachSecond(player); |
| 305 |
} |
| 306 |
} |
| 307 |
break; |
| 308 |
case 1: |
| 309 |
break; |
| 310 |
case 2: |
| 311 |
break; |
| 312 |
case 3: // minimap update |
| 313 |
UI.Minimap.Update(); |
| 314 |
break; |
| 315 |
case 4: |
| 316 |
break; |
| 317 |
case 5: |
| 318 |
break; |
| 319 |
case 6: // overtaking units |
| 320 |
RescueUnits(); |
| 321 |
break; |
| 322 |
default: |
| 323 |
// FIXME: assume that NumPlayers < (CYCLES_PER_SECOND - 7) |
| 324 |
player = (GameCycle % CYCLES_PER_SECOND) - 7; |
| 325 |
Assert(player >= 0); |
| 326 |
if (player < NumPlayers) { |
| 327 |
PlayersEachSecond(player); |
| 328 |
} |
| 329 |
} |
| 330 |
} |
| 331 |
|
| 332 |
TriggersEachCycle(); // handle triggers |
| 333 |
UpdateMessages(); // update messages |
| 334 |
ParticleManager.update(); // handle particles |
| 335 |
|
| 336 |
CheckMusicFinished(); // Check for next song |
| 337 |
|
| 338 |
// |
| 339 |
// Map scrolling |
| 340 |
// |
| 341 |
DoScrollArea(MouseScrollState | KeyScrollState, (KeyModifiers & ModifierControl) != 0); |
| 342 |
|
| 343 |
if (FastForwardCycle <= GameCycle || GameCycle <= 10 || !(GameCycle & 0x3f)) { |
| 344 |
//FIXME: this might be better placed somewhere at front of the |
| 345 |
// program, as we now still have a game on the background and |
| 346 |
// need to go through the game-menu or supply a map file |
| 347 |
UpdateDisplay(); |
| 348 |
|
| 349 |
// |
| 350 |
// If double-buffered mode, we will display the contains of |
| 351 |
// VideoMemory. If direct mode this does nothing. In X11 it does |
| 352 |
// XFlush |
| 353 |
// |
| 354 |
RealizeVideoMemory(); |
| 355 |
} |
| 356 |
|
| 357 |
if (FastForwardCycle <= GameCycle || !(GameCycle & 0x3f)) { |
| 358 |
WaitEventsOneFrame(); |
| 359 |
} |
| 360 |
if (!NetworkInSync) { |
| 361 |
NetworkRecover(); // recover network |
| 362 |
} |
| 363 |
} |
| 364 |
|
| 365 |
// |
| 366 |
// Game over |
| 367 |
// |
| 368 |
NetworkQuit(); |
| 369 |
EndReplayLog(); |
| 370 |
|
| 371 |
CParticleManager::exit(); |
| 372 |
|
| 373 |
FlagRevealMap = 0; |
| 374 |
ReplayRevealMap = 0; |
| 375 |
GamePaused = false; |
| 376 |
GodMode = false; |
| 377 |
|
| 378 |
SetCallbacks(old_callbacks); |
| 379 |
} |
| 380 |
|
| 381 |
//@} |